1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-11 16:49:51 +01:00
parallel/src/Worker/Internal/TaskRunner.php

67 lines
1.3 KiB
PHP
Raw Normal View History

2015-08-27 16:10:08 +02:00
<?php
namespace Icicle\Concurrent\Worker\Internal;
use Icicle\Concurrent\Sync\ChannelInterface;
2015-09-10 06:29:41 +02:00
use Icicle\Concurrent\Worker\Environment;
2015-08-27 16:10:08 +02:00
use Icicle\Concurrent\Worker\TaskInterface;
class TaskRunner
{
/**
* @var bool
*/
private $idle = true;
/**
* @var \Icicle\Concurrent\Sync\ChannelInterface
2015-08-27 16:10:08 +02:00
*/
private $channel;
2015-09-10 06:29:41 +02:00
/**
* @var \Icicle\Concurrent\Worker\Environment
*/
private $environment;
public function __construct(ChannelInterface $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
}
/**
* @coroutine
*
* @return \Generator
*/
public function run()
{
$task = (yield $this->channel->receive());
2015-08-27 16:10:08 +02:00
while ($task instanceof TaskInterface) {
$this->idle = false;
try {
2015-09-10 06:29:41 +02:00
$result = (yield $task->run($this->environment));
2015-08-27 16:10:08 +02:00
} catch (\Exception $exception) {
$result = new TaskFailure($exception);
}
yield $this->channel->send($result);
$this->idle = true;
$task = (yield $this->channel->receive());
2015-08-27 16:10:08 +02:00
}
yield $task;
}
/**
* @return bool
*/
public function isIdle()
{
return $this->idle;
}
}