Wait till next minute after running jobs

This commit is contained in:
Juan Pablo Vial
2025-05-13 15:53:13 -04:00
parent c47d94d475
commit e7f3b33850

View File

@ -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);