2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2015-08-27 16:10:08 +02:00
|
|
|
|
2017-07-26 07:41:00 +02:00
|
|
|
namespace Amp\Parallel\Worker;
|
2015-08-27 16:10:08 +02:00
|
|
|
|
2017-05-18 09:51:31 +02:00
|
|
|
use Amp\Coroutine;
|
|
|
|
use Amp\Parallel\Sync\Channel;
|
2018-10-24 05:10:12 +02:00
|
|
|
use Amp\Parallel\Sync\SerializationException;
|
2017-03-16 23:03:59 +01:00
|
|
|
use Amp\Promise;
|
2017-05-20 00:13:42 +02:00
|
|
|
use function Amp\call;
|
2016-08-18 18:04:48 +02:00
|
|
|
|
2018-10-21 17:54:46 +02:00
|
|
|
final class TaskRunner
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2020-02-15 00:33:38 +01:00
|
|
|
/** @var Channel */
|
2015-08-27 16:10:08 +02:00
|
|
|
private $channel;
|
|
|
|
|
2020-02-15 00:33:38 +01:00
|
|
|
/** @var Environment */
|
2015-09-10 06:29:41 +02:00
|
|
|
private $environment;
|
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
public function __construct(Channel $channel, Environment $environment)
|
|
|
|
{
|
2015-08-27 16:10:08 +02:00
|
|
|
$this->channel = $channel;
|
2015-09-10 06:29:41 +02:00
|
|
|
$this->environment = $environment;
|
2015-08-27 16:10:08 +02:00
|
|
|
}
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
/**
|
|
|
|
* Runs the task runner, receiving tasks from the parent and sending the result of those tasks.
|
|
|
|
*
|
2017-03-16 23:03:59 +01:00
|
|
|
* @return \Amp\Promise
|
2016-08-18 18:04:48 +02:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function run(): Promise
|
|
|
|
{
|
2016-08-18 18:04:48 +02:00
|
|
|
return new Coroutine($this->execute());
|
|
|
|
}
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2015-08-27 16:10:08 +02:00
|
|
|
/**
|
|
|
|
* @coroutine
|
|
|
|
*
|
|
|
|
* @return \Generator
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
private function execute(): \Generator
|
|
|
|
{
|
2016-08-21 17:33:39 +02:00
|
|
|
$job = yield $this->channel->receive();
|
2015-08-27 16:10:08 +02:00
|
|
|
|
2017-07-26 07:41:00 +02:00
|
|
|
while ($job instanceof Internal\Job) {
|
2017-12-06 22:59:28 +01:00
|
|
|
try {
|
|
|
|
$result = yield call([$job->getTask(), "run"], $this->environment);
|
|
|
|
$result = new Internal\TaskSuccess($job->getId(), $result);
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
$result = new Internal\TaskFailure($job->getId(), $exception);
|
|
|
|
}
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2017-12-06 22:59:28 +01:00
|
|
|
$job = null; // Free memory from last job.
|
2015-08-27 16:10:08 +02:00
|
|
|
|
2018-10-24 05:10:12 +02:00
|
|
|
try {
|
|
|
|
yield $this->channel->send($result);
|
|
|
|
} catch (SerializationException $exception) {
|
|
|
|
// Could not serialize task result.
|
|
|
|
yield $this->channel->send(new Internal\TaskFailure($result->getId(), $exception));
|
|
|
|
}
|
2017-12-06 22:59:28 +01:00
|
|
|
|
|
|
|
$result = null; // Free memory from last result.
|
2017-11-10 17:32:01 +01:00
|
|
|
|
2016-08-21 17:33:39 +02:00
|
|
|
$job = yield $this->channel->receive();
|
2015-08-27 16:10:08 +02:00
|
|
|
}
|
|
|
|
|
2016-08-21 17:33:39 +02:00
|
|
|
return $job;
|
2015-08-27 16:10:08 +02:00
|
|
|
}
|
|
|
|
}
|