Merge branch 'MDL-80749-main' of https://github.com/HuongNV13/moodle

This commit is contained in:
Andrew Nicols 2024-03-05 21:18:44 +08:00
commit 30bfb5fe81
No known key found for this signature in database
GPG key ID: 6D1E3157C8CFBF14
5 changed files with 96 additions and 5 deletions

View file

@ -1227,6 +1227,13 @@ class manager {
$task->set_pid($pid);
$record = self::record_from_adhoc_task($task);
// If this is the first time the task has been started, then set the first starting time.
$firststartingtime = $DB->get_field('task_adhoc', 'firststartingtime', ['id' => $record->id]);
if (is_null($firststartingtime)) {
$record->firststartingtime = $time;
}
$DB->update_record('task_adhoc', $record);
self::task_starting($task);
@ -1813,7 +1820,7 @@ class manager {
$CFG->task_adhoc_failed_retention : static::ADHOC_TASK_FAILED_RETENTION;
$DB->delete_records_select(
table: 'task_adhoc',
select: 'attemptsavailable = 0 AND timestarted < :time',
select: 'attemptsavailable = 0 AND firststartingtime < :time',
params: ['time' => time() - $difftime],
);
}

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="lib/db" VERSION="20240123" COMMENT="XMLDB file for core Moodle tables"
<XMLDB PATH="lib/db" VERSION="20240129" COMMENT="XMLDB file for core Moodle tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
@ -3510,6 +3510,7 @@
<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="firststartingtime" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The start time of the first run of the task"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>

View file

@ -1093,5 +1093,20 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2024022300.02);
}
if ($oldversion < 2024030500.01) {
// Define field firststartingtime to be added to task_adhoc.
$table = new xmldb_table('task_adhoc');
$field = new xmldb_field('firststartingtime', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'attemptsavailable');
// Conditionally launch add field firststartingtime.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2024030500.01);
}
return true;
}

View file

@ -305,7 +305,7 @@ class adhoc_task_test extends \advanced_testcase {
// Update the time of the task2 to be older more than 2 days.
$DB->set_field(
table: 'task_adhoc',
newfield: 'timestarted',
newfield: 'firststartingtime',
newvalue: time() - (DAYSECS * 2) - 10, // Plus 10 seconds to make sure it is older than 2 days.
conditions: ['id' => $taskid2],
);
@ -322,7 +322,7 @@ class adhoc_task_test extends \advanced_testcase {
// Update the time of the task1 to be older than the cleanup time.
$DB->set_field(
table: 'task_adhoc',
newfield: 'timestarted',
newfield: 'firststartingtime',
newvalue: time() - $CFG->task_adhoc_failed_retention - 10, // Plus 10 seconds to make sure it is older than the retention time.
conditions: ['id' => $taskid1],
);
@ -730,6 +730,74 @@ class adhoc_task_test extends \advanced_testcase {
$this->assertEquals($user->id, $task->get_userid());
}
/**
* Test adhoc task with the first starting time.
*
* @covers ::queue_adhoc_task
* @covers ::get_next_adhoc_task
* @covers ::adhoc_task_starting
* @covers ::adhoc_task_failed
*/
public function test_adhoc_task_get_first_starting_time(): void {
global $DB;
$this->resetAfterTest(true);
$now = time();
// Create an adhoc task.
$task = new adhoc_test_task();
// Queue it.
$taskid = manager::queue_adhoc_task(task: $task);
// Get the firststartingtime value.
$firststartingtime = $DB->get_field(
table: 'task_adhoc',
return: 'firststartingtime',
conditions: ['id' => $taskid],
);
$this->assertNull($firststartingtime);
// This will make sure that the task will be started after the $now value.
sleep(3);
// Get the task from the scheduler.
$task = manager::get_next_adhoc_task(timestart: $now);
// Mark the task as starting.
manager::adhoc_task_starting($task);
// Execute the task.
$task->execute();
// Mark the task as failed.
manager::adhoc_task_failed(task: $task);
// Get the firststartingtime value.
$origintimestarted = $DB->get_field(
table: 'task_adhoc',
return: 'firststartingtime',
conditions: ['id' => $taskid],
);
$this->assertNotNull($origintimestarted);
$this->assertGreaterThan($now, $origintimestarted);
// Get the task from the scheduler.
$task = manager::get_next_adhoc_task(timestart: $now + 86400);
// Mark the task as starting.
manager::adhoc_task_starting($task);
// Execute the task.
$task->execute();
// Mark the task as failed.
manager::adhoc_task_failed(task: $task);
// Get the firststartingtime value.
$firststartingtime = $DB->get_field(
table: 'task_adhoc',
return: 'firststartingtime',
conditions: ['id' => $taskid],
);
// The firststartingtime value should not be changed.
$this->assertEquals($origintimestarted, $firststartingtime);
}
/**
* Test get_concurrency_limit() method to return 0 by default.
*

View file

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2024030500.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2024030500.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.4dev+ (Build: 20240305)'; // Human-friendly version name