2015-12-05 06:50:32 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
namespace Amp\Concurrent\Worker;
|
|
|
|
|
|
|
|
use Amp\Concurrent\{ StatusError, Strand, WorkerException} ;
|
|
|
|
use Amp\Concurrent\Worker\Internal\TaskFailure;
|
|
|
|
use Amp\Coroutine;
|
|
|
|
use Amp\Deferred;
|
|
|
|
use Interop\Async\Awaitable;
|
2015-12-05 06:50:32 +01:00
|
|
|
|
2015-12-12 01:15:15 +01:00
|
|
|
/**
|
|
|
|
* Base class for most common types of task workers.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
abstract class AbstractWorker implements Worker {
|
2015-12-05 06:50:32 +01:00
|
|
|
/**
|
2016-08-18 18:04:48 +02:00
|
|
|
* @var \Amp\Concurrent\Strand
|
2015-12-05 06:50:32 +01:00
|
|
|
*/
|
|
|
|
private $context;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $shutdown = false;
|
|
|
|
|
2015-12-12 05:44:25 +01:00
|
|
|
/**
|
2016-08-18 18:04:48 +02:00
|
|
|
* @var \Amp\Coroutine
|
2015-12-12 05:44:25 +01:00
|
|
|
*/
|
2016-01-26 05:59:02 +01:00
|
|
|
private $active;
|
2015-12-12 05:44:25 +01:00
|
|
|
|
2015-12-12 01:15:15 +01:00
|
|
|
/**
|
|
|
|
* @var \SplQueue
|
|
|
|
*/
|
|
|
|
private $busyQueue;
|
|
|
|
|
2015-12-05 06:50:32 +01:00
|
|
|
/**
|
2016-08-18 18:04:48 +02:00
|
|
|
* @param \Amp\Concurrent\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;
|
2016-08-18 18:04:48 +02:00
|
|
|
$this->busyQueue = new \SplQueue;
|
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 {
|
2016-01-26 05:59:02 +01:00
|
|
|
return null === $this->active;
|
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) {
|
2015-12-12 01:15:15 +01:00
|
|
|
throw new StatusError('The worker has been shut down.');
|
|
|
|
}
|
2016-08-18 18:04:48 +02:00
|
|
|
|
|
|
|
return new Coroutine($this->doEnqueue($task));
|
|
|
|
}
|
2015-12-12 01:15:15 +01:00
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
/**
|
|
|
|
* @coroutine
|
|
|
|
*
|
|
|
|
* @param \Amp\Concurrent\Worker\Task $task
|
|
|
|
*
|
|
|
|
* @return \Generator
|
|
|
|
* @throws \Amp\Concurrent\StatusError
|
|
|
|
* @throws \Amp\Concurrent\TaskException
|
|
|
|
* @throws \Amp\Concurrent\WorkerException
|
|
|
|
*/
|
|
|
|
private function doEnqueue(Task $task): \Generator {
|
2015-12-12 01:15:15 +01:00
|
|
|
// If the worker is currently busy, store the task in a busy queue.
|
2016-01-26 05:59:02 +01:00
|
|
|
if (null !== $this->active) {
|
2016-08-18 18:04:48 +02:00
|
|
|
$deferred = new Deferred;
|
|
|
|
$this->busyQueue->enqueue($deferred);
|
|
|
|
yield $deferred->getAwaitable();
|
2015-12-05 06:50:32 +01:00
|
|
|
}
|
|
|
|
|
2016-01-26 05:59:02 +01:00
|
|
|
$this->active = new Coroutine($this->send($task));
|
2015-12-05 06:50:32 +01:00
|
|
|
|
2015-12-16 18:13:06 +01:00
|
|
|
try {
|
2016-01-26 06:02:22 +01:00
|
|
|
$result = yield $this->active;
|
|
|
|
} catch (\Throwable $exception) {
|
2016-01-26 05:59:02 +01:00
|
|
|
$this->kill();
|
|
|
|
throw new WorkerException('Sending the task to the worker failed.', $exception);
|
2015-12-16 18:13:06 +01:00
|
|
|
} finally {
|
2016-01-26 05:59:02 +01:00
|
|
|
$this->active = null;
|
|
|
|
}
|
2015-12-16 18:13:06 +01:00
|
|
|
|
2016-01-26 05:59:02 +01:00
|
|
|
// We're no longer busy at the moment, so dequeue a waiting task.
|
|
|
|
if (!$this->busyQueue->isEmpty()) {
|
|
|
|
$this->busyQueue->dequeue()->resolve();
|
2015-12-12 01:15:15 +01:00
|
|
|
}
|
|
|
|
|
2015-12-05 06:50:32 +01:00
|
|
|
if ($result instanceof TaskFailure) {
|
|
|
|
throw $result->getException();
|
|
|
|
}
|
|
|
|
|
2016-01-23 07:00:56 +01:00
|
|
|
return $result;
|
2015-12-05 06:50:32 +01:00
|
|
|
}
|
|
|
|
|
2016-01-26 05:59:02 +01:00
|
|
|
/**
|
|
|
|
* @coroutine
|
|
|
|
*
|
2016-08-18 18:04:48 +02:00
|
|
|
* @param \Amp\Concurrent\Worker\Task $task
|
2016-01-26 05:59:02 +01:00
|
|
|
*
|
|
|
|
* @return \Generator
|
|
|
|
*
|
|
|
|
* @resolve mixed
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
private function send(Task $task): \Generator {
|
|
|
|
yield $this->context->send($task);
|
|
|
|
return yield $this->context->receive();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function shutdown(): Awaitable {
|
|
|
|
return new Coroutine($this->doShutdown());
|
2016-01-26 05:59:02 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
if (!$this->context->isRunning() || $this->shutdown) {
|
|
|
|
throw new StatusError('The worker is not running.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->shutdown = true;
|
|
|
|
|
2015-12-12 01:15:15 +01:00
|
|
|
// Cancel any waiting tasks.
|
2015-12-12 05:44:25 +01:00
|
|
|
$this->cancelPending();
|
|
|
|
|
|
|
|
// If a task is currently running, wait for it to finish.
|
2016-01-26 05:59:02 +01:00
|
|
|
if (null !== $this->active) {
|
|
|
|
try {
|
|
|
|
yield $this->active;
|
2016-01-26 06:02:22 +01:00
|
|
|
} catch (\Throwable $exception) {
|
2016-01-26 05:59:02 +01:00
|
|
|
// Ignore failure in this context.
|
|
|
|
}
|
2015-12-12 01:15:15 +01:00
|
|
|
}
|
2015-12-12 05:44:25 +01:00
|
|
|
|
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() {
|
2015-12-12 05:44:25 +01:00
|
|
|
$this->cancelPending();
|
2015-12-05 06:50:32 +01:00
|
|
|
$this->context->kill();
|
2015-12-12 05:44:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancels all pending tasks.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
private function cancelPending() {
|
2016-01-15 06:37:02 +01:00
|
|
|
if (!$this->busyQueue->isEmpty()) {
|
|
|
|
$exception = new WorkerException('Worker was shut down.');
|
2015-12-12 01:15:15 +01:00
|
|
|
|
2016-01-15 06:37:02 +01:00
|
|
|
do {
|
2016-08-18 18:04:48 +02:00
|
|
|
$this->busyQueue->dequeue()->fail($exception);
|
2016-01-15 06:37:02 +01:00
|
|
|
} while (!$this->busyQueue->isEmpty());
|
2015-12-12 01:15:15 +01:00
|
|
|
}
|
2015-12-05 06:50:32 +01:00
|
|
|
}
|
|
|
|
}
|