Skip to content

Commit

Permalink
Merge branch 'MDL-81000-main2' of https://github.com/raortegar/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
HuongNV13 committed Mar 28, 2024
2 parents 9572c9b + 8698a9a commit bc67950
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 20 deletions.
12 changes: 6 additions & 6 deletions lib/classes/task/adhoc_task.php
Expand Up @@ -46,8 +46,8 @@ abstract class adhoc_task extends task_base {
/** @var \core\lock\lock The concurrency task lock for this task. */
private $concurrencylock = null;

/** @var integer|null $attemptsavailable - The remaining attempts of the task, or null for unlimited. */
private $attemptsavailable = null;
/** @var int $attemptsavailable - The remaining attempts of the task. */
private $attemptsavailable = 12;

/**
* Provide default implementation of the task name for backward compatibility. Extending classes are expected to implement
Expand Down Expand Up @@ -178,18 +178,18 @@ final public function release_concurrency_lock(): void {
/**
* Set the remaining attempts of the task.
*
* @param int|null $attemptsavailable Number of the remaining attempts of the task, or null for unlimited.
* @param int $attemptsavailable Number of the remaining attempts of the task.
*/
public function set_attempts_available(?int $attemptsavailable): void {
public function set_attempts_available(int $attemptsavailable): void {
$this->attemptsavailable = $attemptsavailable;
}

/**
* Get the remaining attempts of the task.
*
* @return int|null Number of the remaining attempts of the task, or null for unlimited.
* @return int Number of the remaining attempts of the task.
*/
public function get_attempts_available(): ?int {
public function get_attempts_available(): int {
return $this->attemptsavailable;
}

Expand Down
7 changes: 1 addition & 6 deletions lib/classes/task/manager.php
Expand Up @@ -56,11 +56,6 @@ class manager {
*/
const ADHOC_TASK_FAILED_RETENTION = 4 * WEEKSECS;

/**
* @var int Used for max retry.
*/
const MAX_RETRY = 9;

/**
* @var ?task_base $runningtask Used to tell what is the current running task in this process.
*/
Expand Down Expand Up @@ -261,7 +256,7 @@ public static function queue_adhoc_task(adhoc_task $task, $checkforexisting = fa
}

// Check if the task is allowed to be retried or not.
$record->attemptsavailable = $task->retry_until_success() ? self::MAX_RETRY : 1;
$record->attemptsavailable = $task->retry_until_success() ? $record->attemptsavailable : 1;
// Set the time the task was created.
$record->timecreated = time();

Expand Down
4 changes: 2 additions & 2 deletions lib/db/install.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="lib/db" VERSION="20240129" COMMENT="XMLDB file for core Moodle tables"
<XMLDB PATH="lib/db" VERSION="20240326" COMMENT="XMLDB file for core Moodle tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -3509,7 +3509,7 @@
<FIELD NAME="timestarted" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="Time when the task was started"/>
<FIELD NAME="hostname" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="Hostname where the task is running"/>
<FIELD NAME="pid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="PHP process ID that is running the task"/>
<FIELD NAME="attemptsavailable" TYPE="int" LENGTH="1" NOTNULL="false" SEQUENCE="false" COMMENT="The remaining attempts for this task"/>
<FIELD NAME="attemptsavailable" TYPE="int" LENGTH="2" NOTNULL="false" SEQUENCE="false" COMMENT="The remaining attempts for this task"/>
<FIELD NAME="firststartingtime" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The start time of the first run of the task"/>
</FIELDS>
<KEYS>
Expand Down
17 changes: 16 additions & 1 deletion lib/db/upgrade.php
Expand Up @@ -870,7 +870,7 @@ function xmldb_main_upgrade($oldversion) {
$field = new xmldb_field(
name: 'attemptsavailable',
type: XMLDB_TYPE_INTEGER,
precision: '1',
precision: '2',
unsigned: null,
notnull: null,
sequence: null,
Expand Down Expand Up @@ -1129,5 +1129,20 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2024030500.02);
}

if ($oldversion < 2024032600.01) {

// Changing precision of field attemptsavailable on table task_adhoc to (2).
$table = new xmldb_table('task_adhoc');
$field = new xmldb_field('attemptsavailable', XMLDB_TYPE_INTEGER, '2', null, null, null, null, 'pid');

// Launch change of precision for field.
if (!$dbman->field_exists($table, $field)) {
$dbman->change_field_precision($table, $field);
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2024032600.01);
}

return true;
}
34 changes: 30 additions & 4 deletions lib/tests/task/adhoc_task_test.php
Expand Up @@ -136,6 +136,32 @@ public function test_get_next_adhoc_task_fail_retry(): void {
$this->assertNull(manager::get_next_adhoc_task($now));
}

/**
* Test that failed tasks eventually hit the maximum delay.
*
* @covers \core\task\adhoc_task
*/
public function test_get_next_adhoc_task_maximum_fail_delay(): void {
$this->resetAfterTest(true);

$now = time();

// Create an adhoc task.
$task = new adhoc_test_task();
$attemptsavailable = $task->get_attempts_available();
manager::queue_adhoc_task($task);

// Exhaust all attempts available.
for ($x = 0; $x < $attemptsavailable; $x++) {
$delay = $task->get_fail_delay() * 2;
$task = manager::get_next_adhoc_task($now + $delay);
$task->execute();
manager::adhoc_task_failed($task);
}
// Check that the fail delay is now set to 24 hours (the maximum amount of times).
$this->assertEquals(DAYSECS, $task->get_fail_delay());
}

/**
* Test adhoc task failure retry backoff.
*/
Expand All @@ -148,27 +174,27 @@ public function test_adhoc_task_with_retry_flag(): void {
$task = new adhoc_test_task();
$taskid1 = manager::queue_adhoc_task(task: $task);

// This is a normal task, so it should have unlimited attempts. The remaining available attempts should be null.
// This is a normal task, so it should have limited attempts.
$attemptsavailable = $DB->get_field(
table: 'task_adhoc',
return: 'attemptsavailable',
conditions: ['id' => $taskid1]
);
$this->assertEquals(expected: manager::MAX_RETRY, actual: $attemptsavailable);
$this->assertEquals(expected: 12, actual: $attemptsavailable);

// Get the task from the scheduler, execute it, and mark it as failed.
$task = manager::get_next_adhoc_task(timestart: $now);
$taskid1 = $task->get_id();
$task->execute();
manager::adhoc_task_failed(task: $task);

// This is a normal task, so it should have unlimited attempts. The remaining available attempts should be null.
// Now that the task has failed, there should be one less attempt available.
$attemptsavailable = $DB->get_field(
table: 'task_adhoc',
return: 'attemptsavailable',
conditions: ['id' => $taskid1]
);
$this->assertEquals(expected: manager::MAX_RETRY - 1, actual: $attemptsavailable);
$this->assertEquals(expected: 12 - 1, actual: $attemptsavailable);

// Create a no-retry adhoc task.
$now = time();
Expand Down
2 changes: 1 addition & 1 deletion version.php
Expand Up @@ -29,7 +29,7 @@

defined('MOODLE_INTERNAL') || die();

$version = 2024032600.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2024032600.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.4dev+ (Build: 20240326)'; // Human-friendly version name
Expand Down

0 comments on commit bc67950

Please sign in to comment.