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

170 lines
4.0 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
use Amp\{ Coroutine, Deferred, Promise };
2016-08-23 23:47:40 +02:00
use Amp\Parallel\{ StatusError, Strand, WorkerException} ;
use Amp\Parallel\Worker\Internal\{ Job, TaskResult };
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);
}
2016-11-15 00:43:44 +01:00
$deferred->resolve($data->promise());
};
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()) {
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
}
2016-11-15 00:43:44 +01:00
return yield $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) {
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 Promise\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
}
}