MDL-70520 tasks: Keep lastruntime when a scheduled task is reset
authorMikhail Golenkov <mikhailgolenkov@catalyst-au.net>
Mon, 21 Dec 2020 23:37:43 +0000 (10:37 +1100)
committerMikhail Golenkov <mikhailgolenkov@catalyst-au.net>
Tue, 22 Dec 2020 04:30:45 +0000 (15:30 +1100)
lib/classes/task/manager.php
lib/tests/scheduled_task_test.php

index 08b6242..04952db 100644 (file)
@@ -229,6 +229,7 @@ class manager {
         $record = self::record_from_scheduled_task($task);
         $record->id = $original->id;
         $record->nextruntime = $task->get_next_scheduled_time();
+        unset($record->lastruntime);
         $result = $DB->update_record('task_scheduled', $record);
 
         return $result;
index 56f6915..24ac7d4 100644 (file)
@@ -568,4 +568,24 @@ class core_scheduled_task_testcase extends advanced_testcase {
 
         call_user_func_array([$this, 'assertNotEquals'], $args);
     }
+
+    /**
+     * Assert that the lastruntime column holds an original value after a scheduled task is reset.
+     */
+    public function test_reset_scheduled_tasks_for_component_keeps_original_lastruntime(): void {
+        global $DB;
+        $this->resetAfterTest(true);
+
+        // Set lastruntime for the scheduled task.
+        $DB->set_field('task_scheduled', 'lastruntime', 123456789, ['classname' => '\core\task\session_cleanup_task']);
+
+        // Reset the task.
+        \core\task\manager::reset_scheduled_tasks_for_component('moodle');
+
+        // Fetch the task again.
+        $taskafterreset = \core\task\manager::get_scheduled_task(core\task\session_cleanup_task::class);
+
+        // Confirm, that lastruntime is still in place.
+        $this->assertEquals(123456789, $taskafterreset->get_last_run_time());
+    }
 }