From 75ece7ea27d5e93fd1b763ce8a939a043f331b2e Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 23 Apr 2022 08:49:59 +0200 Subject: [PATCH] BUGFIX: mysql could trigger running jobs a lot of times --- lib/Framework/Kernel.php | 4 ++++ src/Command/DaemonCommand.php | 17 +++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/Framework/Kernel.php b/lib/Framework/Kernel.php index 82ddae3..e721ae3 100644 --- a/lib/Framework/Kernel.php +++ b/lib/Framework/Kernel.php @@ -125,6 +125,10 @@ class Kernel } public function getNewDbCon(): Connection { + if(!is_null($this->dbCon)) { + $this->dbCon->close(); + $this->dbCon = null; + } $this->dbCon = DriverManager::getConnection(['url' => $_ENV['DATABASE']]); return $this->dbCon; } diff --git a/src/Command/DaemonCommand.php b/src/Command/DaemonCommand.php index b75a90b..8051447 100644 --- a/src/Command/DaemonCommand.php +++ b/src/Command/DaemonCommand.php @@ -47,11 +47,6 @@ class DaemonCommand extends Command while(1) { if($endofscript !== false && time() > $endofscript) break; - $maxwait = time() + 10; - $nextrun = $jobRepo->getTimeOfNextRun(); - $sleepuntil = min($maxwait, $nextrun); - if($sleepuntil > time()) time_sleep_until($sleepuntil); - $jobsToRun = $jobRepo->getJobsDue(); if(!empty($jobsToRun)) { foreach($jobsToRun as $job) { @@ -68,17 +63,19 @@ class DaemonCommand extends Command declare(ticks = 1); pcntl_signal(SIGCHLD, SIG_IGN); $pid = pcntl_fork(); - $jobRepoFork = new Job($this->kernel->getNewDbCon()); + $jobRepo = NULL; + $jobRepo = new Job($this->kernel->getNewDbCon()); if($pid == -1) { - $jobRepoFork->RunJob($job['id'], $job['running'] == 2); - $jobRepoFork->setJobRunning($job['id'], false); + $jobRepo->RunJob($job['id'], $job['running'] == 2); + $jobRepo->setJobRunning($job['id'], false); } elseif ($pid == 0) { - $jobRepoFork->RunJob($job['id'], $job['running'] == 2); - $jobRepoFork->setJobRunning($job['id'], false); + $jobRepo->RunJob($job['id'], $job['running'] == 2); + $jobRepo->setJobRunning($job['id'], false); exit; } } } + sleep(1); } $output->writeln('Ended after ' . $timelimit . ' seconds'); pcntl_wait($status);