From b2f6f88dc29a98512937e8f9a58d74b53e63775d Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Wed, 22 Jul 2015 19:53:48 +0800 Subject: [PATCH] MDL-50532 tasks: Delete obsolete tasks during upgrade --- lib/classes/task/manager.php | 15 ++++++++++- lib/tests/scheduled_task_test.php | 41 +++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lib/classes/task/manager.php b/lib/classes/task/manager.php index 15e45120c51..24852e15a0d 100644 --- a/lib/classes/task/manager.php +++ b/lib/classes/task/manager.php @@ -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); } /** diff --git a/lib/tests/scheduled_task_test.php b/lib/tests/scheduled_task_test.php index ebe284c43c7..7f3b150e49d 100644 --- a/lib/tests/scheduled_task_test.php +++ b/lib/tests/scheduled_task_test.php @@ -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;