Skip to content

Commit

Permalink
MDL-70341 tasks: Allow disabled tasks to be overridden
Browse files Browse the repository at this point in the history
  • Loading branch information
jamie-catalyst committed Dec 7, 2020
1 parent c8d33eb commit 2fee989
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/classes/task/manager.php
Expand Up @@ -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);
Expand All @@ -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);

Expand Down
41 changes: 41 additions & 0 deletions lib/tests/scheduled_task_test.php
Expand Up @@ -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.
*
Expand Down

0 comments on commit 2fee989

Please sign in to comment.