Skip to content
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()) {
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
2 changes: 1 addition & 1 deletion tools/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"php": "~8.1.0 || ~8.2.0",
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"roave/backward-compatibility-check": "^7.4.0 || ^8.0.0"
}
}
Loading