From 6b2e15f6c516acfa0d0e50c2c07c6f40519c6feb Mon Sep 17 00:00:00 2001 From: Mikhail Golenkov Date: Thu, 2 Jan 2020 17:34:19 +1100 Subject: [PATCH] MDL-67485 task: Release the task runner lock before throwing exception. --- lib/classes/task/manager.php | 2 ++ lib/cronlib.php | 34 +++++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/lib/classes/task/manager.php b/lib/classes/task/manager.php index 091974bfb07..016ac4c69c5 100644 --- a/lib/classes/task/manager.php +++ b/lib/classes/task/manager.php @@ -553,6 +553,7 @@ class manager { * * @param int $timestart * @return \core\task\adhoc_task or null if not found + * @throws \moodle_exception */ public static function get_next_adhoc_task($timestart) { global $DB; @@ -609,6 +610,7 @@ class manager { * * @param int $timestart - The start of the cron process - do not repeat any tasks that have been run more recently than this. * @return \core\task\scheduled_task or null + * @throws \moodle_exception */ public static function get_next_scheduled_task($timestart) { global $DB; diff --git a/lib/cronlib.php b/lib/cronlib.php index db0193fe857..2ccbb3d5d9f 100644 --- a/lib/cronlib.php +++ b/lib/cronlib.php @@ -87,6 +87,7 @@ function cron_run() { * Execute all queued scheduled tasks, applying necessary concurrency limits and time limits. * * @param int $timenow The time this process started. + * @throws \moodle_exception */ function cron_run_scheduled_tasks(int $timenow) { // Allow a restriction on the number of scheduled task runners at once. @@ -109,19 +110,21 @@ function cron_run_scheduled_tasks(int $timenow) { $starttime = time(); // Run all scheduled tasks. - while (!\core\task\manager::static_caches_cleared_since($timenow) && - $task = \core\task\manager::get_next_scheduled_task($timenow)) { - cron_run_inner_scheduled_task($task); - unset($task); + try { + while (!\core\task\manager::static_caches_cleared_since($timenow) && + $task = \core\task\manager::get_next_scheduled_task($timenow)) { + cron_run_inner_scheduled_task($task); + unset($task); - if ((time() - $starttime) > $maxruntime) { - mtrace("Stopping processing of scheduled tasks as time limit has been reached."); - break; + if ((time() - $starttime) > $maxruntime) { + mtrace("Stopping processing of scheduled tasks as time limit has been reached."); + break; + } } + } finally { + // Release the scheduled task runner lock. + $scheduledlock->release(); } - - // Release the scheduled task runner lock. - $scheduledlock->release(); } /** @@ -130,6 +133,7 @@ function cron_run_scheduled_tasks(int $timenow) { * @param int $timenow The time this process started. * @param int $keepalive Keep this function alive for N seconds and poll for new adhoc tasks. * @param bool $checklimits Should we check limits? + * @throws \moodle_exception */ function cron_run_adhoc_tasks(int $timenow, $keepalive = 0, $checklimits = true) { // Allow a restriction on the number of adhoc task runners at once. @@ -168,7 +172,15 @@ function cron_run_adhoc_tasks(int $timenow, $keepalive = 0, $checklimits = true) break; } - $task = \core\task\manager::get_next_adhoc_task(time()); + try { + $task = \core\task\manager::get_next_adhoc_task(time()); + } catch (Exception $e) { + if ($adhoclock) { + // Release the adhoc task runner lock. + $adhoclock->release(); + } + throw $e; + } if ($task) { if ($waiting) {