2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2015-08-27 16:10:08 +02:00
|
|
|
|
2017-11-29 22:01:32 +01:00
|
|
|
namespace Amp\Parallel\Context;
|
2015-08-27 16:10:08 +02:00
|
|
|
|
2017-06-21 17:40:00 +02:00
|
|
|
use Amp\ByteStream;
|
2017-05-18 09:51:31 +02:00
|
|
|
use Amp\Parallel\Sync\ChannelException;
|
2017-06-16 06:46:15 +02:00
|
|
|
use Amp\Parallel\Sync\ChannelledStream;
|
2017-07-29 00:34:24 +02:00
|
|
|
use Amp\Parallel\Sync\ExitResult;
|
2017-12-08 04:26:55 +01:00
|
|
|
use Amp\Parallel\Sync\SynchronizationError;
|
2017-11-29 22:01:32 +01:00
|
|
|
use Amp\Process\Process as BaseProcess;
|
2017-05-18 09:51:31 +02:00
|
|
|
use Amp\Promise;
|
2017-06-21 17:40:00 +02:00
|
|
|
use function Amp\asyncCall;
|
2017-05-18 09:51:31 +02:00
|
|
|
use function Amp\call;
|
2016-08-18 18:04:48 +02:00
|
|
|
|
2017-11-29 22:01:32 +01:00
|
|
|
class Process implements Context {
|
2017-12-10 22:37:33 +01:00
|
|
|
const SCRIPT_PATH = __DIR__ . "/Internal/process-runner.php";
|
|
|
|
|
2018-01-22 15:03:31 +01:00
|
|
|
/** @var string|null External version of SCRIPT_PATH if inside a PHAR. */
|
|
|
|
private static $pharScriptPath;
|
|
|
|
|
2017-12-06 01:21:39 +01:00
|
|
|
/** @var string|null Cached path to located PHP binary. */
|
|
|
|
private static $binaryPath;
|
|
|
|
|
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-12-10 22:37:33 +01:00
|
|
|
/**
|
|
|
|
* Creates and starts the process at the given path using the optional PHP binary path.
|
|
|
|
*
|
|
|
|
* @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', 'Option1Value', 'Option2Value'].
|
2017-12-13 04:06:11 +01:00
|
|
|
* @param string|null $cwd Working directory.
|
|
|
|
* @param mixed[] $env Array of environment variables.
|
2017-12-10 22:37:33 +01:00
|
|
|
* @param string $binary Path to PHP binary. Null will attempt to automatically locate the binary.
|
|
|
|
*
|
|
|
|
* @return \Amp\Parallel\Context\Process
|
|
|
|
*/
|
2017-12-13 04:06:11 +01:00
|
|
|
public static function run($script, string $cwd = null, array $env = [], string $binary = null): self {
|
|
|
|
$process = new self($script, $cwd, $env, $binary);
|
2017-12-10 22:37:33 +01:00
|
|
|
$process->start();
|
|
|
|
return $process;
|
|
|
|
}
|
|
|
|
|
2015-08-27 16:10:08 +02:00
|
|
|
/**
|
2017-07-26 08:02:34 +02:00
|
|
|
* @param string|array $script Path to PHP script or array with first element as path and following elements options
|
2017-12-10 22:37:33 +01:00
|
|
|
* to the PHP script (e.g.: ['bin/worker', 'Option1Value', 'Option2Value'].
|
2017-12-13 04:06:11 +01:00
|
|
|
* @param string|null $cwd Working directory.
|
2015-08-27 16:10:08 +02:00
|
|
|
* @param mixed[] $env Array of environment variables.
|
2017-12-13 04:06:11 +01:00
|
|
|
* @param string $binary Path to PHP binary. Null will attempt to automatically locate the binary.
|
2017-12-10 22:37:33 +01:00
|
|
|
*
|
|
|
|
* @throws \Error If the PHP binary path given cannot be found or is not executable.
|
2015-08-27 16:10:08 +02:00
|
|
|
*/
|
2017-12-13 04:06:11 +01:00
|
|
|
public function __construct($script, string $cwd = null, array $env = [], string $binary = null) {
|
2017-06-21 17:30:26 +02:00
|
|
|
$options = [
|
|
|
|
"html_errors" => "0",
|
|
|
|
"display_errors" => "0",
|
|
|
|
"log_errors" => "1",
|
|
|
|
];
|
|
|
|
|
2017-07-26 08:02:34 +02:00
|
|
|
if (\is_array($script)) {
|
|
|
|
$script = \implode(" ", \array_map("escapeshellarg", $script));
|
|
|
|
} else {
|
|
|
|
$script = \escapeshellarg($script);
|
|
|
|
}
|
|
|
|
|
2017-12-06 01:21:39 +01:00
|
|
|
if ($binary === null) {
|
2017-12-06 23:01:11 +01:00
|
|
|
if (\PHP_SAPI === "cli") {
|
|
|
|
$binary = \PHP_BINARY;
|
|
|
|
} else {
|
|
|
|
$binary = self::$binaryPath ?? self::locateBinary();
|
|
|
|
}
|
2017-12-06 01:21:39 +01:00
|
|
|
} elseif (!\is_executable($binary)) {
|
|
|
|
throw new \Error(\sprintf("The PHP binary path '%s' was not found or is not executable", $binary));
|
2017-06-24 00:29:34 +02:00
|
|
|
}
|
|
|
|
|
2018-01-22 15:03:31 +01:00
|
|
|
// Write process runner to external file if inside a PHAR,
|
|
|
|
// because PHP can't open files inside a PHAR directly except for the stub.
|
|
|
|
if (\strpos(self::SCRIPT_PATH, "phar://") === 0) {
|
|
|
|
if (self::$pharScriptPath) {
|
|
|
|
$scriptPath = self::$pharScriptPath;
|
|
|
|
} else {
|
|
|
|
$contents = \file_get_contents(self::SCRIPT_PATH);
|
|
|
|
$contents = \str_replace("__DIR__", \var_export(\dirname(self::SCRIPT_PATH), true), $contents);
|
|
|
|
self::$pharScriptPath = $scriptPath = \tempnam(\sys_get_temp_dir(), "amp-process-runner-");
|
|
|
|
\file_put_contents($scriptPath, $contents);
|
|
|
|
|
|
|
|
\register_shutdown_function(static function () {
|
|
|
|
@\unlink(self::$pharScriptPath);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$scriptPath = self::SCRIPT_PATH;
|
|
|
|
}
|
|
|
|
|
2017-12-10 22:37:33 +01:00
|
|
|
$command = \implode(" ", [
|
|
|
|
\escapeshellarg($binary),
|
|
|
|
$this->formatOptions($options),
|
2018-01-31 18:31:36 +01:00
|
|
|
\escapeshellarg($scriptPath),
|
2017-12-10 22:37:33 +01:00
|
|
|
$script,
|
|
|
|
]);
|
2017-12-06 01:21:39 +01:00
|
|
|
|
2017-12-06 23:01:11 +01:00
|
|
|
$this->process = new BaseProcess($command, $cwd, $env);
|
2015-08-27 16:10:08 +02:00
|
|
|
}
|
|
|
|
|
2017-12-06 01:21:39 +01:00
|
|
|
private static function locateBinary(): string {
|
|
|
|
$executable = \strncasecmp(\PHP_OS, "WIN", 3) === 0 ? "php.exe" : "php";
|
2018-01-22 23:15:09 +01:00
|
|
|
|
|
|
|
$paths = \array_filter(\explode(\PATH_SEPARATOR, \getenv("PATH")));
|
|
|
|
$paths[] = \PHP_BINDIR;
|
|
|
|
$paths = \array_unique($paths);
|
|
|
|
|
|
|
|
foreach ($paths as $path) {
|
2017-12-06 01:21:39 +01:00
|
|
|
$path .= \DIRECTORY_SEPARATOR . $executable;
|
|
|
|
if (\is_executable($path)) {
|
|
|
|
return self::$binaryPath = $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \Error("Could not locate PHP executable binary");
|
|
|
|
}
|
|
|
|
|
2017-06-21 17:30:26 +02:00
|
|
|
private function formatOptions(array $options) {
|
|
|
|
$result = [];
|
|
|
|
|
|
|
|
foreach ($options as $option => $value) {
|
2017-12-10 22:37:33 +01:00
|
|
|
$result[] = \sprintf("-d%s=%s", $option, $value);
|
2017-06-21 17:30:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return \implode(" ", $result);
|
|
|
|
}
|
|
|
|
|
2015-08-27 20:06:39 +02:00
|
|
|
/**
|
2017-12-13 20:56:11 +01:00
|
|
|
* Private method to prevent cloning.
|
2015-08-27 20:06:39 +02:00
|
|
|
*/
|
2017-12-13 20:56:11 +01:00
|
|
|
private function __clone() {
|
2015-08-27 20:06:39 +02:00
|
|
|
}
|
|
|
|
|
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());
|
2017-06-21 17:40:00 +02:00
|
|
|
|
2017-06-23 16:44:17 +02:00
|
|
|
/** @var ByteStream\ResourceInputStream $childStderr */
|
2017-06-21 17:40:00 +02:00
|
|
|
$childStderr = $this->process->getStderr();
|
2017-06-23 16:44:17 +02:00
|
|
|
$childStderr->unreference();
|
2017-06-21 17:40:00 +02:00
|
|
|
|
2017-12-23 23:18:09 +01:00
|
|
|
asyncCall(static function () use ($childStderr) {
|
2017-06-21 17:40:00 +02:00
|
|
|
$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) {
|
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-12-08 03:49:44 +01:00
|
|
|
return call(function () {
|
|
|
|
try {
|
|
|
|
$data = yield $this->channel->receive();
|
|
|
|
} 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:15:30 +01:00
|
|
|
|
2017-12-08 03:49:44 +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)
|
|
|
|
));
|
|
|
|
}
|
2017-03-09 23:48:34 +01:00
|
|
|
|
2017-12-08 03:49:44 +01:00
|
|
|
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-12-13 23:29:44 +01:00
|
|
|
return $this->channel->send($data);
|
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");
|
|
|
|
}
|
|
|
|
|
2017-12-08 03:49:44 +01:00
|
|
|
return call(function () {
|
|
|
|
try {
|
|
|
|
$data = yield $this->channel->receive();
|
|
|
|
if (!$data instanceof ExitResult) {
|
|
|
|
throw new SynchronizationError("Did not receive an exit result from process");
|
|
|
|
}
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
$this->kill();
|
|
|
|
throw $exception;
|
2017-01-16 19:56:49 +01:00
|
|
|
}
|
|
|
|
|
2017-12-08 03:49:44 +01:00
|
|
|
$code = yield $this->process->join();
|
|
|
|
if ($code !== 0) {
|
|
|
|
throw new ContextException(\sprintf("Process exited with code %d", $code));
|
|
|
|
}
|
2017-01-16 19:56:49 +01:00
|
|
|
|
2017-12-08 03:49:44 +01:00
|
|
|
return $data->getResult();
|
|
|
|
});
|
2015-08-27 16:10:08 +02:00
|
|
|
}
|
|
|
|
|
2018-03-15 00:51:03 +01:00
|
|
|
/**
|
|
|
|
* Send a signal to the process.
|
|
|
|
*
|
|
|
|
* @see \Amp\Process\Process::signal()
|
|
|
|
*
|
|
|
|
* @param int $signo
|
|
|
|
*
|
|
|
|
* @throws \Amp\Process\ProcessException
|
|
|
|
* @throws \Amp\Process\StatusError
|
|
|
|
*/
|
|
|
|
public function signal(int $signo) {
|
|
|
|
$this->process->signal($signo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a promise resolving to the process PID.
|
|
|
|
*
|
|
|
|
* @see \Amp\Process\Process::getPid()
|
|
|
|
*
|
|
|
|
* @return \Amp\Promise
|
|
|
|
* @throws \Amp\Process\StatusError
|
|
|
|
*/
|
|
|
|
public function getPid(): Promise {
|
|
|
|
return $this->process->getPid();
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|