MDL-70975 task: Support for running only failed ad hoc tasks

* CLI adhoc_task.php: new option --failed
This commit is contained in:
Srdjan 2022-03-24 15:25:55 +10:00
parent fa61fce08d
commit 6d9aaa8412
3 changed files with 32 additions and 3 deletions

View File

@ -40,6 +40,7 @@ list($options, $unrecognized) = cli_get_params(
'id' => null, 'id' => null,
'classname' => null, 'classname' => null,
'taskslimit' => null, 'taskslimit' => null,
'failed' => false,
], [ ], [
'h' => 'help', 'h' => 'help',
'e' => 'execute', 'e' => 'execute',
@ -70,6 +71,7 @@ Options:
--id Run (failed) task with id --id Run (failed) task with id
-c, --classname Run tasks with a certain classname (FQN) -c, --classname Run tasks with a certain classname (FQN)
-l, --taskslimit=N Run at most N tasks -l, --taskslimit=N Run at most N tasks
--failed Run only tasks that failed, ie those with a fail delay
Run all queued tasks: Run all queued tasks:
\$sudo -u www-data /usr/bin/php admin/cli/adhoc_task.php --execute \$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()); $humantimenow = date('r', time());
mtrace("Server Time: {$humantimenow}\n"); mtrace("Server Time: {$humantimenow}\n");
$classname = $options['classname'];
// Run a single adhoc task only, if requested. // Run a single adhoc task only, if requested.
if (!empty($options['id'])) { if (!empty($options['id'])) {
$taskid = (int) $options['id']; $taskid = (int) $options['id'];
@ -145,9 +149,14 @@ if (!empty($options['id'])) {
exit(0); 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. // Examine params and determine if we should run.
$execute = (bool) $options['execute']; $execute = (bool) $options['execute'];
$classname = $options['classname'];
$keepalive = empty($options['keep-alive']) ? 0 : (int) $options['keep-alive']; $keepalive = empty($options['keep-alive']) ? 0 : (int) $options['keep-alive'];
$taskslimit = empty($options['taskslimit']) ? null : (int) $options['taskslimit']; $taskslimit = empty($options['taskslimit']) ? null : (int) $options['taskslimit'];
$checklimits = empty($options['ignorelimits']); $checklimits = empty($options['ignorelimits']);

View File

@ -335,7 +335,7 @@ class cron {
} }
/** /**
* Execute a (failed) adhoc task. * Execute an adhoc task.
* *
* @param int $taskid * @param int $taskid
*/ */
@ -349,6 +349,26 @@ class cron {
self::set_process_title("Running adhoc task $taskid"); 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. * Shared code that handles running of a single scheduled task within the cron.
* *

View File

@ -1359,7 +1359,7 @@ class manager {
* *
* @param string|task_base $taskorstring Task object or a string * @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)) { if (is_string($taskorstring)) {
$classname = $taskorstring; $classname = $taskorstring;
} else { } else {