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
|
|
|
|
2017-05-18 09:51:31 +02:00
|
|
|
use Amp\Deferred;
|
2017-11-10 16:58:42 +01:00
|
|
|
use Amp\Parallel\Context;
|
2017-06-18 06:57:12 +02:00
|
|
|
use Amp\Parallel\ContextException;
|
2017-05-18 09:51:31 +02:00
|
|
|
use Amp\Parallel\StatusError;
|
|
|
|
use Amp\Promise;
|
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.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
abstract class AbstractWorker implements Worker {
|
2017-11-10 16:58:42 +01:00
|
|
|
/** @var \Amp\Parallel\Context */
|
2015-12-05 06:50:32 +01:00
|
|
|
private $context;
|
|
|
|
|
2016-08-21 17:33:39 +02:00
|
|
|
/** @var bool */
|
2015-12-05 06:50:32 +01:00
|
|
|
private $shutdown = false;
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2016-08-21 17:33:39 +02:00
|
|
|
/** @var \Amp\Deferred[] */
|
|
|
|
private $jobQueue = [];
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2016-08-21 17:33:39 +02:00
|
|
|
/** @var callable */
|
2017-06-18 06:57:12 +02:00
|
|
|
private $onResolve;
|
2015-12-12 01:15:15 +01:00
|
|
|
|
2015-12-05 06:50:32 +01:00
|
|
|
/**
|
2017-11-10 16:58:42 +01:00
|
|
|
* @param \Amp\Parallel\Context $context
|
2015-12-05 06:50:32 +01:00
|
|
|
*/
|
2017-11-10 16:58:42 +01: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;
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2017-06-18 06:57:12 +02:00
|
|
|
$this->onResolve = function ($exception, $data) {
|
2016-08-21 17:33:39 +02:00
|
|
|
if ($exception) {
|
2017-06-18 06:57:12 +02:00
|
|
|
$this->cancel($exception);
|
2016-08-21 17:33:39 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2017-06-18 06:57:12 +02:00
|
|
|
if (!$data instanceof Internal\TaskResult) {
|
|
|
|
$this->cancel(new ContextException("Context did not return a task result"));
|
2016-08-21 17:33:39 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2016-08-21 17:33:39 +02:00
|
|
|
$id = $data->getId();
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2016-08-21 17:33:39 +02:00
|
|
|
if (!isset($this->jobQueue[$id])) {
|
2017-06-18 06:57:12 +02:00
|
|
|
$this->cancel(new ContextException("Job ID returned by context does not exist"));
|
2016-08-21 17:33:39 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2016-08-21 17:33:39 +02:00
|
|
|
$deferred = $this->jobQueue[$id];
|
|
|
|
unset($this->jobQueue[$id]);
|
2017-06-20 05:14:19 +02:00
|
|
|
$empty = empty($this->jobQueue);
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2017-06-20 05:14:19 +02:00
|
|
|
$deferred->resolve($data->promise());
|
|
|
|
|
|
|
|
if (!$empty) {
|
2017-06-18 06:57:12 +02:00
|
|
|
$this->context->receive()->onResolve($this->onResolve);
|
2016-08-21 17:33:39 +02:00
|
|
|
}
|
|
|
|
};
|
2015-12-05 06:50:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function isRunning(): bool {
|
2015-12-05 06:50:32 +01:00
|
|
|
return $this->context->isRunning();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function isIdle(): bool {
|
2016-08-21 17:33:39 +02:00
|
|
|
return empty($this->jobQueue);
|
2015-12-05 06:50:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function start() {
|
2015-12-05 06:50:32 +01:00
|
|
|
$this->context->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 00:43:44 +01:00
|
|
|
public function enqueue(Task $task): Promise {
|
2015-12-05 06:50:32 +01:00
|
|
|
if (!$this->context->isRunning()) {
|
2017-06-18 06:57:12 +02:00
|
|
|
throw new StatusError("The worker has not been started");
|
2015-12-05 06:50:32 +01:00
|
|
|
}
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2015-12-05 06:50:32 +01:00
|
|
|
if ($this->shutdown) {
|
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
|
|
|
|
2017-06-20 05:14:19 +02:00
|
|
|
$empty = empty($this->jobQueue);
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2017-07-29 00:34:24 +02:00
|
|
|
$job = new Internal\Job($task);
|
|
|
|
$this->jobQueue[$job->getId()] = $deferred = new Deferred;
|
2017-06-20 05:14:19 +02:00
|
|
|
|
2017-12-08 03:49:44 +01:00
|
|
|
$this->context->send($job);
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2017-07-29 00:34:24 +02:00
|
|
|
if ($empty) {
|
|
|
|
$this->context->receive()->onResolve($this->onResolve);
|
|
|
|
}
|
|
|
|
|
2017-12-08 03:49:44 +01:00
|
|
|
return $deferred->promise();
|
2016-08-18 18:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 00:43:44 +01:00
|
|
|
public function shutdown(): Promise {
|
2016-08-21 17:33:39 +02:00
|
|
|
if (!$this->context->isRunning() || $this->shutdown) {
|
2017-06-18 06:57:12 +02:00
|
|
|
throw new StatusError("The worker is not running");
|
2016-08-21 17:33:39 +02:00
|
|
|
}
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2015-12-05 06:50:32 +01:00
|
|
|
$this->shutdown = true;
|
|
|
|
|
2017-12-08 03:49:44 +01:00
|
|
|
return call(function () {
|
|
|
|
if (!empty($this->jobQueue)) {
|
|
|
|
// If a task is currently running, wait for it to finish.
|
|
|
|
yield Promise\any(\array_map(function (Deferred $deferred): Promise {
|
|
|
|
return $deferred->promise();
|
|
|
|
}, $this->jobQueue));
|
|
|
|
}
|
2015-12-12 05:44:25 +01:00
|
|
|
|
2017-12-08 03:49:44 +01:00
|
|
|
yield $this->context->send(0);
|
|
|
|
return yield $this->context->join();
|
|
|
|
});
|
2015-12-05 06:50:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function kill() {
|
2017-06-18 06:57:12 +02:00
|
|
|
$this->cancel();
|
2015-12-12 05:44:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-18 06:57:12 +02:00
|
|
|
* Cancels all pending tasks and kills the context.
|
|
|
|
*
|
|
|
|
* @param \Throwable|null $exception Optional exception to be used as the previous exception.
|
2015-12-12 05:44:25 +01:00
|
|
|
*/
|
2017-06-18 06:57:12 +02:00
|
|
|
protected function cancel(\Throwable $exception = null) {
|
2016-08-21 17:33:39 +02:00
|
|
|
if (!empty($this->jobQueue)) {
|
2017-06-18 06:57:12 +02:00
|
|
|
$exception = new WorkerException('Worker was shut down', $exception);
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2016-08-21 17:33:39 +02:00
|
|
|
foreach ($this->jobQueue as $job) {
|
|
|
|
$job->fail($exception);
|
|
|
|
}
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2016-08-21 17:33:39 +02:00
|
|
|
$this->jobQueue = [];
|
2015-12-12 01:15:15 +01:00
|
|
|
}
|
2017-06-18 06:57:12 +02:00
|
|
|
|
|
|
|
$this->context->kill();
|
2015-12-05 06:50:32 +01:00
|
|
|
}
|
|
|
|
}
|