Skip to content

Commit

Permalink
Moved RestInterval to Worker::do() argument
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Aug 29, 2020
1 parent ff262f7 commit 12c59aa
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 29 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "0.7-dev"
"dev-master": "0.8-dev"
}
}
}
48 changes: 48 additions & 0 deletions src/RestInterval.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace HappyInc\Worker;

/**
* @psalm-immutable
*/
final class RestInterval
{
/**
* @var int
*/
public $microseconds;

private function __construct(int $microseconds)
{
$this->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);
}
}
15 changes: 3 additions & 12 deletions src/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -56,7 +47,7 @@ public function do(callable $job): WorkerStopped
break;
}

sleep($this->restIntervalSeconds);
usleep($restInterval->microseconds);
++$jobIndex;
}

Expand Down
17 changes: 1 addition & 16 deletions src/WorkerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ final class WorkerBuilder
*/
private $eventDispatcher;

/**
* @psalm-var positive-int
*/
private $restIntervalSeconds = 1;

/**
* @var int|null
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 12c59aa

Please sign in to comment.