2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2015-12-05 06:50:32 +01:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
namespace Amp\Parallel\Worker;
|
2016-08-18 18:04:48 +02:00
|
|
|
|
2018-10-27 16:59:10 +02:00
|
|
|
use Amp\Failure;
|
2017-12-08 04:26:55 +01:00
|
|
|
use Amp\Parallel\Context\Context;
|
|
|
|
use Amp\Parallel\Context\StatusError;
|
2018-10-24 05:14:51 +02:00
|
|
|
use Amp\Parallel\Sync\ChannelException;
|
2017-05-18 09:51:31 +02:00
|
|
|
use Amp\Promise;
|
2017-12-13 21:14:31 +01:00
|
|
|
use Amp\Success;
|
2017-12-08 03:49:44 +01:00
|
|
|
use function Amp\call;
|
2015-12-05 06:50:32 +01:00
|
|
|
|
2015-12-12 01:15:15 +01:00
|
|
|
/**
|
|
|
|
* Base class for most common types of task workers.
|
|
|
|
*/
|
2018-10-27 01:29:45 +02:00
|
|
|
abstract class TaskWorker implements Worker
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2018-12-30 19:37:42 +01:00
|
|
|
const SHUTDOWN_TIMEOUT = 1000;
|
|
|
|
|
2017-12-08 04:26:55 +01:00
|
|
|
/** @var \Amp\Parallel\Context\Context */
|
2015-12-05 06:50:32 +01:00
|
|
|
private $context;
|
|
|
|
|
2017-12-24 23:49:23 +01:00
|
|
|
/** @var \Amp\Promise|null */
|
|
|
|
private $pending;
|
2017-12-23 23:18:09 +01:00
|
|
|
|
2018-10-27 16:59:10 +02:00
|
|
|
/** @var \Amp\Promise|null */
|
|
|
|
private $exitStatus;
|
|
|
|
|
2015-12-05 06:50:32 +01:00
|
|
|
/**
|
2018-10-24 05:14:51 +02:00
|
|
|
* @param \Amp\Parallel\Context\Context $context A context running an instance of TaskRunner.
|
2015-12-05 06:50:32 +01:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function __construct(Context $context)
|
|
|
|
{
|
2017-11-29 21:40:07 +01:00
|
|
|
if ($context->isRunning()) {
|
|
|
|
throw new \Error("The context was already running");
|
|
|
|
}
|
|
|
|
|
2017-11-10 16:58:42 +01:00
|
|
|
$this->context = $context;
|
2018-12-30 19:37:42 +01:00
|
|
|
|
|
|
|
$context = &$this->context;
|
|
|
|
\register_shutdown_function(static function () use (&$context) {
|
|
|
|
if ($context === null || !$context->isRunning()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Promise\wait(Promise\timeout(call(function () use ($context) {
|
|
|
|
yield $context->send(0);
|
|
|
|
return yield $context->join();
|
|
|
|
}), self::SHUTDOWN_TIMEOUT));
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
$context->kill();
|
|
|
|
}
|
|
|
|
});
|
2015-12-05 06:50:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function isRunning(): bool
|
|
|
|
{
|
2018-10-27 16:59:10 +02:00
|
|
|
return !$this->exitStatus;
|
2015-12-05 06:50:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function isIdle(): bool
|
|
|
|
{
|
2017-12-24 23:49:23 +01:00
|
|
|
return $this->pending === null;
|
2015-12-05 06:50:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function enqueue(Task $task): Promise
|
|
|
|
{
|
2018-10-27 16:59:10 +02:00
|
|
|
if ($this->exitStatus) {
|
2017-06-18 06:57:12 +02:00
|
|
|
throw new StatusError("The worker has been shut down");
|
2015-12-12 01:15:15 +01:00
|
|
|
}
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2018-10-08 18:24:46 +02:00
|
|
|
$promise = $this->pending = call(function () use ($task) {
|
2017-12-24 23:49:23 +01:00
|
|
|
if ($this->pending) {
|
|
|
|
try {
|
|
|
|
yield $this->pending;
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
// Ignore error from prior job.
|
2017-12-13 23:29:44 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-29 00:34:24 +02:00
|
|
|
|
2018-10-27 16:59:10 +02:00
|
|
|
if ($this->exitStatus) {
|
2017-12-24 23:49:23 +01:00
|
|
|
throw new WorkerException("The worker was shutdown");
|
|
|
|
}
|
|
|
|
|
2018-10-06 17:05:49 +02:00
|
|
|
if (!$this->context->isRunning()) {
|
|
|
|
yield $this->context->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
$job = new Internal\Job($task);
|
|
|
|
|
2018-10-24 05:14:51 +02:00
|
|
|
try {
|
|
|
|
yield $this->context->send($job);
|
|
|
|
$result = yield $this->context->receive();
|
|
|
|
} catch (ChannelException $exception) {
|
2018-12-30 19:37:42 +01:00
|
|
|
$this->kill();
|
2018-10-25 05:49:01 +02:00
|
|
|
throw new WorkerException("Communicating with the worker failed", 0, $exception);
|
2018-10-24 05:14:51 +02:00
|
|
|
}
|
2017-12-24 23:49:23 +01:00
|
|
|
|
2018-10-08 18:24:46 +02:00
|
|
|
if (!$result instanceof Internal\TaskResult) {
|
|
|
|
$this->kill();
|
|
|
|
throw new WorkerException("Context did not return a task result");
|
2017-12-24 23:49:23 +01:00
|
|
|
}
|
|
|
|
|
2018-10-06 17:05:49 +02:00
|
|
|
if ($result->getId() !== $job->getId()) {
|
2018-10-08 18:24:46 +02:00
|
|
|
$this->kill();
|
|
|
|
throw new WorkerException("Task results returned out of order");
|
2017-12-24 23:49:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result->promise();
|
2017-12-13 23:29:44 +01:00
|
|
|
});
|
2017-12-24 23:49:23 +01:00
|
|
|
|
|
|
|
$promise->onResolve(function () use ($promise) {
|
|
|
|
if ($this->pending === $promise) {
|
|
|
|
$this->pending = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return $promise;
|
2016-08-18 18:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function shutdown(): Promise
|
|
|
|
{
|
2018-10-27 16:59:10 +02:00
|
|
|
if ($this->exitStatus) {
|
|
|
|
return $this->exitStatus;
|
2016-08-21 17:33:39 +02:00
|
|
|
}
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2018-12-30 19:37:42 +01:00
|
|
|
if ($this->context === null || !$this->context->isRunning()) {
|
2018-10-27 16:59:10 +02:00
|
|
|
return $this->exitStatus = new Success(0);
|
2017-12-13 21:14:31 +01:00
|
|
|
}
|
|
|
|
|
2018-10-27 16:59:10 +02:00
|
|
|
return $this->exitStatus = call(function () {
|
2017-12-24 23:49:23 +01:00
|
|
|
if ($this->pending) {
|
2017-12-08 03:49:44 +01:00
|
|
|
// If a task is currently running, wait for it to finish.
|
2017-12-24 23:49:23 +01:00
|
|
|
yield Promise\any([$this->pending]);
|
2017-12-08 03:49:44 +01:00
|
|
|
}
|
2015-12-12 05:44:25 +01:00
|
|
|
|
2017-12-08 03:49:44 +01:00
|
|
|
yield $this->context->send(0);
|
2018-12-30 19:37:42 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
return yield Promise\timeout($this->context->join(), self::SHUTDOWN_TIMEOUT);
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
$this->context->kill();
|
|
|
|
throw new WorkerException("Failed to gracefully shutdown worker", 0, $exception);
|
|
|
|
} finally {
|
|
|
|
$this->context = null; // Null property to free memory because shutdown function has reference to context.
|
|
|
|
}
|
2017-12-08 03:49:44 +01:00
|
|
|
});
|
2015-12-05 06:50:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function kill()
|
|
|
|
{
|
2018-12-30 19:37:42 +01:00
|
|
|
if ($this->exitStatus || $this->context === null) {
|
2018-10-27 16:59:10 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-12-12 05:44:25 +01:00
|
|
|
|
2017-12-24 23:49:23 +01:00
|
|
|
if ($this->context->isRunning()) {
|
|
|
|
$this->context->kill();
|
2018-10-27 16:59:10 +02:00
|
|
|
$this->exitStatus = new Failure(new WorkerException("The worker was killed"));
|
|
|
|
return;
|
2017-12-24 23:49:23 +01:00
|
|
|
}
|
2018-10-27 16:59:10 +02:00
|
|
|
|
|
|
|
$this->exitStatus = new Success(0);
|
2018-12-30 19:37:42 +01:00
|
|
|
$this->context = null; // Null property to free memory because shutdown function has reference to context.
|
2015-12-05 06:50:32 +01:00
|
|
|
}
|
|
|
|
}
|