From 6d9aaa841235c23f1ec6af8d410666401bf33450 Mon Sep 17 00:00:00 2001 From: Srdjan Date: Thu, 24 Mar 2022 15:25:55 +1000 Subject: [PATCH] MDL-70975 task: Support for running only failed ad hoc tasks * CLI adhoc_task.php: new option --failed --- admin/cli/adhoc_task.php | 11 ++++++++++- lib/classes/cron.php | 22 +++++++++++++++++++++- lib/classes/task/manager.php | 2 +- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/admin/cli/adhoc_task.php b/admin/cli/adhoc_task.php index e0a211c1866..7f8bd4bde17 100644 --- a/admin/cli/adhoc_task.php +++ b/admin/cli/adhoc_task.php @@ -40,6 +40,7 @@ list($options, $unrecognized) = cli_get_params( 'id' => null, 'classname' => null, 'taskslimit' => null, + 'failed' => false, ], [ 'h' => 'help', 'e' => 'execute', @@ -70,6 +71,7 @@ Options: --id Run (failed) task with id -c, --classname Run tasks with a certain classname (FQN) -l, --taskslimit=N Run at most N tasks + --failed Run only tasks that failed, ie those with a fail delay Run all queued tasks: \$sudo -u www-data /usr/bin/php admin/cli/adhoc_task.php --execute @@ -138,6 +140,8 @@ raise_memory_limit(MEMORY_EXTRA); $humantimenow = date('r', time()); mtrace("Server Time: {$humantimenow}\n"); +$classname = $options['classname']; + // Run a single adhoc task only, if requested. if (!empty($options['id'])) { $taskid = (int) $options['id']; @@ -145,9 +149,14 @@ if (!empty($options['id'])) { exit(0); } +// Run all failed tasks. +if (!empty($options['failed'])) { + \core\cron::run_failed_adhoc_tasks($classname); + exit(0); +} + // Examine params and determine if we should run. $execute = (bool) $options['execute']; -$classname = $options['classname']; $keepalive = empty($options['keep-alive']) ? 0 : (int) $options['keep-alive']; $taskslimit = empty($options['taskslimit']) ? null : (int) $options['taskslimit']; $checklimits = empty($options['ignorelimits']); diff --git a/lib/classes/cron.php b/lib/classes/cron.php index a4de32b3719..74470025752 100644 --- a/lib/classes/cron.php +++ b/lib/classes/cron.php @@ -335,7 +335,7 @@ class cron { } /** - * Execute a (failed) adhoc task. + * Execute an adhoc task. * * @param int $taskid */ @@ -349,6 +349,26 @@ class cron { 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. * diff --git a/lib/classes/task/manager.php b/lib/classes/task/manager.php index c3741092781..4437ab7ba68 100644 --- a/lib/classes/task/manager.php +++ b/lib/classes/task/manager.php @@ -1359,7 +1359,7 @@ class manager { * * @param string|task_base $taskorstring Task object or a string */ - protected static function get_canonical_class_name($taskorstring) { + public static function get_canonical_class_name($taskorstring) { if (is_string($taskorstring)) { $classname = $taskorstring; } else {