Skip to content

Commit

Permalink
Add check for pending adhoc tasks before executing new Scheduled task
Browse files Browse the repository at this point in the history
  • Loading branch information
rajandangi committed Oct 15, 2024
1 parent ff91925 commit 557c3b2
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions classes/task/cron_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 557c3b2

Please sign in to comment.