From 9db09854950ed046259b508b45bd78471f33debd Mon Sep 17 00:00:00 2001 From: Uwe Steinmann Date: Tue, 2 Jul 2024 16:25:05 +0200 Subject: [PATCH] isDue() reads nextrun from from database --- inc/inc.ClassSchedulerTask.php | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/inc/inc.ClassSchedulerTask.php b/inc/inc.ClassSchedulerTask.php index 5c19dda7a..2945e7861 100644 --- a/inc/inc.ClassSchedulerTask.php +++ b/inc/inc.ClassSchedulerTask.php @@ -259,7 +259,50 @@ class SeedDMS_SchedulerTask { 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() { + $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'); }