Merge branch 'MDL-70341-task-override-fix-disabled' of https://github.com/jamie-catalyst/moodle

This commit is contained in:
Víctor Déniz 2020-12-22 12:04:41 +00:00
commit 7c3f210441
2 changed files with 48 additions and 7 deletions

View File

@ -679,7 +679,6 @@ class manager {
$where = "(lastruntime IS NULL OR lastruntime < :timestart1)
AND (nextruntime IS NULL OR nextruntime < :timestart2)
AND disabled = 0
ORDER BY lastruntime, id ASC";
$params = array('timestart1' => $timestart, 'timestart2' => $timestart);
$records = $DB->get_records_select('task_scheduled', $where, $params);
@ -688,14 +687,15 @@ class manager {
foreach ($records as $record) {
$task = self::scheduled_task_from_record($record);
// Safety check in case the task in the DB does not match a real class (maybe something was uninstalled).
// Also check to see if task is disabled or enabled after applying overrides.
if (!$task || $task->get_disabled()) {
continue;
}
if ($lock = $cronlockfactory->get_lock(($record->classname), 0)) {
$classname = '\\' . $record->classname;
$task = self::scheduled_task_from_record($record);
// Safety check in case the task in the DB does not match a real class (maybe something was uninstalled).
if (!$task) {
$lock->release();
continue;
}
$task->set_lock($lock);

View File

@ -640,6 +640,47 @@ class core_scheduled_task_testcase extends advanced_testcase {
}
}
/**
* Check that an overridden task is sent to be processed.
*/
public function test_scheduled_task_overridden_task_can_run(): void {
global $CFG, $DB;
$this->resetAfterTest();
// Delete all existing scheduled tasks.
$DB->delete_records('task_scheduled');
// Add overrides to the config.
$CFG->scheduled_tasks = [
'\core\task\scheduled_test_task' => [
'disabled' => 1
],
'\core\task\scheduled_test2_task' => [
'disabled' => 0
],
];
// A task that runs once per hour.
$record = new stdClass();
$record->component = 'test_scheduled_task';
$record->classname = '\core\task\scheduled_test_task';
$record->disabled = 0;
$DB->insert_record('task_scheduled', $record);
// And disabled test.
$record->classname = '\core\task\scheduled_test2_task';
$record->disabled = 1;
$DB->insert_record('task_scheduled', $record);
$now = time();
$scheduledtask = \core\task\manager::get_next_scheduled_task($now);
$this->assertInstanceOf('\core\task\scheduled_test2_task', $scheduledtask);
$scheduledtask->execute();
\core\task\manager::scheduled_task_complete($scheduledtask);
}
/**
* Assert that the specified tasks are equal.
*