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
|
|
|
|
2017-01-16 19:56:49 +01:00
|
|
|
use Amp\Coroutine;
|
2017-03-09 23:15:30 +01:00
|
|
|
use Amp\Parallel\{
|
|
|
|
ChannelException,
|
|
|
|
ContextException,
|
|
|
|
Process as ProcessContext,
|
|
|
|
StatusError,
|
|
|
|
Strand,
|
|
|
|
SynchronizationError
|
|
|
|
};
|
2017-01-17 06:24:59 +01:00
|
|
|
use Amp\Parallel\Sync\{ ChannelledSocket, Internal\ExitResult };
|
2017-01-16 19:56:49 +01:00
|
|
|
use Amp\Process\Process;
|
2017-01-09 18:11:25 +01:00
|
|
|
use AsyncInterop\Promise;
|
2016-08-18 18:04:48 +02:00
|
|
|
|
|
|
|
class ChannelledProcess implements ProcessContext, Strand {
|
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;
|
|
|
|
|
2017-01-16 19:56:49 +01:00
|
|
|
/** @var \AsyncInterop\Promise */
|
|
|
|
private $promise;
|
|
|
|
|
2015-08-27 16:10:08 +02:00
|
|
|
/**
|
|
|
|
* @param string $path Path to PHP script.
|
|
|
|
* @param string $cwd Working directory.
|
|
|
|
* @param mixed[] $env Array of environment variables.
|
|
|
|
*/
|
2016-08-26 18:14:42 +02:00
|
|
|
public function __construct(string $path, string $cwd = "", array $env = []) {
|
2017-01-16 19:56:49 +01:00
|
|
|
$command = \PHP_BINARY . " " . \escapeshellarg($path);
|
2015-08-27 16:10:08 +02:00
|
|
|
$this->process = new Process($command, $cwd, $env);
|
|
|
|
}
|
|
|
|
|
2015-08-27 20:06:39 +02:00
|
|
|
/**
|
|
|
|
* Resets process values.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function __clone() {
|
2015-08-27 20:06:39 +02:00
|
|
|
$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-01-16 19:56:49 +01:00
|
|
|
$this->promise = $this->process->execute();
|
2016-08-31 01:27:14 +02:00
|
|
|
$this->channel = new ChannelledSocket($this->process->getStdOut(), $this->process->getStdIn(), false);
|
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) {
|
2016-08-26 18:14:42 +02:00
|
|
|
throw new StatusError("The process has not been started");
|
2015-08-27 16:10:08 +02:00
|
|
|
}
|
|
|
|
|
2017-03-09 23:15:30 +01:00
|
|
|
return new Coroutine($this->doReceive());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function doReceive() {
|
|
|
|
try {
|
|
|
|
$data = yield $this->channel->receive();
|
|
|
|
|
2017-01-17 06:24:59 +01:00
|
|
|
if ($data instanceof ExitResult) {
|
2016-08-18 18:04:48 +02:00
|
|
|
$data = $data->getResult();
|
|
|
|
throw new SynchronizationError(\sprintf(
|
2017-03-09 23:15:30 +01:00
|
|
|
'Process unexpectedly exited with result of type: %s',
|
2016-08-18 18:04:48 +02:00
|
|
|
\is_object($data) ? \get_class($data) : \gettype($data)
|
|
|
|
));
|
|
|
|
}
|
2017-03-09 23:15:30 +01:00
|
|
|
} catch (ChannelException $exception) {
|
|
|
|
throw new ContextException(
|
|
|
|
"The context stopped responding, potentially due to a fatal error or calling exit", 0, $exception
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2016-08-26 18:14:42 +02:00
|
|
|
throw new StatusError("The process has not been started");
|
2015-08-27 20:06:39 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-09 23:15:30 +01:00
|
|
|
return \Amp\capture($this->channel->send($data), ChannelException::class, function (ChannelException $exception) {
|
|
|
|
throw new ContextException(
|
|
|
|
"The context went away, potentially due to a fatal error or calling exit", 0, $exception
|
|
|
|
);
|
|
|
|
});
|
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-03-09 23:15:30 +01:00
|
|
|
} catch (ChannelException $exception) {
|
|
|
|
$this->kill();
|
|
|
|
throw new ContextException(
|
|
|
|
"The context stopped responding, potentially due to a fatal error or calling exit", 0, $exception
|
|
|
|
);
|
2017-01-16 19:56:49 +01:00
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
$this->kill();
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
$code = yield $this->promise;
|
|
|
|
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();
|
|
|
|
}
|
2015-10-20 07:06:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function getPid(): int {
|
2015-10-20 07:06:43 +02:00
|
|
|
return $this->process->getPid();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function signal(int $signo) {
|
2015-10-20 07:06:43 +02:00
|
|
|
$this->process->signal($signo);
|
|
|
|
}
|
2015-08-27 16:10:08 +02:00
|
|
|
}
|