2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2016-01-15 00:05:31 +01:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
namespace Amp\Parallel\Worker\Internal;
|
2016-01-15 00:05:31 +01:00
|
|
|
|
2017-05-18 09:51:31 +02:00
|
|
|
use Amp\Parallel\Worker\Task;
|
|
|
|
use Amp\Parallel\Worker\Worker;
|
2017-03-16 23:03:59 +01:00
|
|
|
use Amp\Promise;
|
2016-08-18 18:04:48 +02:00
|
|
|
|
2017-07-28 06:49:20 +02:00
|
|
|
/** @internal */
|
2016-08-18 18:04:48 +02:00
|
|
|
class PooledWorker implements Worker {
|
2016-08-26 17:10:03 +02:00
|
|
|
/** @var callable */
|
2016-01-15 00:05:31 +01:00
|
|
|
private $push;
|
|
|
|
|
2016-08-26 17:10:03 +02:00
|
|
|
/** @var \Amp\Parallel\Worker\Worker */
|
2016-01-15 00:05:31 +01:00
|
|
|
private $worker;
|
|
|
|
|
|
|
|
/**
|
2016-08-23 23:47:40 +02:00
|
|
|
* @param \Amp\Parallel\Worker\Worker $worker
|
2016-01-15 00:05:31 +01:00
|
|
|
* @param callable $push Callable to push the worker back into the queue.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function __construct(Worker $worker, callable $push) {
|
2016-01-15 00:05:31 +01:00
|
|
|
$this->worker = $worker;
|
|
|
|
$this->push = $push;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Automatically pushes the worker back into the queue.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function __destruct() {
|
2016-01-23 07:00:56 +01:00
|
|
|
($this->push)($this->worker);
|
2016-01-15 00:05:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function isRunning(): bool {
|
2016-01-15 00:05:31 +01:00
|
|
|
return $this->worker->isRunning();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function isIdle(): bool {
|
2016-01-15 00:05:31 +01:00
|
|
|
return $this->worker->isIdle();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 00:43:44 +01:00
|
|
|
public function enqueue(Task $task): Promise {
|
2016-01-15 00:05:31 +01:00
|
|
|
return $this->worker->enqueue($task);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 00:43:44 +01:00
|
|
|
public function shutdown(): Promise {
|
2016-01-15 00:05:31 +01:00
|
|
|
return $this->worker->shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function kill() {
|
2016-01-15 00:05:31 +01:00
|
|
|
$this->worker->kill();
|
|
|
|
}
|
2017-05-18 09:51:31 +02:00
|
|
|
}
|