1
0
mirror of https://github.com/danog/parallel.git synced 2025-01-23 06:21:12 +01:00
parallel/lib/Worker/AbstractWorker.php

145 lines
3.5 KiB
PHP
Raw Normal View History

2016-12-29 19:16:04 -06:00
<?php
2015-12-04 23:50:32 -06:00
2016-08-23 16:47:40 -05:00
namespace Amp\Parallel\Worker;
2016-08-18 11:04:48 -05:00
2017-12-07 21:26:55 -06:00
use Amp\Parallel\Context\Context;
use Amp\Parallel\Context\StatusError;
use Amp\Parallel\Worker\Internal\TaskResult;
2017-05-18 09:51:31 +02:00
use Amp\Promise;
use Amp\Success;
use function Amp\call;
2015-12-04 23:50:32 -06:00
/**
* Base class for most common types of task workers.
*/
2016-08-18 11:04:48 -05:00
abstract class AbstractWorker implements Worker {
2017-12-07 21:26:55 -06:00
/** @var \Amp\Parallel\Context\Context */
2015-12-04 23:50:32 -06:00
private $context;
/** @var bool */
2015-12-04 23:50:32 -06:00
private $shutdown = false;
2017-05-18 09:51:31 +02:00
/** @var \Amp\Promise|null */
private $pending;
2015-12-04 23:50:32 -06:00
/**
2017-12-07 21:26:55 -06:00
* @param \Amp\Parallel\Context\Context $context
2015-12-04 23:50:32 -06:00
*/
public function __construct(Context $context) {
2017-11-29 14:40:07 -06:00
if ($context->isRunning()) {
throw new \Error("The context was already running");
}
$this->context = $context;
2015-12-04 23:50:32 -06:00
}
/**
* {@inheritdoc}
*/
2016-08-18 11:04:48 -05:00
public function isRunning(): bool {
2015-12-04 23:50:32 -06:00
return $this->context->isRunning();
}
/**
* {@inheritdoc}
*/
2016-08-18 11:04:48 -05:00
public function isIdle(): bool {
return $this->pending === null;
2015-12-04 23:50:32 -06:00
}
/**
* {@inheritdoc}
*/
2016-11-14 17:43:44 -06:00
public function enqueue(Task $task): Promise {
2015-12-04 23:50:32 -06:00
if ($this->shutdown) {
2017-06-17 23:57:12 -05:00
throw new StatusError("The worker has been shut down");
}
2017-05-18 09:51:31 +02:00
$promise = $this->pending = call(function () use (&$promise, $task) {
if ($this->pending) {
try {
yield $this->pending;
} catch (\Throwable $exception) {
// Ignore error from prior job.
2017-12-13 16:29:44 -06:00
}
}
if ($this->shutdown) {
throw new WorkerException("The worker was shutdown");
}
if (!$this->context->isRunning()) {
yield $this->context->start();
}
$job = new Internal\Job($task);
yield $this->context->send($job);
$result = yield $this->context->receive();
if (!$result instanceof TaskResult) {
$this->cancel(new WorkerException("Context did not return a task result"));
}
if ($result->getId() !== $job->getId()) {
$this->cancel(new WorkerException("Task results returned out of order"));
}
return $result->promise();
2017-12-13 16:29:44 -06:00
});
$promise->onResolve(function () use ($promise) {
if ($this->pending === $promise) {
$this->pending = null;
}
});
return $promise;
2016-08-18 11:04:48 -05:00
}
/**
* {@inheritdoc}
*/
2016-11-14 17:43:44 -06:00
public function shutdown(): Promise {
if ($this->shutdown) {
2017-06-17 23:57:12 -05:00
throw new StatusError("The worker is not running");
}
2017-05-18 09:51:31 +02:00
2015-12-04 23:50:32 -06:00
$this->shutdown = true;
if (!$this->context->isRunning()) {
return new Success(0);
}
return call(function () {
if ($this->pending) {
// If a task is currently running, wait for it to finish.
yield Promise\any([$this->pending]);
}
yield $this->context->send(0);
return yield $this->context->join();
});
2015-12-04 23:50:32 -06:00
}
/**
* {@inheritdoc}
*/
2016-08-18 11:04:48 -05:00
public function kill() {
2017-06-17 23:57:12 -05:00
$this->cancel();
}
/**
2017-06-17 23:57:12 -05:00
* Cancels all pending tasks and kills the context.
*
* @TODO Parameter kept for BC, remove in future version.
*
2017-06-17 23:57:12 -05:00
* @param \Throwable|null $exception Optional exception to be used as the previous exception.
*/
2017-06-17 23:57:12 -05:00
protected function cancel(\Throwable $exception = null) {
if ($this->context->isRunning()) {
$this->context->kill();
}
2015-12-04 23:50:32 -06:00
}
}