/** * {@inheritdoc} */ public function wait(Process $process) { $start = microtime(true); $end = $start + $this->timeout / 1000; while (!$process->isTerminated() && microtime(true) < $end) { usleep(self::TICK * 1000); } if ($process->isRunning()) { $callback = $this->callback; $callback(); } $process->wait(); }
/** * @return bool */ private function handleProcess(Process $process) { if ($process->isStarted()) { if ($process->isTerminated()) { $this->running--; return true; } return false; } // Only start a new process if we haven't reached the limit yet. if ($this->running < $this->config->getProcessAsyncLimit()) { $process->start(); $this->running++; } return false; }
public function testLockingFunctionality() { if (!class_exists('AppKernel')) { $this->markTestSkipped("This test does only work if a full application is installed (including AppKernel class"); } $commandName = $this->getCommand()->getName(); $reflector = new \ReflectionClass(\AppKernel::class); $appDirectory = dirname($reflector->getFileName()); // start commands in a separate processes $process1 = new Process("php {$appDirectory}/console {$commandName} --env=test"); $process2 = new Process("php {$appDirectory}/console {$commandName} --env=test"); $process1->start(); $process2->start(); // wait until both processes have terminated while (!$process1->isTerminated() || !$process2->isTerminated()) { usleep(10); } $this->assertContains('The command is already running in another process.', $process2->getOutput() . $process1->getOutput()); }
/** * @param string $command * @param OutputInterface $output * @param callable|null $callback A valid PHP callback */ protected function basicProcess($command, OutputInterface $output, $callback = null) { $process = new Process($command); $process->setTimeout(600); $output->writeln($this->helper->formatSection('Executing', $process->getCommandLine(), 'comment')); $process->start(); $process->wait(function ($type, $buffer) use($output) { if (Process::ERR == $type) { $output->write($this->helper->formatSection('Error', $buffer, 'error')); } else { $output->write($this->helper->formatSection('Progress', $buffer, 'comment')); } }); if ($process->isTerminated()) { $output->writeln($this->helper->formatSection('Finishing', $process->getCommandLine(), 'comment')); if (null !== $callback) { $callback(); } } }
/** * Check if the process has terminated. * * @return bool */ public function isDoneRunning() { return $this->process->isTerminated(); }
/** * @param $address * @param $environment */ protected function startServer() { $publicDir = $this->getApplication()->getWorkingPath() . DS . 'public'; $shellCommand = $this->getBaseCommand(); $process = new Process($shellCommand, $publicDir); if ($this->getInput()->getOption('background')) { $process->disableOutput(); $process->start(); $processId = $this->getProcessId(); $this->getApplication()->getConfig()->setOption('server', ['pid' => $processId, 'address' => $address = 'http://' . $this->getAddress()]); $this->getOutput()->writeln($this->info('Server has been started at ' . $address)); } else { while ($process instanceof Process) { if (!$process->isStarted()) { $process->start(); continue; } echo $process->getIncrementalOutput(); echo $process->getIncrementalErrorOutput(); if (!$process->isRunning() || $process->isTerminated()) { $process = false; $this->getOutput()->writeln(""); $this->getOutput()->writeln($this->info('Server has been stopped.')); } sleep(1); } } }