From 3d5863e77aa649f096c72d876ac3efa8d502b76c Mon Sep 17 00:00:00 2001 From: jeroen Date: Tue, 23 Aug 2022 16:40:12 +0200 Subject: [PATCH] Reimplemented periodic check --- src/Command/DaemonCommand.php | 6 +++++- src/Repository/JobRepository.php | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Command/DaemonCommand.php b/src/Command/DaemonCommand.php index 356ad3c..28fe47d 100644 --- a/src/Command/DaemonCommand.php +++ b/src/Command/DaemonCommand.php @@ -86,7 +86,11 @@ class DaemonCommand extends Command } } $this->doctrine->getManager()->clear(); - sleep(1); + + $maxwait = time() + 30; + $nextrun = $jobRepo->getTimeOfNextRun(); + $sleepuntil = min($maxwait, $nextrun); + if($sleepuntil > time()) time_sleep_until($sleepuntil); } $output->writeln('Ended after ' . $timelimit . ' seconds'); pcntl_wait($status); diff --git a/src/Repository/JobRepository.php b/src/Repository/JobRepository.php index 21a4a05..524c180 100644 --- a/src/Repository/JobRepository.php +++ b/src/Repository/JobRepository.php @@ -589,6 +589,37 @@ class JobRepository extends EntityRepository return ['success' => true, 'message' => 'Cronjob succesfully edited']; } + public function getTimeOfNextRun() + { + if(!empty($this->getJobsDue())) return time(); + + $qb = $this->createQueryBuilder('job'); + $firstScheduledJob = $qb + ->where('job.running = 0') + ->orderBy('job.nextrun') + ->getQuery()->getResult(); + + $firstRebootJob = $qb + ->where('job.running > 2') + ->orderBy('job.running') + ->getQuery()->getResult(); + + + if(empty($firstScheduledJob)) { + $val1 = PHP_INT_MAX; + } else { + $val1 = $firstScheduledJob[0]->getNextRun(); + } + + if(empty($firstRebootJob)) { + $val2 = PHP_INT_MAX; + } else { + $val2 = $firstRebootJob[0]->getRunning(); + } + + return min($val1, $val2); + } + /** * @param array $values * @param Job|null $job