diff --git a/composer.json b/composer.json index c4ebd44..bce750a 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,7 @@ }, "extra": { "branch-alias": { - "dev-master": "0.7-dev" + "dev-master": "0.8-dev" } } } diff --git a/src/RestInterval.php b/src/RestInterval.php new file mode 100644 index 0000000..2212d22 --- /dev/null +++ b/src/RestInterval.php @@ -0,0 +1,48 @@ +microseconds = $microseconds; + } + + public static function fromMicroseconds(int $microseconds): self + { + if ($microseconds < 0) { + throw new \InvalidArgumentException(sprintf('Rest interval must be zero or positive.')); + } + + return new self($microseconds); + } + + public static function fromMilliseconds(int $milliseconds): self + { + if ($milliseconds < 0) { + throw new \InvalidArgumentException(sprintf('Rest interval must be zero or positive.')); + } + + return new self($milliseconds * 1000); + } + + public static function fromSeconds(int $seconds): self + { + if ($seconds < 0) { + throw new \InvalidArgumentException(sprintf('Rest interval must be zero or positive.')); + } + + return new self($seconds * 1000 * 1000); + } +} diff --git a/src/Worker.php b/src/Worker.php index 5b806fe..0a5e2ae 100644 --- a/src/Worker.php +++ b/src/Worker.php @@ -13,24 +13,15 @@ final class Worker */ private $eventDispatcher; - /** - * @psalm-var positive-int - */ - private $restIntervalSeconds; - - /** - * @psalm-param positive-int $restIntervalSeconds - */ - public function __construct(?EventDispatcherInterface $eventDispatcher = null, int $restIntervalSeconds = 1) + public function __construct(?EventDispatcherInterface $eventDispatcher = null) { $this->eventDispatcher = $eventDispatcher ?? new NullEventDispatcher(); - $this->restIntervalSeconds = $restIntervalSeconds; } /** * @psalm-param callable(WorkerJobContext): void $job */ - public function do(callable $job): WorkerStopped + public function do(callable $job, RestInterval $restInterval): WorkerStopped { $this->eventDispatcher->dispatch(new WorkerStarted()); @@ -56,7 +47,7 @@ public function do(callable $job): WorkerStopped break; } - sleep($this->restIntervalSeconds); + usleep($restInterval->microseconds); ++$jobIndex; } diff --git a/src/WorkerBuilder.php b/src/WorkerBuilder.php index 4771ea5..6e0df18 100644 --- a/src/WorkerBuilder.php +++ b/src/WorkerBuilder.php @@ -24,11 +24,6 @@ final class WorkerBuilder */ private $eventDispatcher; - /** - * @psalm-var positive-int - */ - private $restIntervalSeconds = 1; - /** * @var int|null */ @@ -110,16 +105,6 @@ public function setEventDispatcher(?EventDispatcherInterface $eventDispatcher): return $this; } - /** - * @psalm-param positive-int $restIntervalSeconds - */ - public function setRestIntervalSeconds(int $restIntervalSeconds): self - { - $this->restIntervalSeconds = $restIntervalSeconds; - - return $this; - } - public function setMemorySoftLimitBytes(?int $memorySoftLimitBytes): self { $this->memorySoftLimitBytes = $memorySoftLimitBytes; @@ -201,6 +186,6 @@ public function build(): Worker } /** @psalm-suppress ArgumentTypeCoercion */ - return new Worker(new EventDispatcher($listeners, $this->eventDispatcher), $this->restIntervalSeconds); + return new Worker(new EventDispatcher($listeners, $this->eventDispatcher)); } }