Skip to content

Commit

Permalink
MDL-67485 task: Release the task runner lock before throwing exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
golenkovm committed Jan 9, 2020
1 parent c554024 commit 6b2e15f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
2 changes: 2 additions & 0 deletions lib/classes/task/manager.php
Expand Up @@ -553,6 +553,7 @@ public static function ensure_adhoc_task_qos(array $records): array {
*
* @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;
Expand Down Expand Up @@ -609,6 +610,7 @@ public static function get_next_adhoc_task($timestart) {
*
* @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;
Expand Down
34 changes: 23 additions & 11 deletions lib/cronlib.php
Expand Up @@ -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.
Expand All @@ -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();
}

/**
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6b2e15f

Please sign in to comment.