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

164 lines
3.9 KiB
PHP
Raw Normal View History

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;
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;
use function Amp\call;
2015-12-05 06:50:32 +01:00
/**
* Base class for most common types of task workers.
*/
2016-08-18 18:04:48 +02:00
abstract class AbstractWorker implements Worker {
/** @var \Amp\Parallel\Context */
2015-12-05 06:50:32 +01:00
private $context;
/** @var bool */
2015-12-05 06:50:32 +01:00
private $shutdown = false;
2017-05-18 09:51:31 +02:00
/** @var \Amp\Deferred[] */
private $jobQueue = [];
2017-05-18 09:51:31 +02:00
/** @var callable */
2017-06-18 06:57:12 +02:00
private $onResolve;
2015-12-05 06:50:32 +01:00
/**
* @param \Amp\Parallel\Context $context
2015-12-05 06:50:32 +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");
}
$this->context = $context;
2017-05-18 09:51:31 +02:00
2017-06-18 06:57:12 +02:00
$this->onResolve = function ($exception, $data) {
if ($exception) {
2017-06-18 06:57:12 +02:00
$this->cancel($exception);
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"));
return;
}
2017-05-18 09:51:31 +02:00
$id = $data->getId();
2017-05-18 09:51:31 +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"));
return;
}
2017-05-18 09:51:31 +02:00
$deferred = $this->jobQueue[$id];
unset($this->jobQueue[$id]);
$empty = empty($this->jobQueue);
2017-05-18 09:51:31 +02:00
$deferred->resolve($data->promise());
if (!$empty) {
2017-06-18 06:57:12 +02:00
$this->context->receive()->onResolve($this->onResolve);
}
};
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 {
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");
}
2017-05-18 09:51:31 +02:00
$empty = empty($this->jobQueue);
2017-05-18 09:51:31 +02:00
$job = new Internal\Job($task);
$this->jobQueue[$job->getId()] = $deferred = new Deferred;
$this->context->send($job);
2017-05-18 09:51:31 +02:00
if ($empty) {
$this->context->receive()->onResolve($this->onResolve);
}
return $deferred->promise();
2016-08-18 18:04:48 +02:00
}
/**
* {@inheritdoc}
*/
2016-11-15 00:43:44 +01:00
public function shutdown(): Promise {
if (!$this->context->isRunning() || $this->shutdown) {
2017-06-18 06:57:12 +02:00
throw new StatusError("The worker is not running");
}
2017-05-18 09:51:31 +02:00
2015-12-05 06:50:32 +01:00
$this->shutdown = true;
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));
}
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();
}
/**
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.
*/
2017-06-18 06:57:12 +02:00
protected function cancel(\Throwable $exception = null) {
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
foreach ($this->jobQueue as $job) {
$job->fail($exception);
}
2017-05-18 09:51:31 +02:00
$this->jobQueue = [];
}
2017-06-18 06:57:12 +02:00
$this->context->kill();
2015-12-05 06:50:32 +01:00
}
}