Skip to content
Permalink
Browse files

MDL-50532 tasks: Delete obsolete tasks during upgrade

  • Loading branch information...
FMCorz committed Jul 22, 2015
1 parent 97c2995 commit c535e6255290793a22453623243d4dc7282bd22c
Showing with 55 additions and 1 deletion.
  1. +14 −1 lib/classes/task/manager.php
  2. +41 −0 lib/tests/scheduled_task_test.php
@@ -55,7 +55,7 @@ public static function load_default_scheduled_tasks_for_component($componentname
}
$tasks = null;
require_once($file);
include($file);
if (!isset($tasks)) {
return array();
@@ -86,13 +86,16 @@ public static function load_default_scheduled_tasks_for_component($componentname
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);
if (strpos($classname, '\\') !== 0) {
$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 @@ public static function reset_scheduled_tasks_for_component($componentname) {
$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);
}
/**
@@ -226,6 +226,47 @@ public function test_reset_scheduled_tasks_for_component() {
$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;

0 comments on commit c535e62

Please sign in to comment.
You can’t perform that action at this time.