Unlock jobs upon starting daemon

This commit is contained in:
Jeroen De Meerleer 2021-05-29 14:20:05 +02:00
parent 94c2cfc48c
commit 9e78dedebd
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
2 changed files with 18 additions and 2 deletions

View File

@ -42,6 +42,7 @@ class DaemonCommand extends Command
} else { } else {
throw new \InvalidArgumentException('Time limit has incorrect value'); throw new \InvalidArgumentException('Time limit has incorrect value');
} }
$jobRepo->unlockJob();
while(1) { while(1) {
if($endofscript !== false && time() > $endofscript) break; if($endofscript !== false && time() > $endofscript) break;

View File

@ -38,7 +38,7 @@ class Job extends Repository
public function getJobsDue() 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); $jobsStmt = $this->dbcon->prepare($jobsSql);
$jobsRslt = $jobsStmt->executeQuery([':timestamp' => time(), ':timestamprun' => time()]); $jobsRslt = $jobsStmt->executeQuery([':timestamp' => time(), ':timestamprun' => time()]);
$jobs = $jobsRslt->fetchAllAssociative(); $jobs = $jobsRslt->fetchAllAssociative();
@ -51,7 +51,7 @@ class Job extends Repository
public function setJobRunning(int $job, bool $status): void 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 = $this->dbcon->prepare($jobsSql);
$jobsStmt->executeQuery([':id' => $job, ':status' => $status ? 1 : 0]); $jobsStmt->executeQuery([':id' => $job, ':status' => $status ? 1 : 0]);
return; return;
@ -263,6 +263,21 @@ class Job extends Repository
$addRunStmt = $this->dbcon->prepare($addRunSql); $addRunStmt = $this->dbcon->prepare($addRunSql);
$addRunStmt->executeQuery([':id' => $job['id'], ':nextrun' => $nextrun]); $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) public function addJob(array $values)
{ {
if(empty($values['crontype']) || if(empty($values['crontype']) ||