From 7b9d56e0dfbda434234b62eae05acc3c2386e4c9 Mon Sep 17 00:00:00 2001 From: Huong Nguyen Date: Fri, 26 Jan 2024 10:02:19 +0700 Subject: [PATCH] MDL-74775 adhoc_task: Do not clear the timecreated of failed tasks --- lib/classes/task/manager.php | 4 ++- lib/tests/task/adhoc_task_test.php | 50 ++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/lib/classes/task/manager.php b/lib/classes/task/manager.php index a59ddbfa0c8..d559749b6bd 100644 --- a/lib/classes/task/manager.php +++ b/lib/classes/task/manager.php @@ -229,6 +229,9 @@ class manager { $record->nextruntime = time() - 1; } + // Set the time the task was created. + $record->timecreated = time(); + // Check if the same task is already scheduled. if ($checkforexisting && self::task_is_scheduled($task)) { return false; @@ -307,7 +310,6 @@ class manager { $record->faildelay = $task->get_fail_delay(); $record->customdata = $task->get_custom_data_as_string(); $record->userid = $task->get_userid(); - $record->timecreated = time(); $record->timestarted = $task->get_timestarted(); $record->hostname = $task->get_hostname(); $record->pid = $task->get_pid(); diff --git a/lib/tests/task/adhoc_task_test.php b/lib/tests/task/adhoc_task_test.php index 008c4b880ea..167ed86f21f 100644 --- a/lib/tests/task/adhoc_task_test.php +++ b/lib/tests/task/adhoc_task_test.php @@ -644,4 +644,54 @@ class adhoc_task_test extends \advanced_testcase { $this->expectExceptionMessage('error/invalidtaskid'); manager::get_adhoc_task($taskid); } + + /** + * Test adhoc task failure will retain the time information. + * + * @covers ::queue_adhoc_task + * @covers ::get_next_adhoc_task + * @covers ::adhoc_task_failed + */ + public function test_adhoc_task_failed_will_retain_time_info(): void { + global $DB; + $this->resetAfterTest(); + + $now = time(); + // Create an adhoc task. + $task = new adhoc_test_task(); + // Queue it. + $taskid = manager::queue_adhoc_task(task: $task); + + // Update the timecreated of the task to be older. + $DB->set_field( + table: 'task_adhoc', + newfield: 'timecreated', + newvalue: time() - DAYSECS, + conditions: ['id' => $taskid], + ); + + // Get the timecreated value before marking the task as failed. + $timecreatedbefore = $DB->get_field( + table: 'task_adhoc', + return: 'timecreated', + conditions: ['id' => $taskid], + ); + + // Get the task from the scheduler. + $task = manager::get_next_adhoc_task(timestart: $now); + // Execute the task. + $task->execute(); + // Mark the task as failed. + manager::adhoc_task_failed(task: $task); + + // Get the timecreated value after marking the task as failed. + $timecreatedafter = $DB->get_field( + table: 'task_adhoc', + return: 'timecreated', + conditions: ['id' => $taskid], + ); + + // The timecreated values should be the same. + $this->assertEquals($timecreatedbefore, $timecreatedafter); + } }