From e7f3b33850b8eef63023d40d2e4f4b7d4bbab8cc Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Tue, 13 May 2025 15:53:13 -0400 Subject: [PATCH] Wait till next minute after running jobs --- cli/src/Command/BaseLoop.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/cli/src/Command/BaseLoop.php b/cli/src/Command/BaseLoop.php index 5c18877..3c7e815 100644 --- a/cli/src/Command/BaseLoop.php +++ b/cli/src/Command/BaseLoop.php @@ -28,24 +28,27 @@ class BaseLoop extends Console\Command\Command $this->write($output, 'Starting loop...'); while (true) { $commands = $this->scheduleService->getPending(); + $start = new DateTimeImmutable(); foreach ($commands as $command) { $this->runCommand($input, $output, $command); - sleep(10); } + $this->waitNextMinute($output, $start); } return self::SUCCESS; } - protected function waitNextMinute(Console\Output\OutputInterface $output): void + protected function waitNextMinute(Console\Output\OutputInterface $output, ?DateTimeInterface $start = null): void { // wait for next minute - $now = new DateTimeImmutable(); - $nextMinute = new DateTimeImmutable($now->format('Y-m-d H:i:00')); + if ($start === null) { + $start = new DateTimeImmutable(); + } + $nextMinute = new DateTimeImmutable($start->format('Y-m-d H:i:00')); $nextMinute = $nextMinute->add(new \DateInterval('PT1M')); $this->logger->debug('Wait', [ - 'now' => $now->format('Y-m-d H:i:s.u'), + 'start' => $start->format('Y-m-d H:i:s.u'), 'nextMinute' => $nextMinute->format('Y-m-d H:i:s.u'), ]); - $diff = $nextMinute->getTimestamp() - $now->getTimestamp(); + $diff = $nextMinute->getTimestamp() - $start->getTimestamp(); if ($diff > 0) { $output->writeln("Waiting {$diff} seconds..."); sleep($diff);