From 6923152a62340f15142af96e8602c9585f42c773 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 22 Apr 2022 09:33:41 +0200 Subject: [PATCH] Very long running jobs were losing the database connection --- lib/Framework/Kernel.php | 4 ---- src/Command/DaemonCommand.php | 11 +++++------ src/Repository/Job.php | 4 +++- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/Framework/Kernel.php b/lib/Framework/Kernel.php index e721ae3..82ddae3 100644 --- a/lib/Framework/Kernel.php +++ b/lib/Framework/Kernel.php @@ -125,10 +125,6 @@ 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 9e9fc90..b75a90b 100644 --- a/src/Command/DaemonCommand.php +++ b/src/Command/DaemonCommand.php @@ -68,14 +68,13 @@ class DaemonCommand extends Command declare(ticks = 1); pcntl_signal(SIGCHLD, SIG_IGN); $pid = pcntl_fork(); - $jobRepo = NULL; - $jobRepo = new Job($this->kernel->getNewDbCon()); + $jobRepoFork = new Job($this->kernel->getNewDbCon()); if($pid == -1) { - $jobRepo->RunJob($job['id'], $job['running'] == 2); - $jobRepo->setJobRunning($job['id'], false); + $jobRepoFork->RunJob($job['id'], $job['running'] == 2); + $jobRepoFork->setJobRunning($job['id'], false); } elseif ($pid == 0) { - $jobRepo->RunJob($job['id'], $job['running'] == 2); - $jobRepo->setJobRunning($job['id'], false); + $jobRepoFork->RunJob($job['id'], $job['running'] == 2); + $jobRepoFork->setJobRunning($job['id'], false); exit; } } diff --git a/src/Repository/Job.php b/src/Repository/Job.php index 0ca19bc..e318325 100644 --- a/src/Repository/Job.php +++ b/src/Repository/Job.php @@ -416,6 +416,7 @@ class Job extends Repository public function runJob(int $job, bool $manual): array { + global $kernel; $starttime = microtime(true); $job = $this->getJob($job, true); if ($job['data']['crontype'] == 'http') { @@ -450,7 +451,8 @@ class Job extends Repository } } // saving to database - $runRepo = new Run($this->dbcon); + $runRepo = new Run($kernel->getNewDbCon()); + $this->dbcon = $kernel->getNewDbCon(); $runRepo->addRun($job['id'], $result['exitcode'], floor($starttime), $runtime, $result['output'], $flags); if (!$manual){ // setting nextrun to next run