1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-02 17:52:14 +01:00
parallel/lib/Process/ChannelledProcess.php

190 lines
5.4 KiB
PHP
Raw Normal View History

2016-12-30 02:16:04 +01:00
<?php
2015-08-27 16:10:08 +02:00
2016-08-23 23:47:40 +02:00
namespace Amp\Parallel\Process;
2015-08-27 16:10:08 +02:00
use Amp\ByteStream;
2017-05-18 09:51:31 +02:00
use Amp\Coroutine;
use Amp\Parallel\Context;
2017-05-18 09:51:31 +02:00
use Amp\Parallel\ContextException;
use Amp\Parallel\StatusError;
use Amp\Parallel\Sync\ChannelException;
2017-06-16 06:46:15 +02:00
use Amp\Parallel\Sync\ChannelledStream;
use Amp\Parallel\Sync\ExitResult;
2017-05-18 09:51:31 +02:00
use Amp\Parallel\SynchronizationError;
2017-01-16 19:56:49 +01:00
use Amp\Process\Process;
2017-05-18 09:51:31 +02:00
use Amp\Promise;
use function Amp\asyncCall;
2017-05-18 09:51:31 +02:00
use function Amp\call;
2016-08-18 18:04:48 +02:00
class ChannelledProcess implements Context {
2017-01-16 19:56:49 +01:00
/** @var \Amp\Process\Process */
2015-08-27 16:10:08 +02:00
private $process;
2016-08-26 17:10:03 +02:00
/** @var \Amp\Parallel\Sync\Channel */
2015-08-27 16:10:08 +02:00
private $channel;
/**
* @param string|array $script Path to PHP script or array with first element as path and following elements options
* to the PHP script (e.g.: ['bin/worker', '-eOptionValue', '-nOptionValue'].
* @param string $cwd Working directory.
2015-08-27 16:10:08 +02:00
* @param mixed[] $env Array of environment variables.
*/
public function __construct($script, string $cwd = "", array $env = []) {
2017-06-21 17:30:26 +02:00
$options = [
"html_errors" => "0",
"display_errors" => "0",
"log_errors" => "1",
];
if (\is_array($script)) {
$script = \implode(" ", \array_map("escapeshellarg", $script));
} else {
$script = \escapeshellarg($script);
}
$options = (\PHP_SAPI === "phpdbg" ? " -b -qrr " : " ") . $this->formatOptions($options);
$separator = \PHP_SAPI === "phpdbg" ? " -- " : " ";
$command = \escapeshellarg(\PHP_BINARY) . $options . $separator . $script;
2017-06-21 17:30:26 +02:00
$processOptions = [];
if (\strncasecmp(\PHP_OS, "WIN", 3) === 0) {
$processOptions = ["bypass_shell" => true];
}
$this->process = new Process($command, $cwd, $env, $processOptions);
2015-08-27 16:10:08 +02:00
}
2017-06-21 17:30:26 +02:00
private function formatOptions(array $options) {
$result = [];
foreach ($options as $option => $value) {
$result[] = \sprintf("-d %s=%s", $option, $value);
}
return \implode(" ", $result);
}
/**
* Resets process values.
*/
2016-08-18 18:04:48 +02:00
public function __clone() {
$this->process = clone $this->process;
$this->channel = null;
}
2015-08-27 16:10:08 +02:00
/**
* {@inheritdoc}
*/
2016-08-18 18:04:48 +02:00
public function start() {
2017-03-17 04:26:05 +01:00
$this->process->start();
2017-06-21 17:30:26 +02:00
$this->channel = new ChannelledStream($this->process->getStdout(), $this->process->getStdin());
/** @var ByteStream\ResourceInputStream $childStderr */
$childStderr = $this->process->getStderr();
$childStderr->unreference();
asyncCall(function () use ($childStderr) {
$stderr = new ByteStream\ResourceOutputStream(\STDERR);
yield ByteStream\pipe($childStderr, $stderr);
});
2015-08-27 16:10:08 +02:00
}
/**
* {@inheritdoc}
*/
2016-08-18 18:04:48 +02:00
public function isRunning(): bool {
2015-08-27 16:10:08 +02:00
return $this->process->isRunning();
}
/**
* {@inheritdoc}
*/
2016-11-15 00:43:44 +01:00
public function receive(): Promise {
2016-08-23 01:25:19 +02:00
if ($this->channel === null) {
throw new StatusError("The process has not been started");
2015-08-27 16:10:08 +02:00
}
return new Coroutine($this->doReceive());
}
private function doReceive() {
try {
$data = yield $this->channel->receive();
2017-07-21 06:58:13 +02:00
} catch (ChannelException $e) {
throw new ContextException("The context stopped responding, potentially due to a fatal error or calling exit", 0, $e);
}
2017-03-09 23:48:34 +01:00
if ($data instanceof ExitResult) {
$data = $data->getResult();
throw new SynchronizationError(\sprintf(
'Process unexpectedly exited with result of type: %s',
\is_object($data) ? \get_class($data) : \gettype($data)
));
}
return $data;
2015-08-27 16:10:08 +02:00
}
/**
* {@inheritdoc}
*/
2016-11-15 00:43:44 +01:00
public function send($data): Promise {
2016-08-23 01:25:19 +02:00
if ($this->channel === null) {
throw new StatusError("The process has not been started");
}
2017-01-17 06:24:59 +01:00
if ($data instanceof ExitResult) {
throw new \Error("Cannot send exit result objects");
2015-08-27 16:10:08 +02:00
}
return call(function () use ($data) {
try {
yield $this->channel->send($data);
} catch (ChannelException $e) {
2017-07-21 06:58:13 +02:00
throw new ContextException("The context went away, potentially due to a fatal error or calling exit", 0, $e);
}
});
2015-08-27 16:10:08 +02:00
}
/**
* {@inheritdoc}
*/
2016-11-15 00:43:44 +01:00
public function join(): Promise {
2017-01-16 19:56:49 +01:00
if ($this->channel === null) {
throw new StatusError("The process has not been started");
}
return new Coroutine($this->doJoin());
}
private function doJoin(): \Generator {
try {
$data = yield $this->channel->receive();
2017-01-17 06:24:59 +01:00
if (!$data instanceof ExitResult) {
throw new SynchronizationError("Did not receive an exit result from process");
2017-01-16 19:56:49 +01:00
}
2017-07-21 06:58:13 +02:00
} catch (ChannelException $e) {
$this->kill();
2017-07-21 06:58:13 +02:00
throw new ContextException("The context stopped responding, potentially due to a fatal error or calling exit", 0, $e);
2017-01-16 19:56:49 +01:00
} catch (\Throwable $exception) {
$this->kill();
throw $exception;
}
2017-03-17 04:26:05 +01:00
$code = yield $this->process->join();
2017-01-16 19:56:49 +01:00
if ($code !== 0) {
throw new ContextException(\sprintf("Process exited with code %d", $code));
}
return $data->getResult();
2015-08-27 16:10:08 +02:00
}
/**
* {@inheritdoc}
*/
2016-08-18 18:04:48 +02:00
public function kill() {
2015-08-27 16:10:08 +02:00
$this->process->kill();
}
}