Skip to content

Commit

Permalink
Merge pull request #13 from patchlevel/1.1.x-merge-up-into-1.2.x_pyvq…
Browse files Browse the repository at this point in the history
…PfFG

Merge release 1.1.1 into 1.2.x
  • Loading branch information
DavidBadura authored Feb 1, 2024
2 parents 7913945 + 1076160 commit 79b6128
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 98 deletions.
7 changes: 5 additions & 2 deletions src/DefaultWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use function max;
use function microtime;
use function round;
use function usleep;

final class DefaultWorker implements Worker
Expand All @@ -32,6 +33,7 @@ public function __construct(
) {
}

/** @param int $sleepTimer in milliseconds */
public function run(int $sleepTimer = 1000): void
{
$this->logger?->debug('Worker starting');
Expand All @@ -41,11 +43,12 @@ public function run(int $sleepTimer = 1000): void
while (!$this->shouldStop) {
$this->logger?->debug('Worker starting job run');

$startTime = microtime(true);
$startTime = (int)round(microtime(true) * 1000);

($this->job)();

$ranTime = (int)(microtime(true) - $startTime);
$endTime = (int)round(microtime(true) * 1000);
$ranTime = $endTime - $startTime;

$this->logger?->debug('Worker finished job run ({ranTime}ms)', ['ranTime' => $ranTime]);

Expand Down
12 changes: 6 additions & 6 deletions src/Listener/StopWorkerOnTimeLimitListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

use function microtime;
use function time;

final class StopWorkerOnTimeLimitListener implements EventSubscriberInterface
{
private float $endTime = 0;

/** @param positive-int $timeLimitInSeconds */
/** @param positive-int $timeLimit in seconds */
public function __construct(
private readonly int $timeLimitInSeconds,
private readonly int $timeLimit,
private readonly LoggerInterface|null $logger = null,
) {
}

public function onWorkerStarted(): void
{
$this->endTime = microtime(true) + $this->timeLimitInSeconds;
$this->endTime = time() + $this->timeLimit;
}

public function onWorkerRunning(WorkerRunningEvent $event): void
{
if ($this->endTime >= microtime(true)) {
if ($this->endTime >= time()) {

Check warning on line 32 in src/Listener/StopWorkerOnTimeLimitListener.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.3, ubuntu-latest)

Escaped Mutant for Mutator "GreaterThanOrEqualTo": --- Original +++ New @@ @@ } public function onWorkerRunning(WorkerRunningEvent $event) : void { - if ($this->endTime >= time()) { + if ($this->endTime > time()) { return; } $event->worker->stop();
return;
}

$event->worker->stop();
$this->logger?->info(
'Worker stopped due to time limit of {timeLimit}s exceeded',
['timeLimit' => $this->timeLimitInSeconds],
['timeLimit' => $this->timeLimit],
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Listener/StopWorkerOnTimeLimitListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testShouldStop(): void
$listener = new StopWorkerOnTimeLimitListener(1);
$listener->onWorkerStarted();

sleep(1);
sleep(2);

$listener->onWorkerRunning(new WorkerRunningEvent($worker));
}
Expand Down
Loading

0 comments on commit 79b6128

Please sign in to comment.