2020-12-11 01:25:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Toalett\Multiprocessing;
|
|
|
|
|
|
|
|
use React\EventLoop\Factory;
|
|
|
|
use React\EventLoop\LoopInterface;
|
2020-12-12 02:11:05 +01:00
|
|
|
use Toalett\Multiprocessing\Task\Interval;
|
2020-12-11 01:25:38 +01:00
|
|
|
|
|
|
|
class ContextBuilder
|
|
|
|
{
|
2020-12-12 02:11:05 +01:00
|
|
|
private ?LoopInterface $loop = null;
|
|
|
|
private ?ConcurrencyLimit $limit = null;
|
|
|
|
private ?Workers $workers = null;
|
|
|
|
private ?Interval $garbageCollectionInterval = null;
|
|
|
|
private ?Interval $cleanupInterval = null;
|
|
|
|
|
|
|
|
public static function create(): self
|
|
|
|
{
|
|
|
|
return new self();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withEventLoop(LoopInterface $loop): self
|
|
|
|
{
|
|
|
|
$instance = clone $this;
|
|
|
|
$instance->loop = $loop;
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withLimit(ConcurrencyLimit $limit): self
|
|
|
|
{
|
|
|
|
$instance = clone $this;
|
|
|
|
$instance->limit = $limit;
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withWorkers(Workers $workers): self
|
|
|
|
{
|
|
|
|
$instance = clone $this;
|
|
|
|
$instance->workers = $workers;
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withGarbageCollectionInterval(Interval $interval): self
|
|
|
|
{
|
|
|
|
$instance = clone $this;
|
|
|
|
$instance->garbageCollectionInterval = $interval;
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withCleanupInterval(Interval $interval): self
|
|
|
|
{
|
|
|
|
$instance = clone $this;
|
|
|
|
$instance->cleanupInterval = $interval;
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function build(): Context
|
|
|
|
{
|
|
|
|
return new Context(
|
|
|
|
$this->loop ?? Factory::create(),
|
|
|
|
$this->limit ?? ConcurrencyLimit::unlimited(),
|
|
|
|
$this->workers,
|
|
|
|
$this->cleanupInterval,
|
|
|
|
$this->garbageCollectionInterval
|
|
|
|
);
|
|
|
|
}
|
2020-12-11 01:25:38 +01:00
|
|
|
}
|