/** * Starts background proccess * * @param \Closure $callback Function to execute if executed application provides output(either STDERR or STDOUT) ($type, $buffer) * * @return int The exit status code */ public function start($callback = null) { $statusCode = $this->symfonyProcess->start($callback); // give the new process a bit of breathing room sleep(2); return $statusCode; }
function initServer() { $process = new PhpProcess(file_get_contents(__DIR__ . '/worker.php'), __DIR__); $process->start(); sleep(5); return $process; }
protected function execute(InputInterface $input, OutputInterface $output) { /** @var FormatterHelper $formatter */ $formatter = $this->getHelper('formatter'); // $process = new Process('wkhtmltopdf http://google.com/ test.pdf'); // $process->run(); // $process->stop(3, SIGINT); // $process->mustRun(function ($type, $message) use ($output, $formatter) { // if ($type === Process::ERR) { // $output->writeln($formatter->formatBlock($message, 'error')); // } else { // $output->writeln($formatter->formatBlock($message, 'comment')); // } // }); $process = new PhpProcess(<<<EOF <?php sleep(5); echo "OK "; ?> EOF ); $process->start(); $process->stop(2, SIGINT); if (!$process->isSuccessful()) { $output->writeln($formatter->formatBlock($process->getErrorOutput(), 'error', true)); return; } $output->writeln($formatter->formatSection('success', $process->getOutput())); }
public function testNonBlockingWorks() { $expected = 'hello world!'; $process = new PhpProcess(<<<PHP <?php echo '{$expected}'; PHP ); $process->start(); $process->wait(); $this->assertEquals($expected, $process->getOutput()); }
/** * {@inheritdoc} */ public function run(JobReportInterface $report) { try { $this->process->start(); $this->savePid($this->process->getPid(), $report); while ($this->process->isRunning()) { // waiting for process to finish } if ($this->process->isSuccessful()) { $report->setOutput(trim($this->process->getOutput())); return JobState::STATE_FINISHED; } $report->setErrorOutput(trim($this->process->getErrorOutput())); } catch (LogicException $e) { $report->setErrorOutput($e->getMessage()); } catch (RuntimeException $e) { $report->setErrorOutput($e->getMessage()); } return JobState::STATE_FAILED; }
/** * Starts the web server. * * @param callable $callback An output function to call whenever there is * a log entry made to the server. */ public function start($callback = null) { // Find the PHP executable. if (false === ($php = $this->executableFinder->find())) { throw new \RuntimeException('Unable to find the PHP executable.'); } $options = ' -S ' . $this->addr . ':' . $this->port; if (isset($this->documentRoot)) { $options .= ' -t ' . $this->documentRoot; } $this->setCommandLine($php . $options); parent::start($callback); }
public function testCommandLine() { $process = new PhpProcess(<<<'PHP' <?php echo 'foobar'; PHP ); $commandLine = $process->getCommandLine(); $f = new PhpExecutableFinder(); $this->assertContains($f->find(), $commandLine, '::getCommandLine() returns the command line of PHP before start'); $process->start(); $this->assertContains($commandLine, $process->getCommandLine(), '::getCommandLine() returns the command line of PHP after start'); $process->wait(); $this->assertContains($commandLine, $process->getCommandLine(), '::getCommandLine() returns the command line of PHP after wait'); }
public function testCommandLine() { if ('phpdbg' === PHP_SAPI) { $this->markTestSkipped('phpdbg SAPI is not supported by this test.'); } $process = new PhpProcess(<<<PHP <?php echo 'foobar'; PHP ); $f = new PhpExecutableFinder(); $commandLine = $f->find(); $this->assertSame($commandLine, $process->getCommandLine(), '::getCommandLine() returns the command line of PHP before start'); $process->start(); $this->assertSame($commandLine, $process->getCommandLine(), '::getCommandLine() returns the command line of PHP after start'); $process->wait(); $this->assertSame($commandLine, $process->getCommandLine(), '::getCommandLine() returns the command line of PHP after wait'); }
protected function createWorker($id) { $files = get_included_files(); if ($_SERVER['PHP_SELF'] != '-') { array_shift($files); } $boostrap = "<?php \n foreach (" . var_export($files, true) . " as \$file) {\n require_once \$file;\n }\n\n define('__WORKER__', " . var_export($id, true) . ");\n\n \$config = crodas\\Worker\\Config::import(" . $this->config->export() . ");\n \$config['worker_id'] = __WORKER__;\n\n \$server = new crodas\\Worker\\Server(\$config);\n \$server->worker();\n "; $this->log(null, "Starting process {$id}"); $process = new PhpProcess($boostrap); $process->start(); $process->id = $id; $process->time = time(); $process->status = empty($args) ? 'idle' : 'busy'; $process->jobs = 0; $process->failed = 0; return $process; }
/** * Initiates new actor in a new PHP process * @param integer $id An unique id of an actor, should be free tcp-port in current implementation * @param callable $handler * @return PhpProcess */ public static function createAndRun($id, callable $handler) { $serializedHandler = base64_encode((new Serializer())->serialize($handler)); $autoloadPath = Utils::getAutoloadPath(); $process = new PhpProcess(<<<EOF <?php require '{$autoloadPath}'; \\Phactor\\Phactor\\Actor::initializeChild({$id}, '{$serializedHandler}'); ?> EOF ); if (null === $process->getCommandLine()) { $process->setPhpBinary(PHP_BINARY); } // workaround for portable windows php $process->start(); return $process; }
/** * @medium * @dataProvider backends */ public function testCompressEventsKate($backend) { // CREATE web.scssdx1493.new // MODIFY web.scssdx1493.new // MOVED_FROM web.scssdx1493.new // MOVED_TO web.scss if (!$backend->isAvailable()) { $this->markTestSkipped(); } $f = __DIR__ . '/test/'; sleep(1); $php = "<?php sleep(2);\n file_put_contents('{$f}foo.txtdx1493.new', 'x');\n rename('{$f}foo.txtdx1493.new', '{$f}foo.txt');\n\n file_put_contents('{$f}stop.txt', 'x');\n "; $process = new PhpProcess($php); $process->start(); $gotEvents = array(); $backend->setPath(__DIR__ . '/test'); $backend->addListener(ModifyEvent::NAME, function ($e) use(&$gotEvents, $backend) { $gotEvents[] = 'modify'; }); $backend->addListener(CreateEvent::NAME, function ($e) use(&$gotEvents, $backend) { if ($e->filename == __DIR__ . '/test/stop.txt') { $backend->stop(); } else { $gotEvents[] = 'create'; } }); $backend->addListener(DeleteEvent::NAME, function ($e) use(&$gotEvents, $backend) { $gotEvents[] = 'delete'; }); $backend->addListener(MoveEvent::NAME, function ($e) use(&$gotEvents, $backend) { $gotEvents[] = 'move'; }); $backend->start(); $process->wait(); $this->assertTrue($process->isSuccessful()); $this->assertEquals($gotEvents, array('modify')); }