Merge branch 'MDL-47480-master' of git://github.com/ankitagarwal/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 9 Jun 2015 22:32:10 +0000 (00:32 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 9 Jun 2015 22:32:10 +0000 (00:32 +0200)
lib/classes/task/manager.php
lib/tests/scheduled_task_test.php

index 36772ee..15e4512 100644 (file)
@@ -448,7 +448,8 @@ class manager {
 
         $where = "(lastruntime IS NULL OR lastruntime < :timestart1)
                   AND (nextruntime IS NULL OR nextruntime < :timestart2)
-                  AND disabled = 0";
+                  AND disabled = 0
+                  ORDER BY lastruntime, id ASC";
         $params = array('timestart1' => $timestart, 'timestart2' => $timestart);
         $records = $DB->get_records_select('task_scheduled', $where, $params);
 
index d07b607..ebe284c 100644 (file)
@@ -273,6 +273,33 @@ class core_scheduled_task_testcase extends advanced_testcase {
         // Should not get any task.
         $task = \core\task\manager::get_next_scheduled_task($now);
         $this->assertNull($task);
+
+        // Check ordering.
+        $DB->delete_records('task_scheduled');
+        $record->lastruntime = 2;
+        $record->disabled = 0;
+        $record->classname = '\core\task\scheduled_test_task';
+        $DB->insert_record('task_scheduled', $record);
+
+        $record->lastruntime = 1;
+        $record->classname = '\core\task\scheduled_test2_task';
+        $DB->insert_record('task_scheduled', $record);
+
+        // Should get handed the second task.
+        $task = \core\task\manager::get_next_scheduled_task($now);
+        $this->assertInstanceOf('\core\task\scheduled_test2_task', $task);
+        $task->execute();
+        \core\task\manager::scheduled_task_complete($task);
+
+        // Should get handed the first task.
+        $task = \core\task\manager::get_next_scheduled_task($now);
+        $this->assertInstanceOf('\core\task\scheduled_test_task', $task);
+        $task->execute();
+        \core\task\manager::scheduled_task_complete($task);
+
+        // Should not get any task.
+        $task = \core\task\manager::get_next_scheduled_task($now);
+        $this->assertNull($task);
     }
 
     public function test_get_broken_scheduled_task() {