MDL-50532 tasks: Delete obsolete tasks during upgrade

This commit is contained in:
Frederic Massart 2015-07-22 19:53:48 +08:00
parent d84625438e
commit b2f6f88dc2
2 changed files with 55 additions and 1 deletions

View File

@ -55,7 +55,7 @@ class manager {
}
$tasks = null;
require_once($file);
include($file);
if (!isset($tasks)) {
return array();
@ -86,6 +86,7 @@ class manager {
public static function reset_scheduled_tasks_for_component($componentname) {
global $DB;
$tasks = self::load_default_scheduled_tasks_for_component($componentname);
$validtasks = array();
foreach ($tasks as $taskid => $task) {
$classname = get_class($task);
@ -93,6 +94,8 @@ class manager {
$classname = '\\' . $classname;
}
$validtasks[] = $classname;
if ($currenttask = self::get_scheduled_task($classname)) {
if ($currenttask->is_customised()) {
// If there is an existing task with a custom schedule, do not override it.
@ -110,6 +113,16 @@ class manager {
$DB->insert_record('task_scheduled', $record);
}
}
// Delete any task that is not defined in the component any more.
$sql = "component = :component";
$params = array('component' => $componentname);
if (!empty($validtasks)) {
list($insql, $inparams) = $DB->get_in_or_equal($validtasks, SQL_PARAMS_NAMED, 'param', false);
$sql .= ' AND classname ' . $insql;
$params = array_merge($params, $inparams);
}
$DB->delete_records_select('task_scheduled', $sql, $params);
}
/**

View File

@ -217,6 +217,47 @@ class core_scheduled_task_testcase extends advanced_testcase {
$this->assertEquals($initcount, $finalcount);
}
/**
* Tests that the reset function deletes old tasks.
*/
public function test_reset_scheduled_tasks_for_component_delete() {
global $DB;
$this->resetAfterTest(true);
$count = $DB->count_records('task_scheduled', array('component' => 'moodle'));
$allcount = $DB->count_records('task_scheduled');
$task = new \core\task\scheduled_test_task();
$task->set_component('moodle');
$record = \core\task\manager::record_from_scheduled_task($task);
$DB->insert_record('task_scheduled', $record);
$this->assertTrue($DB->record_exists('task_scheduled', array('classname' => '\core\task\scheduled_test_task',
'component' => 'moodle')));
$task = new \core\task\scheduled_test2_task();
$task->set_component('moodle');
$record = \core\task\manager::record_from_scheduled_task($task);
$DB->insert_record('task_scheduled', $record);
$this->assertTrue($DB->record_exists('task_scheduled', array('classname' => '\core\task\scheduled_test2_task',
'component' => 'moodle')));
$aftercount = $DB->count_records('task_scheduled', array('component' => 'moodle'));
$afterallcount = $DB->count_records('task_scheduled');
$this->assertEquals($count + 2, $aftercount);
$this->assertEquals($allcount + 2, $afterallcount);
// Now check that the right things were deleted.
\core\task\manager::reset_scheduled_tasks_for_component('moodle');
$this->assertEquals($count, $DB->count_records('task_scheduled', array('component' => 'moodle')));
$this->assertEquals($allcount, $DB->count_records('task_scheduled'));
$this->assertFalse($DB->record_exists('task_scheduled', array('classname' => '\core\task\scheduled_test2_task',
'component' => 'moodle')));
$this->assertFalse($DB->record_exists('task_scheduled', array('classname' => '\core\task\scheduled_test_task',
'component' => 'moodle')));
}
public function test_get_next_scheduled_task() {
global $DB;