File tree Expand file tree Collapse file tree 3 files changed +12
-9
lines changed Expand file tree Collapse file tree 3 files changed +12
-9
lines changed Original file line number Diff line number Diff line change 1919
2020use function max ;
2121use function microtime ;
22+ use function round ;
2223use function usleep ;
2324
2425final class DefaultWorker implements Worker
@@ -32,6 +33,7 @@ public function __construct(
3233 ) {
3334 }
3435
36+ /** @param int $sleepTimer in milliseconds */
3537 public function run (int $ sleepTimer = 1000 ): void
3638 {
3739 $ this ->logger ?->debug('Worker starting ' );
@@ -41,11 +43,12 @@ public function run(int $sleepTimer = 1000): void
4143 while (!$ this ->shouldStop ) {
4244 $ this ->logger ?->debug('Worker starting job run ' );
4345
44- $ startTime = microtime (true );
46+ $ startTime = ( int ) round ( microtime (true ) * 1000 );
4547
4648 ($ this ->job )();
4749
48- $ ranTime = (int )(microtime (true ) - $ startTime );
50+ $ endTime = (int )round (microtime (true ) * 1000 );
51+ $ ranTime = $ endTime - $ startTime ;
4952
5053 $ this ->logger ?->debug('Worker finished job run ({ranTime}ms) ' , ['ranTime ' => $ ranTime ]);
5154
Original file line number Diff line number Diff line change 99use Psr \Log \LoggerInterface ;
1010use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
1111
12- use function microtime ;
12+ use function time ;
1313
1414final class StopWorkerOnTimeLimitListener implements EventSubscriberInterface
1515{
1616 private float $ endTime = 0 ;
1717
18- /** @param positive-int $timeLimitInSeconds */
18+ /** @param positive-int $timeLimit in seconds */
1919 public function __construct (
20- private readonly int $ timeLimitInSeconds ,
20+ private readonly int $ timeLimit ,
2121 private readonly LoggerInterface |null $ logger = null ,
2222 ) {
2323 }
2424
2525 public function onWorkerStarted (): void
2626 {
27- $ this ->endTime = microtime ( true ) + $ this ->timeLimitInSeconds ;
27+ $ this ->endTime = time ( ) + $ this ->timeLimit ;
2828 }
2929
3030 public function onWorkerRunning (WorkerRunningEvent $ event ): void
3131 {
32- if ($ this ->endTime >= microtime ( true )) {
32+ if ($ this ->endTime >= time ( )) {
3333 return ;
3434 }
3535
3636 $ event ->worker ->stop ();
3737 $ this ->logger ?->info(
3838 'Worker stopped due to time limit of {timeLimit}s exceeded ' ,
39- ['timeLimit ' => $ this ->timeLimitInSeconds ],
39+ ['timeLimit ' => $ this ->timeLimit ],
4040 );
4141 }
4242
Original file line number Diff line number Diff line change @@ -36,7 +36,7 @@ public function testShouldStop(): void
3636 $ listener = new StopWorkerOnTimeLimitListener (1 );
3737 $ listener ->onWorkerStarted ();
3838
39- sleep (1 );
39+ sleep (2 );
4040
4141 $ listener ->onWorkerRunning (new WorkerRunningEvent ($ worker ));
4242 }
You can’t perform that action at this time.
0 commit comments