MDL-80749 adhoc_task: Log the first starting time of the task
This commit is contained in:
@@ -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);
|
||||
|
||||
+2
-1
@@ -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"/>
|
||||
|
||||
@@ -1093,5 +1093,20 @@ function xmldb_main_upgrade($oldversion) {
|
||||
upgrade_main_savepoint(true, 2024022300.02);
|
||||
}
|
||||
|
||||
if ($oldversion < 2024021500.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, 2024021500.01);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$version = 2024030100.00; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
$version = 2024030100.01; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
// RR = release increments - 00 in DEV branches.
|
||||
// .XX = incremental changes.
|
||||
$release = '4.4dev (Build: 20240301)'; // Human-friendly version name
|
||||
|
||||
Reference in New Issue
Block a user