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

173 lines
4.2 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
use Amp\{ Coroutine, Deferred, Promise };
use Amp\Parallel\{ StatusError, Strand } ;
2016-08-23 16:47:40 -05:00
use Amp\Parallel\Worker\Internal\{ Job, TaskResult };
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 {
2016-08-23 16:47:40 -05:00
/** @var \Amp\Parallel\Strand */
2015-12-04 23:50:32 -06:00
private $context;
/** @var bool */
2015-12-04 23:50:32 -06:00
private $shutdown = false;
/** @var \Amp\Deferred[] */
private $jobQueue = [];
/** @var callable */
private $when;
2015-12-04 23:50:32 -06:00
/**
2016-08-23 16:47:40 -05:00
* @param \Amp\Parallel\Strand $strand
2015-12-04 23:50:32 -06:00
*/
2016-08-18 11:04:48 -05:00
public function __construct(Strand $strand) {
2015-12-21 11:04:51 -06: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)) {
2017-03-21 18:45:23 -05:00
$this->context->receive()->onResolve($this->when);
}
2016-11-14 17:43:44 -06:00
$deferred->resolve($data->promise());
};
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 empty($this->jobQueue);
2015-12-04 23:50:32 -06:00
}
/**
* {@inheritdoc}
*/
2016-08-18 11:04:48 -05:00
public function start() {
2015-12-04 23:50:32 -06:00
$this->context->start();
}
/**
* {@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->context->isRunning()) {
throw new StatusError('The worker has not been started.');
}
2016-08-18 11:04:48 -05:00
2015-12-04 23:50:32 -06:00
if ($this->shutdown) {
throw new StatusError('The worker has been shut down.');
}
2016-08-18 11:04:48 -05:00
return new Coroutine($this->doEnqueue($task));
}
2016-08-18 11:04:48 -05:00
/**
* @coroutine
*
2016-08-23 16:47:40 -05:00
* @param \Amp\Parallel\Worker\Task $task
2016-08-18 11:04:48 -05:00
*
* @return \Generator
2016-08-23 16:47:40 -05:00
* @throws \Amp\Parallel\StatusError
* @throws \Amp\Parallel\Worker\TaskException
* @throws \Amp\Parallel\Worker\TaskError
* @throws \Amp\Parallel\Worker\WorkerException
2016-08-18 11:04:48 -05:00
*/
private function doEnqueue(Task $task): \Generator {
if (empty($this->jobQueue)) {
2017-03-21 18:45:23 -05:00
$this->context->receive()->onResolve($this->when);
2015-12-04 23:50:32 -06:00
}
2015-12-16 11:13:06 -06:00
try {
$job = new Job($task);
$this->jobQueue[$job->getId()] = $deferred = new Deferred;
yield $this->context->send($job);
2016-01-25 23:02:22 -06:00
} catch (\Throwable $exception) {
$this->kill();
throw new WorkerException('Sending the task to the worker failed.', $exception);
2015-12-04 23:50:32 -06:00
}
2016-11-14 17:43:44 -06:00
return yield $deferred->promise();
2016-08-18 11:04:48 -05:00
}
/**
* {@inheritdoc}
*/
2016-11-14 17:43:44 -06:00
public function shutdown(): Promise {
if (!$this->context->isRunning() || $this->shutdown) {
throw new StatusError('The worker is not running.');
}
2016-08-18 11:04:48 -05:00
return new Coroutine($this->doShutdown());
}
2015-12-04 23:50:32 -06:00
/**
* {@inheritdoc}
*/
2016-08-18 11:04:48 -05:00
private function doShutdown(): \Generator {
2015-12-04 23:50:32 -06:00
$this->shutdown = true;
// If a task is currently running, wait for it to finish.
2017-05-17 23:28:18 -05:00
yield Promise\any(\array_map(function (Deferred $deferred): Promise {
return $deferred->promise();
}, $this->jobQueue));
2016-08-18 11:04:48 -05:00
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() {
$this->cancelPending();
2015-12-04 23:50:32 -06:00
$this->context->kill();
}
/**
* Cancels all pending tasks.
*/
2016-08-18 11:04:48 -05: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-04 23:50:32 -06:00
}
}