Skip to content

Commit 12c59aa

Browse files
committed
Moved RestInterval to Worker::do() argument
1 parent ff262f7 commit 12c59aa

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
},
3838
"extra": {
3939
"branch-alias": {
40-
"dev-master": "0.7-dev"
40+
"dev-master": "0.8-dev"
4141
}
4242
}
4343
}

src/RestInterval.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HappyInc\Worker;
6+
7+
/**
8+
* @psalm-immutable
9+
*/
10+
final class RestInterval
11+
{
12+
/**
13+
* @var int
14+
*/
15+
public $microseconds;
16+
17+
private function __construct(int $microseconds)
18+
{
19+
$this->microseconds = $microseconds;
20+
}
21+
22+
public static function fromMicroseconds(int $microseconds): self
23+
{
24+
if ($microseconds < 0) {
25+
throw new \InvalidArgumentException(sprintf('Rest interval must be zero or positive.'));
26+
}
27+
28+
return new self($microseconds);
29+
}
30+
31+
public static function fromMilliseconds(int $milliseconds): self
32+
{
33+
if ($milliseconds < 0) {
34+
throw new \InvalidArgumentException(sprintf('Rest interval must be zero or positive.'));
35+
}
36+
37+
return new self($milliseconds * 1000);
38+
}
39+
40+
public static function fromSeconds(int $seconds): self
41+
{
42+
if ($seconds < 0) {
43+
throw new \InvalidArgumentException(sprintf('Rest interval must be zero or positive.'));
44+
}
45+
46+
return new self($seconds * 1000 * 1000);
47+
}
48+
}

src/Worker.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,15 @@ final class Worker
1313
*/
1414
private $eventDispatcher;
1515

16-
/**
17-
* @psalm-var positive-int
18-
*/
19-
private $restIntervalSeconds;
20-
21-
/**
22-
* @psalm-param positive-int $restIntervalSeconds
23-
*/
24-
public function __construct(?EventDispatcherInterface $eventDispatcher = null, int $restIntervalSeconds = 1)
16+
public function __construct(?EventDispatcherInterface $eventDispatcher = null)
2517
{
2618
$this->eventDispatcher = $eventDispatcher ?? new NullEventDispatcher();
27-
$this->restIntervalSeconds = $restIntervalSeconds;
2819
}
2920

3021
/**
3122
* @psalm-param callable(WorkerJobContext): void $job
3223
*/
33-
public function do(callable $job): WorkerStopped
24+
public function do(callable $job, RestInterval $restInterval): WorkerStopped
3425
{
3526
$this->eventDispatcher->dispatch(new WorkerStarted());
3627

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

59-
sleep($this->restIntervalSeconds);
50+
usleep($restInterval->microseconds);
6051
++$jobIndex;
6152
}
6253

src/WorkerBuilder.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ final class WorkerBuilder
2424
*/
2525
private $eventDispatcher;
2626

27-
/**
28-
* @psalm-var positive-int
29-
*/
30-
private $restIntervalSeconds = 1;
31-
3227
/**
3328
* @var int|null
3429
*/
@@ -110,16 +105,6 @@ public function setEventDispatcher(?EventDispatcherInterface $eventDispatcher):
110105
return $this;
111106
}
112107

113-
/**
114-
* @psalm-param positive-int $restIntervalSeconds
115-
*/
116-
public function setRestIntervalSeconds(int $restIntervalSeconds): self
117-
{
118-
$this->restIntervalSeconds = $restIntervalSeconds;
119-
120-
return $this;
121-
}
122-
123108
public function setMemorySoftLimitBytes(?int $memorySoftLimitBytes): self
124109
{
125110
$this->memorySoftLimitBytes = $memorySoftLimitBytes;
@@ -201,6 +186,6 @@ public function build(): Worker
201186
}
202187

203188
/** @psalm-suppress ArgumentTypeCoercion */
204-
return new Worker(new EventDispatcher($listeners, $this->eventDispatcher), $this->restIntervalSeconds);
189+
return new Worker(new EventDispatcher($listeners, $this->eventDispatcher));
205190
}
206191
}

0 commit comments

Comments
 (0)