-
Notifications
You must be signed in to change notification settings - Fork 19
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
base: 3.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,8 @@ | |
* STDOUT or STDERR. | ||
* @param callable(): void $tick | ||
*/ | ||
public function create( | ||
Check failure on line 33 in src/Process/ProcessLauncherFactory.php
|
||
array $phpExecutable, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this change is a BC break |
||
array $command, | ||
string $workingDirectory, | ||
?array $extraEnvironmentVariables, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
|
||
int $index, | ||
InputStream $inputStream, | ||
array $phpExecutable, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this change is a BC break |
||
array $command, | ||
string $workingDirectory, | ||
?array $environmentVariables, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,7 +59,8 @@ | |
* STDOUT or STDERR. | ||
* @param callable(): void $tick | ||
*/ | ||
public function __construct( | ||
Check failure on line 62 in src/Process/SymfonyProcessLauncher.php
|
||
private readonly array $phpExecutable, | ||
private readonly array $command, | ||
private readonly string $workingDirectory, | ||
private readonly ?array $environmentVariables, | ||
|
@@ -135,6 +136,7 @@ | |
$process = $this->processFactory->startProcess( | ||
$index, | ||
$inputStream, | ||
$this->phpExecutable, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this change is a BC break |
||
$this->command, | ||
$this->workingDirectory, | ||
$this->environmentVariables, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,8 @@ | |
* STDOUT or STDERR. | ||
* @param callable(): void $tick | ||
*/ | ||
public function create( | ||
Check failure on line 34 in src/Process/SymfonyProcessLauncherFactory.php
|
||
array $phpExecutable, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this change is a BC break |
||
array $command, | ||
string $workingDirectory, | ||
?array $extraEnvironmentVariables, | ||
|
@@ -42,6 +43,7 @@ | |
callable $tick | ||
): ProcessLauncher { | ||
return new SymfonyProcessLauncher( | ||
$phpExecutable, | ||
$command, | ||
$workingDirectory, | ||
$extraEnvironmentVariables, | ||
|
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'; | ||
} | ||
} |
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); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.