File tree Expand file tree Collapse file tree 4 files changed +53
-29
lines changed
Expand file tree Collapse file tree 4 files changed +53
-29
lines changed Original file line number Diff line number Diff line change 3737 },
3838 "extra" : {
3939 "branch-alias" : {
40- "dev-master" : " 0.7 -dev"
40+ "dev-master" : " 0.8 -dev"
4141 }
4242 }
4343}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments