BUGFIX: mysql could trigger running jobs a lot of times

This commit is contained in:
Jeroen De Meerleer 2022-04-23 08:49:59 +02:00
parent 6923152a62
commit 75ece7ea27
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
2 changed files with 11 additions and 10 deletions

View File

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

View File

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