In comparison with the getOutput method which always return the whole
output, this one returns the new output since the last call.
public getIncrementalOutput ( ) : string | ||
return | string | The process output since the last call |
/** * Looks into and processes the child's output * * @param string $childName * @return void */ public function processOutput($childName) { $output = $this->outputBuffer . $this->process->getIncrementalOutput(); $errorOutput = $this->errorOutputBuffer . $this->process->getIncrementalErrorOutput(); $outputLines = explode("\n", $output); $errorOutputLines = explode("\n", $errorOutput); if (count($outputLines) > 0) { $lastItem = array_pop($outputLines); $this->outputBuffer = $lastItem; $this->bufferLength += implode("\n", $outputLines); foreach ($outputLines as $line) { if (strstr($line, "[[CHILD::BUSY]]")) { $this->parent->verboseOutput('<info>' . $childName . ' BUSY</info>'); $this->status = ChildProcessContainer::STATUS_BUSY; } elseif (strstr($line, "[[CHILD::READY]]")) { $this->parent->verboseOutput('<info>' . $childName . ' READY</info>'); if ($this->status != ChildProcessContainer::STATUS_BUSY_BUT_SLEEPY) { $this->status = ChildProcessContainer::STATUS_READY; } else { $this->status = ChildProcessContainer::STATUS_SLEEPY; } } elseif (strlen($line) > 0) { $this->parent->verboseOutput('<info>OUTPUT ' . $childName . ':</info>' . $line); } } } if (count($errorOutputLines) > 0) { $lastItemError = array_pop($errorOutputLines); $this->errorOutputBuffer = $lastItemError; $knownErrorOutput = implode("\n", $errorOutputLines); $this->bufferLength += strlen($knownErrorOutput); } }
private function killExistingSocketServer() { // If socket already started $command = 'lsof -n -i4TCP:' . $this->socketPort . ' | grep LISTEN'; $lsOfProcess = new Process($command); $lsOfProcess->run(); $lsOfProcessOutput = $lsOfProcess->getIncrementalOutput(); if (strlen($lsOfProcessOutput) > 0) { // Kill process $lsOfProcessOutput = preg_replace('/\\s+/', ' ', $lsOfProcessOutput); $lsOfProcessOutput = explode(' ', $lsOfProcessOutput); $pid = $lsOfProcessOutput[1]; $killProcess = new Process("kill {$pid}"); $killProcess->mustRun(); } }
/** * Fire and forget a command. It will be executed asynchronously, but you can get its output via the $callback. * * @param string $cmd Command to be fired. * @param callable $callback [optional] Callback function that will be called periodically during command's execution * and will take two arguments: 1st is a string buffer output and 2nd is bool error. * You can return (bool) false from the callback to stop the running command. */ public function fire($cmd, $callback = null) { $process = new SymfonyProcess($cmd); $process->start(); // if callback is defined then call it periodically if (is_callable($callback)) { while ($process->isRunning()) { // call the callback $continue = call_user_func_array($callback, array($process->getIncrementalOutput())); // if callback returned false then stop the process if ($continue === false) { $process->stop(3, SIGINT); } } } }
private function startProcess($port, $respond, $callback) { $env = array('PORT' => $port, 'RESPOND' => $respond); $process = new Process('php tests/bin/server.php', null, $env); $process->start(); // give it time to start while ($process->getIncrementalOutput() !== 'started') { usleep(100); } try { $callback(); } catch (Exception $e) { $process->signal(SIGKILL); $process->stop(); throw $e; } }
/** * Executes post install SH script * * @param \Symfony\Component\Console\Input\InputInterface $input * @param OutputInterface $output * * @internal param \Symfony\Component\Console\Input\InputInterface $intput * @return int|null|void */ protected function execute(InputInterface $input, OutputInterface $output) { $command = $input->getOption("post"); $output->writeln(""); $output->writeln("========================"); $output->writeln('Executing post ' . $command . ' script.'); $output->writeln("========================"); // TODO: openssl req -new -x509 -sha256 -days 365 -nodes -out /tmp/server.crt -keyout /tmp/server.key $process = new Process("/bin/bash ./src/FIT/NetopeerBundle/bin/netconfwebgui-postinstall.sh"); $process->run(); while ($process->isRunning()) { $process->getIncrementalOutput(); $process->getIncrementalErrorOutput(); } if (!$process->isSuccessful()) { $output->writeln("Error in post " . $command . " script occured."); $output->writeln($process->getErrorOutput()); } else { $output->writeln($process->getOutput()); } $output->writeln("========================"); $output->writeln("End of post " . $command . " script"); $output->writeln("========================"); }
public function testGetIncrementalOutput() { $p = new Process(sprintf('php -r %s', escapeshellarg('$n=0;while ($n<3) { echo \' foo \'; usleep(50000); $n++; }'))); $p->start(); while ($p->isRunning()) { $this->assertLessThanOrEqual(1, preg_match_all('/foo/', $p->getIncrementalOutput(), $matches)); usleep(20000); } }
/** * Execute * * @return Funstaff\Tika\Wrapper */ public function execute() { $base = $this->generateCommand(); foreach ($this->document as $name => $doc) { if ($doc->getPassword()) { $command = sprintf('%s --password=%s', $base, $doc->getPassword()); } else { $command = $base; } $command = sprintf('%s %s', $command, escapeshellarg($doc->getPath())); if ($this->logger) { $this->logger->addInfo(sprintf('Tika command: "%s"', $command)); } $process = new Process($command); $process->run(); if (!$process->isSuccessful()) { throw new \InvalidArgumentException($process->getErrorOutput()); } $content = $process->getIncrementalOutput(); $doc->setRawContent($content); if ($this->config->getMetadataOnly()) { $this->loadMetadata($doc, $content); } else { if (in_array($this->config->getOutputFormat(), array('xml', 'html'))) { $this->loadDocument($doc, $content); } else { $doc->setContent($content); } } } return $this; }
/** * @param string $command * @return string * * @throws \RuntimeException */ protected function runProcess($command) { $process = new Process($command); $process->run(); if (!$process->isSuccessful()) { throw new \InvalidArgumentException($process->getErrorOutput()); } return $process->getIncrementalOutput(); }
/** * @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); } } }
/** * Execute behat command for featurename and return exit status. * * @param string $featurename name of the feature * @param string $featurepath path of feature file * @return int status code. */ protected function execute_behat_generator($featurename, $featurepath) { $cmd = "vendor/bin/behat --config " . util::get_tool_dir() . DIRECTORY_SEPARATOR . 'behat.yml ' . $featurepath; $process = new symfonyprocess($cmd); $process->setWorkingDirectory(__DIR__ . "/../../moodle"); $process->setTimeout(null); $process->start(); if ($process->getStatus() !== 'started') { echo "Error starting process: {$featurename}"; $process->signal(SIGKILL); exit(1); } while ($process->isRunning()) { $output = $process->getIncrementalOutput(); // Don't show start data everytime. $output = preg_replace('/[a-z0-9.\\(\\)].*/im', '', $output); $op = trim($output); if (!empty($op)) { echo $output; } } return $process->getExitCode(); }
/** * @param Process $process * @return callable */ public function updateRelease(Process $process) { $out = $process->getIncrementalOutput() . $process->getIncrementalErrorOutput(); $this->release->update(['raw_log' => $process->getOutput() . PHP_EOL . $process->getErrorOutput()]); if (!empty($out)) { $this->release->logger()->info($out); } }
/** * Execute behat command for featurename and return exit status. * * @return int status code. */ protected function execute_behat_generator() { $cmd = "vendor/bin/behat --config " . util::get_tool_dir() . DIRECTORY_SEPARATOR . 'behat.yml '; $process = new Process($cmd); $process->setWorkingDirectory(__DIR__ . "/../../../../../"); $process->setTimeout(null); $process->start(); if ($process->getStatus() !== 'started') { echo "Error starting process"; $process->signal(SIGKILL); exit(1); } while ($process->isRunning()) { $output = $process->getIncrementalOutput(); $op = trim($output); if (!empty($op)) { echo $output; } } if ($process->getExitCode() !== 0) { echo $process->getErrorOutput(); } return $process->getExitCode(); }
/** * @param $writeFolderPath * @param $readFilePath * @return bool */ public function processContractDocument($writeFolderPath, $readFilePath) { set_time_limit(0); $commandPath = config('nrgi.pdf_process_path'); $command = sprintf('python %s/run.py -i %s -o %s', $commandPath, $readFilePath, $writeFolderPath); $this->logger->info("processing command", ['command' => $command]); $process = new Process($command); $process->setTimeout(360 * 10); $process->start(); while ($process->isRunning()) { echo $process->getIncrementalOutput(); } if (!$process->isSuccessful()) { //todo remove folder $this->logger->error("error while executing command.{$process->getErrorOutput()}", ['command' => $command]); throw new \RuntimeException($process->getErrorOutput()); } return true; }
/** * search job in jobs * * @param string $job * @return mixed */ public function jobExists($job = '') { $process = new Process('crontab -l'); $process->run(); $output = $process->getIncrementalOutput(); $jobs = array_filter(explode(PHP_EOL, $output), function ($line) { return '' != trim($line); }); return is_array($jobs) ? array_search($job, $jobs) : false; }
/** * Decorate and return Process output * @param Process $process * @return string */ protected function getProcessOutput(Process $process) { $output = ''; // Add standard process output if ($processOutput = $process->getIncrementalOutput()) { $processOutputLines = explode("\n", $processOutput); // color output lines containing "[WARN]" foreach ($processOutputLines as &$processOutputLine) { if (strpos($processOutputLine, '[WARN]') !== false) { $processOutputLine = '<fg=black;bg=yellow>' . $processOutputLine . '</fg=black;bg=yellow>'; } elseif (strpos($processOutputLine, '[DEBUG]') !== false) { $processOutputLine = '<comment>' . $processOutputLine . '</comment>'; } } $output .= implode("\n", $processOutputLines); } // Add error output if ($errorOutput = $process->getIncrementalErrorOutput()) { $output .= '<error>' . rtrim($errorOutput, PHP_EOL) . '</error>' . "\n"; } return $output; }