2016-01-15 00:05:31 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
namespace Amp\Concurrent\Worker\Internal;
|
2016-01-15 00:05:31 +01:00
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
use Amp\Concurrent\Worker\{ Task, Worker };
|
|
|
|
use Interop\Async\Awaitable;
|
|
|
|
|
|
|
|
class PooledWorker implements Worker {
|
2016-01-15 00:05:31 +01:00
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
private $push;
|
|
|
|
|
|
|
|
/**
|
2016-08-18 18:04:48 +02:00
|
|
|
* @var \Amp\Concurrent\Worker\Worker
|
2016-01-15 00:05:31 +01:00
|
|
|
*/
|
|
|
|
private $worker;
|
|
|
|
|
|
|
|
/**
|
2016-08-18 18:04:48 +02:00
|
|
|
* @param \Amp\Concurrent\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-08-18 18:04:48 +02:00
|
|
|
public function start() {
|
2016-01-15 00:05:31 +01:00
|
|
|
$this->worker->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function enqueue(Task $task): Awaitable {
|
2016-01-15 00:05:31 +01:00
|
|
|
return $this->worker->enqueue($task);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function shutdown(): Awaitable {
|
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();
|
|
|
|
}
|
|
|
|
}
|