isDue() reads nextrun from from database

This commit is contained in:
Uwe Steinmann 2024-07-02 16:25:05 +02:00
parent 6f11dc6dc2
commit 9db0985495

View File

@ -259,7 +259,50 @@ class SeedDMS_SchedulerTask {
return $this->_params; return $this->_params;
} }
/**
* Check if task is due
*
* This methods compares the current time with the time in the database
* field `nextrun`.
* If nextrun is smaller than the current time, the the task is due.
* The methode does not rely on the value in the class variable `_nextrun`,
* because that value could be 'very old', retrieved at a time
* when the task list was fetched for checking due tasks e.g. by the
* scheduler client. There is good reason to always take the current
* value of nextrun from the database.
*
* Assuming there are two tasks. Task 1 takes 13 mins and task 2 takes only
* 30 sec. Task 1 is run every hour and task 2 starts at 8:06. The cronjob
* runs every 5 min. At e.g. 8:00 the list of tasks is read from the database
* task 1 is due and starts running and before it runs it sets the database
* field nextrun to 9:00. Task 2 isn't due at that time.
* At 8:05 the cron job runs again, task 1 has already a new nextrun value
* and will not run again. Task 2 isn't due yet and task 1 started at 8:00 is
* still running.
* At 8:10 task 1 is still running an not due again, but task 2 is due and
* will be run. The database field `nextrun` of task 2 will be set to 8:06
* on the next day.
* At 8:13 task 1 which started at 8:00 is finished and the list of tasks
* from that time will be processed further. Task 2 still has the old value
* in the class variable `_nextrun` (8:06 the current day),
* though the database field `nextrun` has been updated in
* between. Taking the value of the class variable would rerun task 2 again,
* though it ran at 8:10 already.
* That's why this method always takes the current value of nextrun
* from the database.
*
* @return boolean true if task is due, otherwise false
*/
public function isDue() { public function isDue() {
$queryStr = "SELECT * FROM `tblSchedulerTask` WHERE `id` = " . $this->_id;
$resArr = $db->getResultArray($queryStr);
if (is_bool($resArr) && $resArr == false)
return false;
if (count($resArr) != 1)
return false;
$row = $resArr[0];
$this->_nextrun = $row['nextrun'];
return $this->_nextrun < date('Y-m-d H:i:s'); return $this->_nextrun < date('Y-m-d H:i:s');
} }