2015-08-27 09:10:08 -05:00
|
|
|
<?php
|
|
|
|
|
2016-08-18 11:04:48 -05:00
|
|
|
namespace Amp\Concurrent\Worker\Internal;
|
2015-08-27 09:10:08 -05:00
|
|
|
|
2016-08-18 11:04:48 -05:00
|
|
|
use Amp\Concurrent\Sync\Channel;
|
|
|
|
use Amp\Concurrent\Worker\{ Environment, Task };
|
|
|
|
use Amp\Coroutine;
|
|
|
|
use Interop\Async\Awaitable;
|
|
|
|
|
|
|
|
class TaskRunner {
|
2015-08-27 09:10:08 -05:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $idle = true;
|
|
|
|
|
|
|
|
/**
|
2016-08-18 11:04:48 -05:00
|
|
|
* @var \Amp\Concurrent\Sync\Channel
|
2015-08-27 09:10:08 -05:00
|
|
|
*/
|
|
|
|
private $channel;
|
|
|
|
|
2015-09-09 23:29:41 -05:00
|
|
|
/**
|
2016-08-18 11:04:48 -05:00
|
|
|
* @var \Amp\Concurrent\Worker\Environment
|
2015-09-09 23:29:41 -05:00
|
|
|
*/
|
|
|
|
private $environment;
|
|
|
|
|
2016-08-18 11:04:48 -05:00
|
|
|
public function __construct(Channel $channel, Environment $environment) {
|
2015-08-27 09:10:08 -05:00
|
|
|
$this->channel = $channel;
|
2015-09-09 23:29:41 -05:00
|
|
|
$this->environment = $environment;
|
2015-08-27 09:10:08 -05:00
|
|
|
}
|
2016-08-18 11:04:48 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs the task runner, receiving tasks from the parent and sending the result of those tasks.
|
|
|
|
*
|
|
|
|
* @return \Interop\Async\Awaitable
|
|
|
|
*/
|
|
|
|
public function run(): Awaitable {
|
|
|
|
return new Coroutine($this->execute());
|
|
|
|
}
|
|
|
|
|
2015-08-27 09:10:08 -05:00
|
|
|
/**
|
|
|
|
* @coroutine
|
|
|
|
*
|
|
|
|
* @return \Generator
|
|
|
|
*/
|
2016-08-18 11:04:48 -05:00
|
|
|
private function execute(): \Generator {
|
|
|
|
$task = yield $this->channel->receive();
|
2015-08-27 09:10:08 -05:00
|
|
|
|
2015-12-04 23:50:32 -06:00
|
|
|
while ($task instanceof Task) {
|
2015-08-27 09:10:08 -05:00
|
|
|
$this->idle = false;
|
|
|
|
|
|
|
|
try {
|
2016-08-18 11:04:48 -05:00
|
|
|
$result = $task->run($this->environment);
|
|
|
|
|
|
|
|
if ($result instanceof \Generator) {
|
|
|
|
$result = new Coroutine($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result instanceof Awaitable) {
|
|
|
|
$result = yield $result;
|
|
|
|
}
|
2016-01-23 00:00:56 -06:00
|
|
|
} catch (\Throwable $exception) {
|
2015-08-27 09:10:08 -05:00
|
|
|
$result = new TaskFailure($exception);
|
|
|
|
}
|
|
|
|
|
2016-08-18 11:04:48 -05:00
|
|
|
yield $this->channel->send($result);
|
2015-08-27 09:10:08 -05:00
|
|
|
|
|
|
|
$this->idle = true;
|
|
|
|
|
2016-08-18 11:04:48 -05:00
|
|
|
$task = yield $this->channel->receive();
|
2015-08-27 09:10:08 -05:00
|
|
|
}
|
|
|
|
|
2016-01-23 00:00:56 -06:00
|
|
|
return $task;
|
2015-08-27 09:10:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-08-18 11:04:48 -05:00
|
|
|
public function isIdle(): bool {
|
2015-08-27 09:10:08 -05:00
|
|
|
return $this->idle;
|
|
|
|
}
|
|
|
|
}
|