Skip to content

Commit 1076160

Browse files
authored
Merge pull request #11 from patchlevel/fix-time-logic
fix time logic
2 parents 09e9cf5 + e4bac43 commit 1076160

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

src/DefaultWorker.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use function max;
2121
use function microtime;
22+
use function round;
2223
use function usleep;
2324

2425
final 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

src/Listener/StopWorkerOnTimeLimitListener.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,34 @@
99
use Psr\Log\LoggerInterface;
1010
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1111

12-
use function microtime;
12+
use function time;
1313

1414
final 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

tests/Unit/Listener/StopWorkerOnTimeLimitListenerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)