Merge branch 'MDL-81000-main2' of https://github.com/raortegar/moodle
This commit is contained in:
@@ -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
|
||||
@@ -178,18 +178,18 @@ abstract class adhoc_task extends task_base {
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
@@ -261,7 +256,7 @@ class manager {
|
||||
}
|
||||
|
||||
// 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();
|
||||
|
||||
|
||||
+2
-2
@@ -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"
|
||||
>
|
||||
@@ -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>
|
||||
|
||||
+16
-1
@@ -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,
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -136,6 +136,32 @@ final class adhoc_task_test extends \advanced_testcase {
|
||||
$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.
|
||||
*/
|
||||
@@ -148,13 +174,13 @@ final class adhoc_task_test extends \advanced_testcase {
|
||||
$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);
|
||||
@@ -162,13 +188,13 @@ final class adhoc_task_test extends \advanced_testcase {
|
||||
$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();
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user