2015-12-04 23:50:32 -06:00
|
|
|
<?php
|
|
|
|
namespace Icicle\Concurrent\Worker;
|
|
|
|
|
2015-12-11 18:15:15 -06:00
|
|
|
use Icicle\Awaitable\Delayed;
|
2015-12-21 11:04:51 -06:00
|
|
|
use Icicle\Concurrent\Strand;
|
2016-01-23 00:00:56 -06:00
|
|
|
use Icicle\Concurrent\Exception\{StatusError, WorkerException};
|
2015-12-04 23:50:32 -06:00
|
|
|
use Icicle\Concurrent\Worker\Internal\TaskFailure;
|
|
|
|
|
2015-12-11 18:15:15 -06:00
|
|
|
/**
|
|
|
|
* Base class for most common types of task workers.
|
|
|
|
*/
|
2015-12-04 23:50:32 -06:00
|
|
|
abstract class AbstractWorker implements Worker
|
|
|
|
{
|
|
|
|
/**
|
2015-12-21 11:04:51 -06:00
|
|
|
* @var \Icicle\Concurrent\Strand
|
2015-12-04 23:50:32 -06:00
|
|
|
*/
|
|
|
|
private $context;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $idle = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $shutdown = false;
|
|
|
|
|
2015-12-11 22:44:25 -06:00
|
|
|
/**
|
|
|
|
* @var \Icicle\Awaitable\Delayed
|
|
|
|
*/
|
|
|
|
private $activeDelayed;
|
|
|
|
|
2015-12-11 18:15:15 -06:00
|
|
|
/**
|
|
|
|
* @var \SplQueue
|
|
|
|
*/
|
|
|
|
private $busyQueue;
|
|
|
|
|
2015-12-11 22:44:25 -06:00
|
|
|
|
2015-12-04 23:50:32 -06:00
|
|
|
/**
|
2015-12-21 11:04:51 -06:00
|
|
|
* @param \Icicle\Concurrent\Strand $strand
|
2015-12-04 23:50:32 -06:00
|
|
|
*/
|
2015-12-21 11:04:51 -06:00
|
|
|
public function __construct(Strand $strand)
|
2015-12-04 23:50:32 -06:00
|
|
|
{
|
2015-12-21 11:04:51 -06:00
|
|
|
$this->context = $strand;
|
2015-12-11 18:15:15 -06:00
|
|
|
$this->busyQueue = new \SplQueue();
|
2015-12-04 23:50:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-01-23 00:00:56 -06:00
|
|
|
public function isRunning(): bool
|
2015-12-04 23:50:32 -06:00
|
|
|
{
|
|
|
|
return $this->context->isRunning();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-01-23 00:00:56 -06:00
|
|
|
public function isIdle(): bool
|
2015-12-04 23:50:32 -06:00
|
|
|
{
|
|
|
|
return $this->idle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function start()
|
|
|
|
{
|
|
|
|
$this->context->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-01-23 00:00:56 -06:00
|
|
|
public function enqueue(Task $task): \Generator
|
2015-12-04 23:50:32 -06:00
|
|
|
{
|
|
|
|
if (!$this->context->isRunning()) {
|
|
|
|
throw new StatusError('The worker has not been started.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->shutdown) {
|
2015-12-11 18:15:15 -06:00
|
|
|
throw new StatusError('The worker has been shut down.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the worker is currently busy, store the task in a busy queue.
|
|
|
|
if (!$this->idle) {
|
|
|
|
$delayed = new Delayed();
|
|
|
|
$this->busyQueue->enqueue($delayed);
|
|
|
|
yield $delayed;
|
2015-12-04 23:50:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->idle = false;
|
2015-12-11 22:44:25 -06:00
|
|
|
$this->activeDelayed = new Delayed();
|
2015-12-04 23:50:32 -06:00
|
|
|
|
2015-12-16 11:13:06 -06:00
|
|
|
try {
|
2016-01-23 00:00:56 -06:00
|
|
|
yield from $this->context->send($task);
|
2015-12-16 11:13:06 -06:00
|
|
|
|
2016-01-23 00:00:56 -06:00
|
|
|
$result = yield from $this->context->receive();
|
2015-12-16 11:13:06 -06:00
|
|
|
} finally {
|
|
|
|
$this->idle = true;
|
|
|
|
$this->activeDelayed->resolve();
|
|
|
|
|
|
|
|
// We're no longer busy at the moment, so dequeue a waiting task.
|
|
|
|
if (!$this->busyQueue->isEmpty()) {
|
|
|
|
$this->busyQueue->dequeue()->resolve();
|
|
|
|
}
|
2015-12-11 18:15:15 -06:00
|
|
|
}
|
|
|
|
|
2015-12-04 23:50:32 -06:00
|
|
|
if ($result instanceof TaskFailure) {
|
|
|
|
throw $result->getException();
|
|
|
|
}
|
|
|
|
|
2016-01-23 00:00:56 -06:00
|
|
|
return $result;
|
2015-12-04 23:50:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-01-23 00:00:56 -06:00
|
|
|
public function shutdown(): \Generator
|
2015-12-04 23:50:32 -06:00
|
|
|
{
|
|
|
|
if (!$this->context->isRunning() || $this->shutdown) {
|
|
|
|
throw new StatusError('The worker is not running.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->shutdown = true;
|
|
|
|
|
2015-12-11 18:15:15 -06:00
|
|
|
// Cancel any waiting tasks.
|
2015-12-11 22:44:25 -06:00
|
|
|
$this->cancelPending();
|
|
|
|
|
|
|
|
// If a task is currently running, wait for it to finish.
|
|
|
|
if (!$this->idle) {
|
|
|
|
yield $this->activeDelayed;
|
2015-12-11 18:15:15 -06:00
|
|
|
}
|
2015-12-11 22:44:25 -06:00
|
|
|
|
2016-01-23 00:00:56 -06:00
|
|
|
yield from $this->context->send(0);
|
|
|
|
return yield from $this->context->join();
|
2015-12-04 23:50:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function kill()
|
|
|
|
{
|
2015-12-11 22:44:25 -06:00
|
|
|
$this->cancelPending();
|
2015-12-04 23:50:32 -06:00
|
|
|
$this->context->kill();
|
2015-12-11 22:44:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancels all pending tasks.
|
|
|
|
*/
|
|
|
|
private function cancelPending()
|
|
|
|
{
|
2016-01-14 23:37:02 -06:00
|
|
|
if (!$this->busyQueue->isEmpty()) {
|
|
|
|
$exception = new WorkerException('Worker was shut down.');
|
2015-12-11 18:15:15 -06:00
|
|
|
|
2016-01-14 23:37:02 -06:00
|
|
|
do {
|
|
|
|
$this->busyQueue->dequeue()->cancel($exception);
|
|
|
|
} while (!$this->busyQueue->isEmpty());
|
2015-12-11 18:15:15 -06:00
|
|
|
}
|
2015-12-04 23:50:32 -06:00
|
|
|
}
|
|
|
|
}
|