From b3e0d3edeca07257eb81abe21bc4dc4bcb36dd25 Mon Sep 17 00:00:00 2001 From: Mikhail Golenkov Date: Thu, 9 Jan 2020 14:49:38 +1100 Subject: [PATCH] MDL-67485 task: Release the task runner lock before throwing exception. --- lib/classes/task/manager.php | 2 ++ lib/cronlib.php | 46 ++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/lib/classes/task/manager.php b/lib/classes/task/manager.php index 87fe34287ac..55f1dd19e85 100644 --- a/lib/classes/task/manager.php +++ b/lib/classes/task/manager.php @@ -478,6 +478,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; @@ -532,6 +533,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 c735ac2501b..d6ded2bf5c7 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,25 +110,28 @@ 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(); } /** * Execute all queued adhoc tasks, applying necessary concurrency limits and time limits. * * @param int $timenow The time this process started. + * @throws \moodle_exception */ function cron_run_adhoc_tasks(int $timenow) { // Allow a restriction on the number of adhoc task runners at once. @@ -150,19 +154,21 @@ function cron_run_adhoc_tasks(int $timenow) { $starttime = time(); // Run all adhoc tasks. - while (!\core\task\manager::static_caches_cleared_since($timenow) && - $task = \core\task\manager::get_next_adhoc_task(time())) { - cron_run_inner_adhoc_task($task); - unset($task); + try { + while (!\core\task\manager::static_caches_cleared_since($timenow) && + $task = \core\task\manager::get_next_adhoc_task(time())) { + cron_run_inner_adhoc_task($task); + unset($task); - if ((time() - $starttime) > $maxruntime) { - mtrace("Stopping processing of adhoc tasks as time limit has been reached."); - break; + if ((time() - $starttime) > $maxruntime) { + mtrace("Stopping processing of adhoc tasks as time limit has been reached."); + break; + } } + } finally { + // Release the adhoc task runner lock. + $adhoclock->release(); } - - // Release the adhoc task runner lock. - $adhoclock->release(); } /**