Skip to content

Commit

Permalink
set memory limit for sub processes
Browse files Browse the repository at this point in the history
  • Loading branch information
m-vo committed Sep 12, 2021
1 parent 50079d9 commit 74bafa4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 25 deletions.
13 changes: 12 additions & 1 deletion manager-bundle/src/Command/ContaoSetupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class ContaoSetupCommand extends Command
*/
private $createProcessHandler;

/**
* @var string
*/
private $memoryLimit;

/**
* @var string|false
*/
Expand All @@ -49,7 +54,7 @@ class ContaoSetupCommand extends Command
/**
* @param (\Closure(array<string>):Process)|null $createProcessHandler
*/
public function __construct(string $projectDir, string $webDir, \Closure $createProcessHandler = null)
public function __construct(string $projectDir, string $webDir, \Closure $createProcessHandler = null, string $memoryLimit = null)
{
$this->webDir = Path::makeRelative($webDir, $projectDir);
$this->phpPath = (new PhpExecutableFinder())->find();
Expand All @@ -59,6 +64,8 @@ public function __construct(string $projectDir, string $webDir, \Closure $create
return new Process($command);
};

$this->memoryLimit = $memoryLimit ?? ini_get('memory_limit');

parent::__construct();
}

Expand All @@ -78,6 +85,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$php = [$this->phpPath];

if (!empty($this->memoryLimit)) {
$php[] = "-dmemory_limit=$this->memoryLimit";
}

if (OutputInterface::VERBOSITY_DEBUG === $output->getVerbosity()) {
$php[] = '-ddisplay_errors=-1';
$php[] = '-ddisplay_startup_errors=-1';
Expand Down
72 changes: 48 additions & 24 deletions manager-bundle/tests/Command/ContaoSetupCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testExecutesCommands(array $options, array $flags, array $phpFla

$createProcessHandler = $this->getCreateProcessHandler($processes, $commandArguments, $invocationCount);

$command = new ContaoSetupCommand('project/dir', 'project/dir/public', $createProcessHandler);
$command = new ContaoSetupCommand('project/dir', 'project/dir/public', $createProcessHandler, '');

(new CommandTester($command))->execute([], $options);

Expand Down Expand Up @@ -143,6 +143,25 @@ public function testDelegatesOutputOfSubProcesses(): void
);
}

public function testSetsMemoryLimitForSubProcesses(): void
{
$createProcessHandler = function (array $command): Process {
$this->assertContains('-dmemory_limit=<memory-limit>', $command);

return $this->getProcessMock();
};

$command = new ContaoSetupCommand(
'project/dir',
'project/dir/public',
$createProcessHandler,
'<memory-limit>'
);

$commandTester = new CommandTester($command);
$commandTester->execute([]);
}

/**
* @return (\Closure(array<string>):Process)
*/
Expand All @@ -164,31 +183,36 @@ private function getProcessMocks(bool $successful = true): array
$processes = [];

for ($i = 1; $i <= 7; ++$i) {
$process = $this->createMock(Process::class);
$process
->method('isSuccessful')
->willReturn($successful)
;

$process
->method('run')
->with($this->callback(
static function ($callable) use ($i) {
$callable('', "[output $i]");

return true;
}
))
;

$process
->method('getErrorOutput')
->willReturn('<error>')
;

$processes[] = $process;
$processes[] = $this->getProcessMock($successful, "[output $i]");
}

return $processes;
}

private function getProcessMock(bool $successful = true, string $output = ''): Process
{
$process = $this->createMock(Process::class);
$process
->method('isSuccessful')
->willReturn($successful)
;

$process
->method('run')
->with($this->callback(
static function ($callable) use ($output) {
$callable('', $output);

return true;
}
))
;

$process
->method('getErrorOutput')
->willReturn('<error>')
;

return $process;
}
}

0 comments on commit 74bafa4

Please sign in to comment.