MDL-75579 adhoc_task: Implement no-retry flag

This commit is contained in:
Huong Nguyen
2024-01-23 09:34:12 +07:00
parent 72c1d1921f
commit ca05ed2c18
4 changed files with 71 additions and 0 deletions
+11
View File
@@ -1088,6 +1088,17 @@ class manager {
$delay = $task->get_fail_delay();
if ($delay > 0) {
// If the task has a fail delay, it's already run at least once.
// We need to check if the task should be retried or not.
$taskcustomdata = $task->get_custom_data();
if ($taskcustomdata && isset($taskcustomdata->noretry) && $taskcustomdata->noretry) {
// The task has been marked as not retrying, so we can mark it as completed and delete it.
self::adhoc_task_complete($task);
return;
}
}
// Reschedule task with exponential fall off for failing tasks.
if (empty($delay)) {
$delay = 60;
+19
View File
@@ -107,6 +107,25 @@ class adhoc_test4_task extends adhoc_test_task {
class adhoc_test5_task extends adhoc_test_task {
}
/**
* Test adhoc-task with no-retry customdata.
*
* @copyright 2023 Huong Nguyen <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class adhoc_test6_task extends adhoc_test_task {
/**
* Execute.
*
* @return void
*/
public function execute(): void {
// Something wrong happened, do not retry.
$this->set_custom_data((object) ['noretry' => true]);
}
}
class scheduled_test_task extends \core\task\scheduled_task {
public function get_name() {
return "Test task";
+39
View File
@@ -605,4 +605,43 @@ class adhoc_task_test extends \advanced_testcase {
$output
);
}
/**
* Test adhoc task failure without retry.
*
* @covers ::get_next_adhoc_task
* @covers ::get_adhoc_task
* @covers ::adhoc_task_failed
*/
public function test_get_next_adhoc_task_without_fail_retry(): void {
global $DB;
$this->resetAfterTest();
// Create an adhoc task.
$task = new adhoc_test6_task();
manager::queue_adhoc_task($task);
$this->assertCount(1, $DB->get_records('task_adhoc'));
$now = time();
// Get the task from the scheduler, execute it, and mark it as failed.
$task = manager::get_next_adhoc_task($now);
$taskid = $task->get_id();
$task->execute();
manager::adhoc_task_failed($task);
$this->assertCount(1, $DB->get_records('task_adhoc'));
// Get the task from the scheduler (retry after delay). Fail it again.
$task = manager::get_next_adhoc_task($now + 120);
$this->assertInstanceOf('\\core\\task\\adhoc_test6_task', $task);
$this->assertEquals($taskid, $task->get_id());
$task->execute();
manager::adhoc_task_failed($task);
// The task was marked as no-retry, so it was deleted.
$this->assertCount(0, $DB->get_records('task_adhoc'));
$this->expectException(\moodle_exception::class);
$this->expectExceptionMessage('error/invalidtaskid');
manager::get_adhoc_task($taskid);
}
}
+2
View File
@@ -4,6 +4,8 @@ information provided here is intended especially for developers.
=== 4.2.5 ===
* The current page language is available in new `core/config` language property for Javascript modules
* The customdata of adhoc_task class now accepts a new value called noretry. If set to true, the adhoc task will not be retried
if it fails.
=== 4.2.4 ===
* Add a new parameter to the debounce (core/utils) function allow it to create its own own Pending promise.