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 all 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
4 changes: 2 additions & 2 deletions src/Input/ChildCommandFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* @param list<string> $phpExecutable
*/
public function __construct(
private array $phpExecutable,
public array $phpExecutable,
private string $scriptPath,
private string $commandName,
private InputDefinition $commandDefinition,
Expand All @@ -53,7 +53,7 @@ private function createBaseCommand(
InputInterface $input
): array {
return array_filter([
...$this->phpExecutable,
//...$this->phpExecutable,
$this->scriptPath,
$this->commandName,
...array_map(strval(...), self::getArguments($input)),
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
19 changes: 18 additions & 1 deletion src/ParallelExecutorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,24 @@ public function withProgressSymbol(string $progressSymbol): self
* The path of the PHP executable. It is the executable that will be used
* to spawn the child process(es).
*
* @param string|list<string> $phpExecutable e.g. ['/path/to/php', '-dmemory_limit=512M']
* It can be a string (the path of the PHP executable), or an array
* to set some PHP settings or others, for example:
*
* ```
* ['/path/to/php', '-dmemory_limit=512M']
* ```
*
* However, beware that those settings will take precedence over the
* inherited settings from the main process. As a result, if you execute:
*
* ```
* $ php -dmemory_limit=1024M bin/console my:command
* ```
*
* Then the memory limit of the main process will be 1024M, but the memory
* limit of the child processes will remain 512M.
*
* @param string|list<string> $phpExecutable
*/
public function withPhpExecutable(string|array $phpExecutable): self
{
Expand Down
1 change: 1 addition & 0 deletions src/Process/ProcessLauncherFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
* STDOUT or STDERR.
* @param callable(): void $tick
*/
public function create(

Check failure on line 33 in src/Process/ProcessLauncherFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Method Webmozarts\Console\Parallelization\Process\ProcessLauncherFactory::create() has parameter $phpExecutable with no value type specified in iterable type array.
array $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

array $command,
string $workingDirectory,
?array $extraEnvironmentVariables,
Expand Down
7 changes: 4 additions & 3 deletions src/Process/StandardSymfonyProcessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@
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)

Method Webmozarts\Console\Parallelization\Process\StandardSymfonyProcessFactory::startProcess() has parameter $phpExecutable with no value type specified in iterable type array.
int $index,
InputStream $inputStream,
array $phpExecutable,
array $command,
string $workingDirectory,
?array $environmentVariables,
callable $processOutput
): Process {
$process = new Process(
$process = new PhpSubprocess(
$command,
$workingDirectory,
$environmentVariables,
null,
null,
php: $phpExecutable,
);

$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 @@ -32,9 +32,10 @@
* there is some output available on
* STDOUT or STDERR.
*/
public function startProcess(

Check failure on line 35 in src/Process/SymfonyProcessFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Method Webmozarts\Console\Parallelization\Process\SymfonyProcessFactory::startProcess() has parameter $phpExecutable with no value type specified in iterable type array.
int $index,
InputStream $inputStream,
array $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

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 @@ -59,7 +59,8 @@
* STDOUT or STDERR.
* @param callable(): void $tick
*/
public function __construct(

Check failure on line 62 in src/Process/SymfonyProcessLauncher.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Method Webmozarts\Console\Parallelization\Process\SymfonyProcessLauncher::__construct() has parameter $phpExecutable with no value type specified in iterable type array.
private readonly array $phpExecutable,
private readonly array $command,
private readonly string $workingDirectory,
private readonly ?array $environmentVariables,
Expand Down Expand Up @@ -135,6 +136,7 @@
$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 @@ -31,7 +31,8 @@
* STDOUT or STDERR.
* @param callable(): void $tick
*/
public function create(

Check failure on line 34 in src/Process/SymfonyProcessLauncherFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Method Webmozarts\Console\Parallelization\Process\SymfonyProcessLauncherFactory::create() has parameter $phpExecutable with no value type specified in iterable type array.
array $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

array $command,
string $workingDirectory,
?array $extraEnvironmentVariables,
Expand All @@ -42,6 +43,7 @@
callable $tick
): ProcessLauncher {
return new SymfonyProcessLauncher(
$phpExecutable,
$command,
$workingDirectory,
$extraEnvironmentVariables,
Expand Down
121 changes: 121 additions & 0 deletions tests/Fixtures/Command/PhpSettingsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?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\ParallelCommand;
use Webmozarts\Console\Parallelization\ParallelExecutorFactory;
use Webmozarts\Console\Parallelization\Parallelization;
use Webmozarts\Console\Parallelization\Process\PhpExecutableFinder;
use function file_get_contents;
use function ini_get;
use function json_decode;
use function realpath;
use function sprintf;
use function xdebug_break;
use const JSON_THROW_ON_ERROR;

final class PhpSettingsCommand extends ParallelCommand
{
public const string MAIN_PROCESS_OUTPUT_DIR = __DIR__.'/../../../dist/php-settings_main-process';
public const string CHILD_PROCESS_OUTPUT_DIR = __DIR__.'/../../../dist/php-settings_child-process';

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

/**
* @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(...))
->withPhpExecutable([
...PhpExecutableFinder::find(),
'-dmax_input_time=30',
])
->withScriptPath(realpath(__DIR__.'/../../../bin/console'));
}

private function runBeforeFirstCommand(): void
{
$this->dumpMemoryLimit(self::MAIN_PROCESS_OUTPUT_DIR);
}

protected function runSingleCommand(string $item, InputInterface $input, OutputInterface $output): void
{
$this->dumpMemoryLimit(self::CHILD_PROCESS_OUTPUT_DIR);
}

private function dumpMemoryLimit(string $filePath): void
{
$this->filesystem->dumpFile(
$filePath,
sprintf(
'memory_limit=%s%smax_input_time=%s',
ini_get('memory_limit'),
"\n",
ini_get('max_input_time'),
),
);
}

public static function createSettingsOutput(string $memoryLimit, string $maxInputTime): string
{
return sprintf(
'memory_limit=%s%smax_input_time=%s',
$memoryLimit,
"\n",
$maxInputTime,
);
}

protected function getItemName(?int $count): string
{
return 1 === $count ? 'item' : 'items';
}
}
137 changes: 137 additions & 0 deletions tests/Integration/PhpProcessSettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?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\Filesystem\Filesystem;
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 ini_get;
use function preg_replace;
use function spl_object_id;
use function str_replace;

/**
* @internal
*/
#[CoversNothing]
class PhpProcessSettingsTest extends TestCase
{
protected function setUp(): void
{
self::cleanupOutputFiles();
}

protected function tearDown(): void
{
self::cleanupOutputFiles();
}

public function test_it_can_run_the_command_setting_the_memory_limit(): void
{
$commandProcess = Process::fromShellCommandline(
'php -dmemory_limit="256M" bin/console test:php-settings',
__DIR__.'/../..',
['XDEBUG_SESSION' => '1', 'XDEBUG_MODE' => 'debug'],
);
$commandProcess->run();

self::assertTrue(
$commandProcess->isSuccessful(),
$commandProcess->getOutput() . $commandProcess->getErrorOutput(),
);

$expectedMainProcessPhpSettings = PhpSettingsCommand::createSettingsOutput(
'256M', // comes from setting it when launching the command
ini_get('max_input_time'),
);
$actualMainProcessPhpSettings = file_get_contents(PhpSettingsCommand::MAIN_PROCESS_OUTPUT_DIR);

$expectedChildProcessPhpSettings = PhpSettingsCommand::createSettingsOutput(
'256M',
'30', // comes from PhpSettingsCommand specifying it in the config
);
$actualChildProcessPhpSettings = file_get_contents(PhpSettingsCommand::CHILD_PROCESS_OUTPUT_DIR);

self::assertSame(
[
'main' => $expectedMainProcessPhpSettings,
'child' => $expectedChildProcessPhpSettings,
],
[
'main' => $actualMainProcessPhpSettings,
'child' => $actualChildProcessPhpSettings,
],
);
}

public function test_it_can_run_the_command_setting_the_a_php_setting_configured_in_the_command(): void
{
$commandProcess = Process::fromShellCommandline(
'php -dmemory_limit="256M" -dmax_input_time=45 bin/console test:php-settings',
__DIR__.'/../..',
['XDEBUG_SESSION' => '1', 'XDEBUG_MODE' => 'debug'],
);
$commandProcess->run();

self::assertTrue(
$commandProcess->isSuccessful(),
$commandProcess->getOutput() . $commandProcess->getErrorOutput(),
);

$expectedMainProcessPhpSettings = PhpSettingsCommand::createSettingsOutput(
'256M', // comes from setting it when launching the command
'45', // comes from setting it when launching the command
);
$actualMainProcessPhpSettings = file_get_contents(PhpSettingsCommand::MAIN_PROCESS_OUTPUT_DIR);

$expectedChildProcessPhpSettings = PhpSettingsCommand::createSettingsOutput(
'256M',
'30', // comes from PhpSettingsCommand specifying it in the config
);
$actualChildProcessPhpSettings = file_get_contents(PhpSettingsCommand::CHILD_PROCESS_OUTPUT_DIR);

self::assertSame(
[
'main' => $expectedMainProcessPhpSettings,
'child' => $expectedChildProcessPhpSettings,
],
[
'main' => $actualMainProcessPhpSettings,
'child' => $actualChildProcessPhpSettings,
],
);
}

private static function cleanupOutputFiles(): void
{
$fileSystem = new Filesystem();
$fileSystem->remove(PhpSettingsCommand::MAIN_PROCESS_OUTPUT_DIR);
$fileSystem->remove(PhpSettingsCommand::CHILD_PROCESS_OUTPUT_DIR);
}
}
Loading