2016-08-21 23:40:48 -05:00
|
|
|
<?php declare(strict_types = 1);
|
2015-08-27 09:10:08 -05:00
|
|
|
|
2016-08-23 16:47:40 -05:00
|
|
|
namespace Amp\Parallel\Worker\Internal;
|
2015-08-27 09:10:08 -05:00
|
|
|
|
2016-08-21 10:33:39 -05:00
|
|
|
use Amp\{ Coroutine, Failure, Success };
|
2016-08-23 16:47:40 -05:00
|
|
|
use Amp\Parallel\{ Sync\Channel, Worker\Environment };
|
2016-11-14 17:43:44 -06:00
|
|
|
use Interop\Async\Promise;
|
2016-08-18 11:04:48 -05:00
|
|
|
|
|
|
|
class TaskRunner {
|
2016-08-26 10:10:03 -05:00
|
|
|
/** @var \Amp\Parallel\Sync\Channel */
|
2015-08-27 09:10:08 -05:00
|
|
|
private $channel;
|
|
|
|
|
2016-08-26 10:10:03 -05:00
|
|
|
/** @var \Amp\Parallel\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.
|
|
|
|
*
|
2016-11-14 17:43:44 -06:00
|
|
|
* @return \Interop\Async\Promise
|
2016-08-18 11:04:48 -05:00
|
|
|
*/
|
2016-11-14 17:43:44 -06:00
|
|
|
public function run(): Promise {
|
2016-08-18 11:04:48 -05:00
|
|
|
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 {
|
2016-08-21 10:33:39 -05:00
|
|
|
$job = yield $this->channel->receive();
|
2015-08-27 09:10:08 -05:00
|
|
|
|
2016-08-21 10:33:39 -05:00
|
|
|
while ($job instanceof Job) {
|
|
|
|
$task = $job->getTask();
|
|
|
|
|
2015-08-27 09:10:08 -05:00
|
|
|
try {
|
2016-08-18 11:04:48 -05:00
|
|
|
$result = $task->run($this->environment);
|
|
|
|
|
|
|
|
if ($result instanceof \Generator) {
|
|
|
|
$result = new Coroutine($result);
|
|
|
|
}
|
|
|
|
|
2016-11-14 17:43:44 -06:00
|
|
|
if (!$result instanceof Promise) {
|
2016-08-21 10:33:39 -05:00
|
|
|
$result = new Success($result);
|
2016-08-18 11:04:48 -05:00
|
|
|
}
|
2016-01-23 00:00:56 -06:00
|
|
|
} catch (\Throwable $exception) {
|
2016-08-21 10:33:39 -05:00
|
|
|
$result = new Failure($exception);
|
2015-08-27 09:10:08 -05:00
|
|
|
}
|
2016-08-21 10:33:39 -05:00
|
|
|
|
|
|
|
$result->when(function ($exception, $value) use ($job) {
|
|
|
|
if ($exception) {
|
|
|
|
$result = new TaskFailure($job->getId(), $exception);
|
|
|
|
} else {
|
|
|
|
$result = new TaskSuccess($job->getId(), $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->channel->send($result);
|
|
|
|
});
|
2015-08-27 09:10:08 -05:00
|
|
|
|
2016-08-21 10:33:39 -05:00
|
|
|
$job = yield $this->channel->receive();
|
2015-08-27 09:10:08 -05:00
|
|
|
}
|
|
|
|
|
2016-08-21 10:33:39 -05:00
|
|
|
return $job;
|
2015-08-27 09:10:08 -05:00
|
|
|
}
|
|
|
|
}
|