Wait till next minute after running jobs
This commit is contained in:
@ -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);
|
||||
|
Reference in New Issue
Block a user