diff --git a/admin/cli/adhoc_task.php b/admin/cli/adhoc_task.php index 4c6fa9f2274..e0a211c1866 100644 --- a/admin/cli/adhoc_task.php +++ b/admin/cli/adhoc_task.php @@ -39,6 +39,7 @@ list($options, $unrecognized) = cli_get_params( 'force' => false, 'id' => null, 'classname' => null, + 'taskslimit' => null, ], [ 'h' => 'help', 'e' => 'execute', @@ -46,6 +47,7 @@ list($options, $unrecognized) = cli_get_params( 'i' => 'ignorelimits', 'f' => 'force', 'c' => 'classname', + 'l' => 'taskslimit', ] ); @@ -54,11 +56,7 @@ if ($unrecognized) { cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); } -if ($options['id'] || $options['classname']) { - $options['execute'] = true; -} -if ($options['help'] or empty($options['execute'])) { - $help = <<set_debug(true); + exit(0); } if (CLI_MAINTENANCE) { @@ -108,8 +109,13 @@ if (!get_config('core', 'cron_enabled') && !$options['force']) { exit(1); } -if (empty($options['keep-alive'])) { - $options['keep-alive'] = 0; +// Common debugging options. +if ($options['showdebugging']) { + set_debugging(DEBUG_DEVELOPER, true); +} + +if ($options['showsql']) { + $DB->set_debug(true); } if (!empty($CFG->showcronsql)) { @@ -119,6 +125,7 @@ if (!empty($CFG->showcrondebugging)) { set_debugging(DEBUG_DEVELOPER, true); } +// Process params. core_php_time_limit::raise(); // Increase memory limit. @@ -131,14 +138,28 @@ raise_memory_limit(MEMORY_EXTRA); $humantimenow = date('r', time()); mtrace("Server Time: {$humantimenow}\n"); +// Run a single adhoc task only, if requested. if (!empty($options['id'])) { $taskid = (int) $options['id']; \core\cron::run_adhoc_task($taskid); -} elseif (!empty($options['execute'])) { - - $checklimits = empty($options['ignorelimits']); - - $keepalive = (int)$options['keep-alive']; - - \core\cron::run_adhoc_tasks(time(), $keepalive, $checklimits, null, $options['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']); + +if ($classname || $keepalive || $taskslimit) { + $execute = true; +} + +// Output the help text if no criteria for running the adhoc tasks are given. +if (!$execute) { + echo $help; + exit(0); +} + +\core\cron::run_adhoc_tasks(time(), $keepalive, $checklimits, null, $taskslimit, $classname); diff --git a/lib/classes/cron.php b/lib/classes/cron.php index 3455b28dbc2..a4de32b3719 100644 --- a/lib/classes/cron.php +++ b/lib/classes/cron.php @@ -226,6 +226,7 @@ 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 */ @@ -234,6 +235,7 @@ 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. @@ -300,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(); diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 67eb38a8a4a..7dc728faf7d 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -116,6 +116,7 @@ information provided here is intended especially for developers. `.activityiconcontainer .activityicon` containers to determine whether CSS filtering should be applied to the icon. If the icon needs to be rendered as is and not whitened out, the `.nofilter` CSS class needs to be applied to the icon. * Functions get_next_adhoc_task() and cron::run_adhoc_tasks() have additional parameter $classname to filter by the specified class. +* Function cron::run_adhoc_tasks() has additional parameter $number to limit the number of the tasks to run. === 4.1 ===