1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-03 10:07:49 +01:00
parallel/lib/Worker/Internal/PooledWorker.php

76 lines
1.5 KiB
PHP
Raw Normal View History

2016-08-22 06:40:48 +02:00
<?php declare(strict_types = 1);
2016-08-18 18:04:48 +02:00
namespace Amp\Concurrent\Worker\Internal;
2016-08-18 18:04:48 +02:00
use Amp\Concurrent\Worker\{ Task, Worker };
use Interop\Async\Awaitable;
class PooledWorker implements Worker {
/**
* @var callable
*/
private $push;
/**
2016-08-18 18:04:48 +02:00
* @var \Amp\Concurrent\Worker\Worker
*/
private $worker;
/**
2016-08-18 18:04:48 +02:00
* @param \Amp\Concurrent\Worker\Worker $worker
* @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) {
$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);
}
/**
* {@inheritdoc}
*/
2016-08-18 18:04:48 +02:00
public function isRunning(): bool {
return $this->worker->isRunning();
}
/**
* {@inheritdoc}
*/
2016-08-18 18:04:48 +02:00
public function isIdle(): bool {
return $this->worker->isIdle();
}
/**
* {@inheritdoc}
*/
2016-08-18 18:04:48 +02:00
public function start() {
$this->worker->start();
}
/**
* {@inheritdoc}
*/
2016-08-18 18:04:48 +02:00
public function enqueue(Task $task): Awaitable {
return $this->worker->enqueue($task);
}
/**
* {@inheritdoc}
*/
2016-08-18 18:04:48 +02:00
public function shutdown(): Awaitable {
return $this->worker->shutdown();
}
/**
* {@inheritdoc}
*/
2016-08-18 18:04:48 +02:00
public function kill() {
$this->worker->kill();
}
}