Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix time logic #11

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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