mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 12:32:08 +02:00
MDL-73293 core_task: Cleanup stale adhoc task metadata
This commit is contained in:
parent
5500d143f4
commit
cd1a7291ea
@ -1327,6 +1327,7 @@ $string['task_duration'] = 'Duration';
|
||||
$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';
|
||||
|
@ -1206,6 +1206,71 @@ class manager {
|
||||
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
|
||||
|
53
lib/classes/task/task_lock_cleanup_task.php
Normal file
53
lib/classes/task/task_lock_cleanup_task.php
Normal file
@ -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();
|
||||
}
|
||||
}
|
@ -428,4 +428,13 @@ $tasks = array(
|
||||
'month' => '*',
|
||||
'dayofweek' => '*',
|
||||
),
|
||||
[
|
||||
'classname' => 'core\task\task_lock_cleanup_task',
|
||||
'blocking' => 0,
|
||||
'minute' => 'R',
|
||||
'hour' => '0',
|
||||
'day' => '*',
|
||||
'dayofweek' => '*',
|
||||
'month' => '*'
|
||||
]
|
||||
);
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user