Very long running jobs were losing the database connection

This commit is contained in:
Jeroen De Meerleer 2022-04-22 09:33:41 +02:00
parent 115b4b74a3
commit 6923152a62
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
3 changed files with 8 additions and 11 deletions

View File

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

View File

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

View File

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