/** * Test that the spawner spawns the process in background. * * @return void */ public function testRunBackground() { $config = new TensideJsonConfig(new JsonArray()); $config->setForceToBackground(true); $process = PhpProcessSpawner::create($config, $this->getTempDir())->spawn(['--version']); $cli = $process->getCommandLine(); if ('\\' === DIRECTORY_SEPARATOR) { $this->assertEquals('start /B ' . escapeshellarg('php') . ' ' . escapeshellarg('--version'), $cli); } else { $this->assertEquals(escapeshellarg('php') . ' ' . escapeshellarg('--version') . ' &', $cli); } }
/** * Test that the spawner really spawns the process correctly. * * @return void */ public function testRun() { $config = new TensideJsonConfig(new JsonArray()); $config->setPhpCliArguments(['-dmemory_limit=1G']); $config->setPhpCliEnvironment(['TESTVAR=TESTVALUE']); $process = PhpProcessSpawner::create($config, $this->getTempDir())->spawn(['-r', 'echo getenv(\'TESTVAR\') . ini_get(\'memory_limit\');']); $process->run(); $cli = $process->getCommandLine(); $this->assertEquals('php \'-dmemory_limit=1G\' ' . '\'-r\' \'echo getenv(\'\\\'\'TESTVAR\'\\\'\') . ini_get(\'\\\'\'memory_limit\'\\\'\');\'', $cli); $this->assertEquals(0, $process->getExitCode()); $this->assertEquals('TESTVALUE1G', $process->getOutput()); $this->assertEquals('', $process->getErrorOutput()); }
/** * Spawn a detached process for a task. * * @param Task $task The task to spawn a process for. * * @return void * * @throws \RuntimeException When the task could not be started. */ private function spawn(Task $task) { $config = $this->getTensideConfig(); $home = $this->getTensideHome(); $commandline = PhpProcessSpawner::create($config, $home)->spawn([$this->get('tenside.cli_script')->cliExecutable(), 'tenside:runtask', $task->getId(), '-v', '--no-interaction']); $this->get('logger')->debug('SPAWN CLI: ' . $commandline->getCommandLine()); $commandline->start(); if (!$commandline->isRunning()) { // We might end up here when the process has been forked. // If exit code is neither 0 nor null, we have a problem here. if ($exitCode = $commandline->getExitCode()) { /** @var LoggerInterface $logger */ $logger = $this->get('logger'); $logger->error('Failed to execute "' . $commandline->getCommandLine() . '"'); $logger->error('Exit code: ' . $commandline->getExitCode()); $logger->error('Output: ' . $commandline->getOutput()); $logger->error('Error output: ' . $commandline->getErrorOutput()); throw new \RuntimeException(sprintf('Spawning process task %s resulted in exit code %s', $task->getId(), $exitCode)); } } }