diff --git a/lib/classes/task/manager.php b/lib/classes/task/manager.php index 559c68f7676cb..b9ef74ee7ddc5 100644 --- a/lib/classes/task/manager.php +++ b/lib/classes/task/manager.php @@ -679,7 +679,6 @@ public static function get_next_scheduled_task($timestart) { $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 @@ public static function get_next_scheduled_task($timestart) { 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); diff --git a/lib/tests/scheduled_task_test.php b/lib/tests/scheduled_task_test.php index a16e8e0bb48b3..3246b6dd9f0ab 100644 --- a/lib/tests/scheduled_task_test.php +++ b/lib/tests/scheduled_task_test.php @@ -640,6 +640,47 @@ public function test_scheduled_task_override_values(array $overrides, array $tas } } + /** + * 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. *