From 9e78dedebd1c03697d4bf3d8ff7a7d3f6f61d9c4 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 29 May 2021 14:20:05 +0200 Subject: [PATCH] Unlock jobs upon starting daemon --- src/Command/DaemonCommand.php | 1 + src/Repository/Job.php | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Command/DaemonCommand.php b/src/Command/DaemonCommand.php index ebff8a3..25baf86 100644 --- a/src/Command/DaemonCommand.php +++ b/src/Command/DaemonCommand.php @@ -42,6 +42,7 @@ class DaemonCommand extends Command } else { throw new \InvalidArgumentException('Time limit has incorrect value'); } + $jobRepo->unlockJob(); while(1) { if($endofscript !== false && time() > $endofscript) break; diff --git a/src/Repository/Job.php b/src/Repository/Job.php index 81ad24f..dca6cd3 100644 --- a/src/Repository/Job.php +++ b/src/Repository/Job.php @@ -38,7 +38,7 @@ class Job extends Repository public function getJobsDue() { - $jobsSql = "SELECT id FROM job WHERE (nextrun <= :timestamp AND running == 0) or (running not in (0,1) and running < :timestamprun)"; + $jobsSql = "SELECT id FROM job WHERE (nextrun <= :timestamp AND running IN (0,2)) or (running IN (0,1,2) and running < :timestamprun)"; $jobsStmt = $this->dbcon->prepare($jobsSql); $jobsRslt = $jobsStmt->executeQuery([':timestamp' => time(), ':timestamprun' => time()]); $jobs = $jobsRslt->fetchAllAssociative(); @@ -51,7 +51,7 @@ class Job extends Repository public function setJobRunning(int $job, bool $status): void { - $jobsSql = "UPDATE job SET running = :status WHERE id = :id AND running in (0,1)"; + $jobsSql = "UPDATE job SET running = :status WHERE id = :id AND running in (0,1,2)"; $jobsStmt = $this->dbcon->prepare($jobsSql); $jobsStmt->executeQuery([':id' => $job, ':status' => $status ? 1 : 0]); return; @@ -263,6 +263,21 @@ class Job extends Repository $addRunStmt = $this->dbcon->prepare($addRunSql); $addRunStmt->executeQuery([':id' => $job['id'], ':nextrun' => $nextrun]); } + + public function unlockJob(int $id = 0): void + { + $jobsSql = "UPDATE job SET running = :status WHERE running in (0,1,2)"; + $params = [':status' => 0]; + + if($id != 0) { + $jobsSql .= " AND id = :id"; + $params[':id'] = $id; + } + $jobsStmt = $this->dbcon->prepare($jobsSql); + $jobsStmt->executeQuery($params); + return; + } + public function addJob(array $values) { if(empty($values['crontype']) ||