Skip to content

fix: Inherit PHP settings from the main process #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions src/Input/ChildCommandFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
final class ChildCommandFactory
{
public function __construct(
private readonly string $phpExecutable,
public readonly string $phpExecutable,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be able to pass the PHP executable to PhpSubprocess we need to expose it, instead of passing it as part of the created command

private readonly string $scriptPath,
private readonly string $commandName,
private readonly InputDefinition $commandDefinition,
Expand All @@ -51,7 +51,6 @@ private function createBaseCommand(
InputInterface $input
): array {
return array_filter([
...$this->getEscapedPhpExecutable(),
$this->scriptPath,
$this->commandName,
...array_map(strval(...), self::getArguments($input)),
Expand All @@ -74,14 +73,6 @@ private function getForwardedOptions(InputInterface $input): array
);
}

/**
* @return list<string>
*/
private function getEscapedPhpExecutable(): array
{
return explode(' ', $this->phpExecutable);
}

/**
* @return list<string|bool|int|float|null|array<string|bool|int|float|null>>
*/
Expand Down
1 change: 1 addition & 0 deletions src/ParallelExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ private function createProcessLauncher(
Logger $logger
): ProcessLauncher {
return $this->processLauncherFactory->create(
$this->childCommandFactory->phpExecutable,
$this->childCommandFactory->createChildCommand($input),
$this->workingDirectory,
$this->extraEnvironmentVariables,
Expand Down
1 change: 1 addition & 0 deletions src/Process/ProcessLauncherFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface ProcessLauncherFactory
* @param callable(): void $tick
*/
public function create(
string $phpExecutable,
array $command,
string $workingDirectory,
?array $extraEnvironmentVariables,
Expand Down
5 changes: 2 additions & 3 deletions src/Process/StandardSymfonyProcessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
namespace Webmozarts\Console\Parallelization\Process;

use Symfony\Component\Process\InputStream;
use Symfony\Component\Process\PhpSubprocess;
use Symfony\Component\Process\Process;

final class StandardSymfonyProcessFactory implements SymfonyProcessFactory
{
public function startProcess(

Check failure on line 22 in src/Process/StandardSymfonyProcessFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Parameter #6 $processOutput (callable) of method Webmozarts\Console\Parallelization\Process\StandardSymfonyProcessFactory::startProcess() is not contravariant with parameter #6 $environmentVariables (array|null) of method Webmozarts\Console\Parallelization\Process\SymfonyProcessFactory::startProcess().

Check failure on line 22 in src/Process/StandardSymfonyProcessFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Parameter #5 $environmentVariables (array|null) of method Webmozarts\Console\Parallelization\Process\StandardSymfonyProcessFactory::startProcess() is not contravariant with parameter #5 $workingDirectory (string) of method Webmozarts\Console\Parallelization\Process\SymfonyProcessFactory::startProcess().

Check failure on line 22 in src/Process/StandardSymfonyProcessFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Parameter #4 $workingDirectory (string) of method Webmozarts\Console\Parallelization\Process\StandardSymfonyProcessFactory::startProcess() is not contravariant with parameter #4 $command (array) of method Webmozarts\Console\Parallelization\Process\SymfonyProcessFactory::startProcess().

Check failure on line 22 in src/Process/StandardSymfonyProcessFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Parameter #3 $command (array) of method Webmozarts\Console\Parallelization\Process\StandardSymfonyProcessFactory::startProcess() is not contravariant with parameter #3 $phpExecutable (string) of method Webmozarts\Console\Parallelization\Process\SymfonyProcessFactory::startProcess().

Check failure on line 22 in src/Process/StandardSymfonyProcessFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Method Webmozarts\Console\Parallelization\Process\StandardSymfonyProcessFactory::startProcess() overrides method Webmozarts\Console\Parallelization\Process\SymfonyProcessFactory::startProcess() but misses parameter #7 $processOutput.

Check failure on line 22 in src/Process/StandardSymfonyProcessFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Method Webmozarts\Console\Parallelization\Process\StandardSymfonyProcessFactory::startProcess() has parameter $environmentVariables with no value type specified in iterable type array.

Check failure on line 22 in src/Process/StandardSymfonyProcessFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Method Webmozarts\Console\Parallelization\Process\StandardSymfonyProcessFactory::startProcess() has parameter $command with no value type specified in iterable type array.
int $index,
InputStream $inputStream,
array $command,
Expand All @@ -26,12 +27,10 @@
?array $environmentVariables,
callable $processOutput
): Process {
$process = new Process(
$process = new PhpSubprocess(
$command,
$workingDirectory,
$environmentVariables,
null,
null,
);

$process->setInput($inputStream);
Expand Down
1 change: 1 addition & 0 deletions src/Process/SymfonyProcessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ interface SymfonyProcessFactory
public function startProcess(
int $index,
InputStream $inputStream,
string $phpExecutable,
array $command,
string $workingDirectory,
?array $environmentVariables,
Expand Down
2 changes: 2 additions & 0 deletions src/Process/SymfonyProcessLauncher.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ final class SymfonyProcessLauncher implements ProcessLauncher
* @param callable(): void $tick
*/
public function __construct(
private readonly string $phpExecutable,
private readonly array $command,
private readonly string $workingDirectory,
private readonly ?array $environmentVariables,
Expand Down Expand Up @@ -135,6 +136,7 @@ private function startProcess(InputStream $inputStream): void
$process = $this->processFactory->startProcess(
$index,
$inputStream,
$this->phpExecutable,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change is a BC break

$this->command,
$this->workingDirectory,
$this->environmentVariables,
Expand Down
2 changes: 2 additions & 0 deletions src/Process/SymfonyProcessLauncherFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function __construct(private readonly SymfonyProcessFactory $processFacto
* @param callable(): void $tick
*/
public function create(
string $phpExecutable,
array $command,
string $workingDirectory,
?array $extraEnvironmentVariables,
Expand All @@ -42,6 +43,7 @@ public function create(
callable $tick
): ProcessLauncher {
return new SymfonyProcessLauncher(
$phpExecutable,
$command,
$workingDirectory,
$extraEnvironmentVariables,
Expand Down
107 changes: 107 additions & 0 deletions tests/Fixtures/Command/PhpSettingsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/*
* This file is part of the Webmozarts Console Parallelization package.
*
* (c) Webmozarts GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Webmozarts\Console\Parallelization\Fixtures\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Terminal;
use Symfony\Component\Filesystem\Filesystem;
use Webmozarts\Console\Parallelization\ErrorHandler\ErrorHandler;
use Webmozarts\Console\Parallelization\Input\ParallelizationInput;
use Webmozarts\Console\Parallelization\Integration\TestDebugProgressBarFactory;
use Webmozarts\Console\Parallelization\Integration\TestLogger;
use Webmozarts\Console\Parallelization\Logger\Logger;
use Webmozarts\Console\Parallelization\Logger\NullLogger;
use Webmozarts\Console\Parallelization\Logger\StandardLogger;
use Webmozarts\Console\Parallelization\ParallelExecutorFactory;
use Webmozarts\Console\Parallelization\Parallelization;
use function file_get_contents;
use function ini_get;
use function json_decode;
use function realpath;
use const JSON_THROW_ON_ERROR;

final class PhpSettingsCommand extends Command
{
use Parallelization;

public const OUTPUT_DIR = __DIR__.'/../../../dist/php-settings';

public function __construct(
private Filesystem $filesystem,
) {
parent::__construct('test:php-settings');
}

protected function configure(): void
{
ParallelizationInput::configureCommand($this);
}

/**
* @return list<string>
*/
protected function fetchItems(InputInterface $input, OutputInterface $output): array
{
return ['item0'];
}

protected function getParallelExecutableFactory(
callable $fetchItems,
callable $runSingleCommand,
callable $getItemName,
string $commandName,
InputDefinition $commandDefinition,
ErrorHandler $errorHandler
): ParallelExecutorFactory {
return ParallelExecutorFactory::create(
$fetchItems,
$runSingleCommand,
$getItemName,
$commandName,
$commandDefinition,
$errorHandler,
)
->withRunBeforeFirstCommand(self::runBeforeFirstCommand(...))
->withScriptPath(realpath(__DIR__.'/../../../bin/console'));
}

private function runBeforeFirstCommand(): void
{
$this->filesystem->dumpFile(
self::OUTPUT_DIR.'_main_process',
ini_get('memory_limit'),
);
}

protected function runSingleCommand(string $item, InputInterface $input, OutputInterface $output): void
{
$this->filesystem->dumpFile(
self::OUTPUT_DIR,
ini_get('memory_limit'),
);
}

protected function getItemName(?int $count): string
{
return 1 === $count ? 'item' : 'items';
}

protected function createLogger(InputInterface $input, OutputInterface $output): Logger
{
return new NullLogger();
}
}
68 changes: 68 additions & 0 deletions tests/Integration/PhpProcessSettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the Webmozarts Console Parallelization package.
*
* (c) Webmozarts GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Webmozarts\Console\Parallelization\Integration;

use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Process\Process;
use Webmozarts\Console\Parallelization\Fixtures\Command\ImportMoviesCommand;
use Webmozarts\Console\Parallelization\Fixtures\Command\ImportUnknownMoviesCountCommand;
use Webmozarts\Console\Parallelization\Fixtures\Command\LegacyCommand;
use Webmozarts\Console\Parallelization\Fixtures\Command\NoSubProcessCommand;
use Webmozarts\Console\Parallelization\Fixtures\Command\PhpSettingsCommand;
use Webmozarts\Console\Parallelization\Integration\BareKernel;
use Webmozarts\Console\Parallelization\Integration\OutputNormalizer;
use Webmozarts\Console\Parallelization\Integration\TestLogger;
use function array_column;
use function array_map;
use function file_get_contents;
use function preg_replace;
use function spl_object_id;
use function str_replace;

/**
* @internal
*/
#[CoversNothing]
class PhpProcessSettingsTest extends TestCase
{
public function test_it_can_run_the_command_without_sub_processes(): void
{
$commandProcess = Process::fromShellCommandline(
'php -dmemory_limit="256M" bin/console test:php-settings',
__DIR__.'/../..',
);
$commandProcess->run();

$expectedMainProcessMemoryLimit = '256M';
$actualMainProcessMemoryLimit = file_get_contents(PhpSettingsCommand::OUTPUT_DIR.'_main_process');

$expectedChildProcessMemoryLimit = '256M';
$actualChildProcessMemoryLimit = file_get_contents(PhpSettingsCommand::OUTPUT_DIR);

self::assertSame(
[
$expectedMainProcessMemoryLimit,
$expectedChildProcessMemoryLimit,
],
[
$actualMainProcessMemoryLimit,
$actualChildProcessMemoryLimit,
],
);
}
}
Loading