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

171 lines
4.1 KiB
PHP
Raw Normal View History

2016-08-22 06:40:48 +02:00
<?php declare(strict_types = 1);
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
use Amp\{ Coroutine, Deferred };
2016-08-23 23:47:40 +02:00
use Amp\Parallel\{ StatusError, Strand, WorkerException} ;
use Amp\Parallel\Worker\Internal\{ Job, TaskResult };
2016-08-18 18:04:48 +02:00
use Interop\Async\Awaitable;
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 {
2016-08-23 23:47:40 +02:00
/** @var \Amp\Parallel\Strand */
2015-12-05 06:50:32 +01:00
private $context;
/** @var bool */
2015-12-05 06:50:32 +01:00
private $shutdown = false;
/** @var \Amp\Deferred[] */
private $jobQueue = [];
/** @var callable */
private $when;
2015-12-05 06:50:32 +01:00
/**
2016-08-23 23:47:40 +02:00
* @param \Amp\Parallel\Strand $strand
2015-12-05 06:50:32 +01:00
*/
2016-08-18 18:04:48 +02:00
public function __construct(Strand $strand) {
2015-12-21 18:04:51 +01:00
$this->context = $strand;
$this->when = function ($exception, $data) {
if ($exception) {
$this->kill();
return;
}
if (!$data instanceof TaskResult) {
$this->kill();
return;
}
$id = $data->getId();
if (!isset($this->jobQueue[$id])) {
$this->kill();
return;
}
$deferred = $this->jobQueue[$id];
unset($this->jobQueue[$id]);
if (!empty($this->jobQueue)) {
$this->context->receive()->when($this->when);
}
$deferred->resolve($data->getAwaitable());
};
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-08-18 18:04:48 +02:00
public function enqueue(Task $task): Awaitable {
2015-12-05 06:50:32 +01:00
if (!$this->context->isRunning()) {
throw new StatusError('The worker has not been started.');
}
2016-08-18 18:04:48 +02:00
2015-12-05 06:50:32 +01:00
if ($this->shutdown) {
throw new StatusError('The worker has been shut down.');
}
2016-08-18 18:04:48 +02:00
return new Coroutine($this->doEnqueue($task));
}
2016-08-18 18:04:48 +02:00
/**
* @coroutine
*
2016-08-23 23:47:40 +02:00
* @param \Amp\Parallel\Worker\Task $task
2016-08-18 18:04:48 +02:00
*
* @return \Generator
2016-08-23 23:47:40 +02:00
* @throws \Amp\Parallel\StatusError
* @throws \Amp\Parallel\TaskException
* @throws \Amp\Parallel\WorkerException
2016-08-18 18:04:48 +02:00
*/
private function doEnqueue(Task $task): \Generator {
if (empty($this->jobQueue)) {
$this->context->receive()->when($this->when);
2015-12-05 06:50:32 +01:00
}
2015-12-16 18:13:06 +01:00
try {
$job = new Job($task);
$this->jobQueue[$job->getId()] = $deferred = new Deferred;
yield $this->context->send($job);
2016-01-26 06:02:22 +01:00
} catch (\Throwable $exception) {
$this->kill();
throw new WorkerException('Sending the task to the worker failed.', $exception);
2015-12-05 06:50:32 +01:00
}
return yield $deferred->getAwaitable();
2016-08-18 18:04:48 +02:00
}
/**
* {@inheritdoc}
*/
public function shutdown(): Awaitable {
if (!$this->context->isRunning() || $this->shutdown) {
throw new StatusError('The worker is not running.');
}
2016-08-18 18:04:48 +02:00
return new Coroutine($this->doShutdown());
}
2015-12-05 06:50:32 +01:00
/**
* {@inheritdoc}
*/
2016-08-18 18:04:48 +02:00
private function doShutdown(): \Generator {
2015-12-05 06:50:32 +01:00
$this->shutdown = true;
// If a task is currently running, wait for it to finish.
yield \Amp\any($this->jobQueue);
2016-08-18 18:04:48 +02: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() {
$this->cancelPending();
2015-12-05 06:50:32 +01:00
$this->context->kill();
}
/**
* Cancels all pending tasks.
*/
2016-08-18 18:04:48 +02:00
private function cancelPending() {
if (!empty($this->jobQueue)) {
$exception = new WorkerException('Worker was shut down.');
foreach ($this->jobQueue as $job) {
$job->fail($exception);
}
$this->jobQueue = [];
}
2015-12-05 06:50:32 +01:00
}
}