Merge branch 'MDL-70975-adhoc-carneval' of https://github.com/catalyst/moodle

This commit is contained in:
Ilya Tregubov
2023-04-13 09:07:58 +08:00
14 changed files with 973 additions and 75 deletions
+43 -1
View File
@@ -226,6 +226,8 @@ class cron {
* @param int $keepalive Keep this public static function alive for N seconds and poll for new adhoc tasks.
* @param bool $checklimits Should we check limits?
* @param null|int $startprocesstime The time this process started.
* @param int|null $maxtasks Limit number of tasks to run`
* @param null|string $classname Run only tasks of this class
* @throws \moodle_exception
*/
public static function run_adhoc_tasks(
@@ -233,6 +235,8 @@ class cron {
$keepalive = 0,
$checklimits = true,
?int $startprocesstime = null,
?int $maxtasks = null,
?string $classname = null,
): void {
// Allow a restriction on the number of adhoc task runners at once.
$cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
@@ -281,7 +285,7 @@ class cron {
}
try {
$task = \core\task\manager::get_next_adhoc_task(time(), $checklimits);
$task = \core\task\manager::get_next_adhoc_task(time(), $checklimits, $classname);
} catch (\Throwable $e) {
if ($adhoclock) {
// Release the adhoc task runner lock.
@@ -298,6 +302,9 @@ class cron {
self::run_inner_adhoc_task($task);
self::set_process_title("Waiting for next adhoc task");
$taskcount++;
if ($maxtasks && $taskcount >= $maxtasks) {
break;
}
unset($task);
} else {
$timeleft = $finishtime - time();
@@ -327,6 +334,41 @@ class cron {
}
}
/**
* Execute an adhoc task.
*
* @param int $taskid
*/
public static function run_adhoc_task(int $taskid): void {
$task = \core\task\manager::get_adhoc_task($taskid);
if (!$task->get_fail_delay() && $task->get_next_run_time() > time()) {
throw new \moodle_exception('wontrunfuturescheduledtask');
}
self::run_inner_adhoc_task($task);
self::set_process_title("Running adhoc task $taskid");
}
/**
* Execute all failed adhoc tasks.
*
* @param string|null $classname Run only tasks of this class
*/
public static function run_failed_adhoc_tasks(?string $classname = null): void {
global $DB;
$where = 'faildelay > 0';
$params = [];
if ($classname) {
$where .= ' AND classname = :classname';
$params['classname'] = \core\task\manager::get_canonical_class_name($classname);
}
$tasks = $DB->get_records_sql("SELECT * from {task_adhoc} WHERE $where", $params);
foreach ($tasks as $t) {
self::run_adhoc_task($t->id);
}
}
/**
* Shared code that handles running of a single scheduled task within the cron.
*