From 557c3b294e49f14a915673ae24bfe2db5037bc66 Mon Sep 17 00:00:00 2001 From: rajandangi Date: Tue, 15 Oct 2024 10:57:16 +1030 Subject: [PATCH] Add check for pending adhoc tasks before executing new Scheduled task --- classes/task/cron_task.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/classes/task/cron_task.php b/classes/task/cron_task.php index 799bc30..669ae25 100644 --- a/classes/task/cron_task.php +++ b/classes/task/cron_task.php @@ -49,7 +49,32 @@ public function get_name() { public function execute() { global $CFG; require_once($CFG->dirroot . '/mod/reengagement/lib.php'); + + if ($this->has_pending_adhoc_tasks()) { + mtrace('Pending reengagement adhoc task already exists, skipping execution.'); + return; + } + reengagement_crontask(); } + /** + * Check if pending adhoc tasks created by this task already exist. + * + * @return bool + */ + protected function has_pending_adhoc_tasks() { + global $DB; + $pendingtasks = $DB->get_records_sql( + "SELECT * FROM {task_adhoc} + WHERE classname = :classname + AND faildelay = 0 + AND nextruntime <= :time", + [ + 'classname' => '\\mod_reengagement\\task\\reengagement_adhoc_task', + 'time' => time() + ] + ); + return !empty($pendingtasks); + } }