Skip to content
Permalink
Browse files
Merge branch 'wip-MDL-59768-33' of git://github.com/marinaglancy/mood…
…le into MOODLE_33_STABLE
  • Loading branch information
David Monllao committed Sep 7, 2017
2 parents e9e0083 + fceada2 commit 064739b3dfcced9eaad543bd45c7dd0699966f79
Showing with 62 additions and 2 deletions.
  1. +1 −1 backup/moodle2/restore_stepslib.php
  2. +25 −1 lib/classes/task/manager.php
  3. +36 −0 lib/tests/adhoc_task_test.php
@@ -5555,6 +5555,6 @@ protected function define_execution() {
// The task will automatically populate all data.
$task = new \core\task\refresh_mod_calendar_events_task();
$task->set_custom_data(array('courseid' => $this->get_courseid()));
\core\task\manager::queue_adhoc_task($task);
\core\task\manager::queue_adhoc_task($task, true);
}
}
@@ -125,20 +125,44 @@ public static function reset_scheduled_tasks_for_component($componentname) {
$DB->delete_records_select('task_scheduled', $sql, $params);
}

/**
* Checks if the task with the same classname, component and customdata is already scheduled
*
* @param adhoc_task $task
* @return bool
*/
protected static function task_is_scheduled($task) {
global $DB;
$record = self::record_from_adhoc_task($task);
$params = [$record->classname, $record->component, $record->customdata];
$sql = 'classname = ? AND component = ? AND ' .
$DB->sql_compare_text('customdata', \core_text::strlen($record->customdata) + 1) . ' = ?';
return $DB->record_exists_select('task_adhoc', $sql, $params);
}

/**
* Queue an adhoc task to run in the background.
*
* @param \core\task\adhoc_task $task - The new adhoc task information to store.
* @param bool $checkforexisting - If set to true and the task with the same classname, component and customdata
* is already scheduled then it will not schedule a new task. Can be used only for ASAP tasks.
* @return boolean - True if the config was saved.
*/
public static function queue_adhoc_task(adhoc_task $task) {
public static function queue_adhoc_task(adhoc_task $task, $checkforexisting = false) {
global $DB;

$record = self::record_from_adhoc_task($task);
// Schedule it immediately if nextruntime not explicitly set.
if (!$task->get_next_run_time()) {
$record->nextruntime = time() - 1;
}

// Check if the same task is already scheduled.
if ($checkforexisting && self::task_is_scheduled($task)) {
return false;
}

// Queue the task.
$result = $DB->insert_record('task_adhoc', $record);

return $result;
@@ -109,4 +109,40 @@ public function test_get_next_adhoc_task_future() {
$task->execute();
\core\task\manager::adhoc_task_complete($task);
}

/**
* Test queue_adhoc_task "if not scheduled".
*/
public function test_queue_adhoc_task_if_not_scheduled() {
$this->resetAfterTest(true);

// Schedule adhoc task.
$task1 = new \core\task\adhoc_test_task();
$task1->set_custom_data(array('courseid' => 10));
$this->assertNotEmpty(\core\task\manager::queue_adhoc_task($task1, true));
$this->assertEquals(1, count(\core\task\manager::get_adhoc_tasks('core\task\adhoc_test_task')));

// Schedule same adhoc task with different custom data.
$task2 = new \core\task\adhoc_test_task();
$task2->set_custom_data(array('courseid' => 1));
$this->assertNotEmpty(\core\task\manager::queue_adhoc_task($task2, true));
$this->assertEquals(2, count(\core\task\manager::get_adhoc_tasks('core\task\adhoc_test_task')));

// Schedule same adhoc task with same custom data.
$task3 = new \core\task\adhoc_test_task();
$task3->set_custom_data(array('courseid' => 1));
$this->assertEmpty(\core\task\manager::queue_adhoc_task($task3, true));
$this->assertEquals(2, count(\core\task\manager::get_adhoc_tasks('core\task\adhoc_test_task')));

// Schedule same adhoc task without custom data.
$task4 = new \core\task\adhoc_test_task();
$this->assertNotEmpty(\core\task\manager::queue_adhoc_task($task4, true));
$this->assertEquals(3, count(\core\task\manager::get_adhoc_tasks('core\task\adhoc_test_task')));

// Schedule same adhoc task without custom data (again).
$task5 = new \core\task\adhoc_test_task();
$this->assertEmpty(\core\task\manager::queue_adhoc_task($task5, true));
$this->assertEquals(3, count(\core\task\manager::get_adhoc_tasks('core\task\adhoc_test_task')));

}
}

0 comments on commit 064739b

Please sign in to comment.