From 894f36abf9dfcefd8e29fef2baf6963769e2b5ba Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Thu, 2 Apr 2015 17:41:48 +1100 Subject: [PATCH] MDL-49740 Lock: Fixed task lock release bugs --- admin/tool/task/cli/schedule_task.php | 40 +++++++++++---------- lib/classes/lock/db_record_lock_factory.php | 2 +- lib/classes/lock/postgres_lock_factory.php | 2 +- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/admin/tool/task/cli/schedule_task.php b/admin/tool/task/cli/schedule_task.php index 3476c1df235..6904d48c8ba 100644 --- a/admin/tool/task/cli/schedule_task.php +++ b/admin/tool/task/cli/schedule_task.php @@ -109,25 +109,29 @@ if ($execute = $options['execute']) { $predbqueries = $DB->perf_get_queries(); $pretime = microtime(true); + + mtrace("Scheduled task: " . $task->get_name()); + // NOTE: it would be tricky to move this code to \core\task\manager class, + // because we want to do detailed error reporting. + $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron'); + if (!$cronlock = $cronlockfactory->get_lock('core_cron', 10)) { + mtrace('Cannot obtain cron lock'); + exit(129); + } + if (!$lock = $cronlockfactory->get_lock('\\' . get_class($task), 10)) { + $cronlock->release(); + mtrace('Cannot obtain task lock'); + exit(130); + } + + $task->set_lock($lock); + if (!$task->is_blocking()) { + $cronlock->release(); + } else { + $task->set_cron_lock($cronlock); + } + try { - mtrace("Scheduled task: " . $task->get_name()); - // NOTE: it would be tricky to move this code to \core\task\manager class, - // because we want to do detailed error reporting. - $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron'); - if (!$cronlock = $cronlockfactory->get_lock('core_cron', 10)) { - mtrace('Cannot obtain cron lock'); - exit(129); - } - if (!$lock = $cronlockfactory->get_lock('\\' . get_class($task), 10)) { - mtrace('Cannot obtain task lock'); - exit(130); - } - $task->set_lock($lock); - if (!$task->is_blocking()) { - $cronlock->release(); - } else { - $task->set_cron_lock($cronlock); - } get_mailer('buffer'); $task->execute(); if (isset($predbqueries)) { diff --git a/lib/classes/lock/db_record_lock_factory.php b/lib/classes/lock/db_record_lock_factory.php index a7fbcfb07e9..3a6325c24de 100644 --- a/lib/classes/lock/db_record_lock_factory.php +++ b/lib/classes/lock/db_record_lock_factory.php @@ -216,7 +216,7 @@ class db_record_lock_factory implements lock_factory { // Called from the shutdown handler. Must release all open locks. foreach ($this->openlocks as $key => $unused) { $lock = new lock($key, $this); - $this->release_lock($lock); + $lock->release(); } } } diff --git a/lib/classes/lock/postgres_lock_factory.php b/lib/classes/lock/postgres_lock_factory.php index f27d532081d..5da94bc5084 100644 --- a/lib/classes/lock/postgres_lock_factory.php +++ b/lib/classes/lock/postgres_lock_factory.php @@ -236,7 +236,7 @@ class postgres_lock_factory implements lock_factory { // Called from the shutdown handler. Must release all open locks. foreach ($this->openlocks as $key => $unused) { $lock = new lock($key, $this); - $this->release_lock($lock); + $lock->release(); } }