Skip to content

Commit

Permalink
MDL-73293 core_task: Cleanup stale adhoc task metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
cameron1729 committed Jun 9, 2022
1 parent 5500d14 commit cd1a729
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 1 deletion.
1 change: 1 addition & 0 deletions lang/en/admin.php
Expand Up @@ -1327,6 +1327,7 @@
$string['task_dbstats'] = 'Database';
$string['task_result'] = 'Result';
$string['tasktype'] = 'Type';
$string['tasklockcleanuptask'] = 'Clean up ad hoc task metadata';
$string['taskadmintitle'] = 'Tasks';
$string['taskanalyticscleanup'] = 'Analytics cleanup';
$string['taskautomatedbackup'] = 'Automated backups';
Expand Down
65 changes: 65 additions & 0 deletions lib/classes/task/manager.php
Expand Up @@ -1206,6 +1206,71 @@ public static function get_running_tasks($sort = ''): array {
return $DB->get_records_sql($sql, $params);
}

/**
* Cleanup stale task metadata.
*/
public static function cleanup_metadata() {
global $DB;

$cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
$runningtasks = self::get_running_tasks();

foreach ($runningtasks as $taskrecord) {
if ($taskrecord->timestarted > time() - HOURSECS) {
continue;
}

if ($taskrecord->type == 'adhoc') {
$lock = $cronlockfactory->get_lock('adhoc_' . $taskrecord->id, 0);
}

if ($taskrecord->type == 'scheduled') {
$lock = $cronlockfactory->get_lock($taskrecord->classname, 0);
}

// If we got this lock it means one of three things:
//
// 1. The task was stopped abnormally and the metadata was not cleaned up
// 2. This is the process running the cleanup task
// 3. We took so long getting to it in this loop that it did finish, and we now have the lock
//
// In the case of 1. we need to make the task as failed, in the case of 2. and 3. we do nothing.
if (!empty($lock)) {
if ($taskrecord->classname == "\\" . \core\task\task_lock_cleanup_task::class) {
$lock->release();
continue;
}

// We need to get the record again to verify whether or not we are dealing with case 3.
$taskrecord = $DB->get_record('task_' . $taskrecord->type, ['id' => $taskrecord->id]);

if ($taskrecord->type == 'scheduled') {
// Empty timestarted indicates that this task finished (case 3) and was properly cleaned up.
if (empty($taskrecord->timestarted)) {
$lock->release();
continue;
}

$task = self::scheduled_task_from_record($taskrecord);
$task->set_lock($lock);
self::scheduled_task_failed($task);
} else if ($taskrecord->type == 'adhoc') {
// Ad hoc tasks are removed from the DB if they finish successfully.
// If we can't re-get this task, that means it finished and was properly
// cleaned up.
if (!$taskrecord) {
$lock->release();
continue;
}

$task = self::adhoc_task_from_record($taskrecord);
$task->set_lock($lock);
self::adhoc_task_failed($task);
}
}
}
}

/**
* This function is used to indicate that any long running cron processes should exit at the
* next opportunity and restart. This is because something (e.g. DB changes) has changed and
Expand Down
53 changes: 53 additions & 0 deletions lib/classes/task/task_lock_cleanup_task.php
@@ -0,0 +1,53 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Cleanup adhoc task metadata.
*
* @package core
* @copyright 2022 Catalyst IT Australia Pty Ltd
* @author Cameron Ball <cameron@cameron1729.xyz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core\task;

/**
* Adhoc task metadata cleanup task.
*
* @package core
* @copyright 2022 Catalyst IT Australia Pty Ltd
* @author Cameron Ball <cameron@cameron1729.xyz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class task_lock_cleanup_task extends scheduled_task {

/**
* Get a descriptive name for this task.
*
* @return string
*/
public function get_name() {
return get_string('tasklockcleanuptask', 'admin');
}

/**
* Processes task.
*/
public function execute() {
\core\task\manager::cleanup_metadata();
}
}
9 changes: 9 additions & 0 deletions lib/db/tasks.php
Expand Up @@ -428,4 +428,13 @@
'month' => '*',
'dayofweek' => '*',
),
[
'classname' => 'core\task\task_lock_cleanup_task',
'blocking' => 0,
'minute' => 'R',
'hour' => '0',
'day' => '*',
'dayofweek' => '*',
'month' => '*'
]
);
2 changes: 1 addition & 1 deletion version.php
Expand Up @@ -29,7 +29,7 @@

defined('MOODLE_INTERNAL') || die();

$version = 2022060300.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2022060300.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.1dev (Build: 20220603)'; // Human-friendly version name
Expand Down

0 comments on commit cd1a729

Please sign in to comment.