MDL-84729 core: Use core\clock for Tasks
This will help to solve the random failure on "test_adhoc_task_get_first_starting_time"
This commit is contained in:
@@ -244,6 +244,8 @@ class manager {
|
||||
public static function queue_adhoc_task(adhoc_task $task, $checkforexisting = false) {
|
||||
global $DB;
|
||||
|
||||
$clock = \core\di::get(\core\clock::class);
|
||||
|
||||
// Don't queue tasks for deprecated components.
|
||||
if (self::task_component_is_deprecated($task)) {
|
||||
return false;
|
||||
@@ -257,13 +259,13 @@ class manager {
|
||||
$record = self::record_from_adhoc_task($task);
|
||||
// Schedule it immediately if nextruntime not explicitly set.
|
||||
if (!$task->get_next_run_time()) {
|
||||
$record->nextruntime = time() - 1;
|
||||
$record->nextruntime = $clock->time() - 1;
|
||||
}
|
||||
|
||||
// Check if the task is allowed to be retried or not.
|
||||
$record->attemptsavailable = $task->retry_until_success() ? $record->attemptsavailable : 1;
|
||||
// Set the time the task was created.
|
||||
$record->timecreated = time();
|
||||
$record->timecreated = $clock->time();
|
||||
|
||||
// Check if the same task is already scheduled.
|
||||
if ($checkforexisting && self::task_is_scheduled($task)) {
|
||||
@@ -556,7 +558,9 @@ class manager {
|
||||
public static function get_adhoc_tasks_summary(): array {
|
||||
global $DB;
|
||||
|
||||
$now = time();
|
||||
$clock = \core\di::get(\core\clock::class);
|
||||
|
||||
$now = $clock->time();
|
||||
$records = $DB->get_records('task_adhoc');
|
||||
$summary = [];
|
||||
foreach ($records as $r) {
|
||||
@@ -1127,6 +1131,9 @@ class manager {
|
||||
*/
|
||||
public static function adhoc_task_failed(adhoc_task $task) {
|
||||
global $DB;
|
||||
|
||||
$clock = \core\di::get(\core\clock::class);
|
||||
|
||||
// Finalise the log output.
|
||||
logmanager::finalise_log(true);
|
||||
|
||||
@@ -1154,7 +1161,7 @@ class manager {
|
||||
$task->set_timestarted();
|
||||
$task->set_hostname();
|
||||
$task->set_pid();
|
||||
$task->set_next_run_time(time() + $delay);
|
||||
$task->set_next_run_time($clock->time() + $delay);
|
||||
$task->set_fail_delay($delay);
|
||||
if ($task->get_attempts_available() > 0) {
|
||||
$task->set_attempts_available($task->get_attempts_available() - 1);
|
||||
@@ -1182,7 +1189,8 @@ class manager {
|
||||
$hostname = (string)gethostname();
|
||||
|
||||
if (empty($time)) {
|
||||
$time = time();
|
||||
$clock = \core\di::get(\core\clock::class);
|
||||
$time = $clock->time();
|
||||
}
|
||||
|
||||
$task->set_timestarted($time);
|
||||
@@ -1233,6 +1241,9 @@ class manager {
|
||||
*/
|
||||
public static function scheduled_task_failed(scheduled_task $task) {
|
||||
global $DB;
|
||||
|
||||
$clock = \core\di::get(\core\clock::class);
|
||||
|
||||
// Finalise the log output.
|
||||
logmanager::finalise_log(true);
|
||||
|
||||
@@ -1263,7 +1274,7 @@ class manager {
|
||||
$classname = self::get_canonical_class_name($task);
|
||||
|
||||
$record = $DB->get_record('task_scheduled', array('classname' => $classname));
|
||||
$record->nextruntime = time() + $delay;
|
||||
$record->nextruntime = $clock->time() + $delay;
|
||||
$record->faildelay = $delay;
|
||||
$record->timestarted = null;
|
||||
$record->hostname = null;
|
||||
@@ -1301,11 +1312,14 @@ class manager {
|
||||
*/
|
||||
public static function scheduled_task_starting(scheduled_task $task, int $time = 0) {
|
||||
global $DB;
|
||||
|
||||
$clock = \core\di::get(\core\clock::class);
|
||||
|
||||
$pid = (int)getmypid();
|
||||
$hostname = (string)gethostname();
|
||||
|
||||
if (!$time) {
|
||||
$time = time();
|
||||
$time = $clock->time();
|
||||
}
|
||||
|
||||
$task->set_timestarted($time);
|
||||
@@ -1330,6 +1344,8 @@ class manager {
|
||||
public static function scheduled_task_complete(scheduled_task $task) {
|
||||
global $DB;
|
||||
|
||||
$clock = \core\di::get(\core\clock::class);
|
||||
|
||||
// Finalise the log output.
|
||||
logmanager::finalise_log();
|
||||
$task->set_timestarted();
|
||||
@@ -1339,7 +1355,7 @@ class manager {
|
||||
$classname = self::get_canonical_class_name($task);
|
||||
$record = $DB->get_record('task_scheduled', array('classname' => $classname));
|
||||
if ($record) {
|
||||
$record->lastruntime = time();
|
||||
$record->lastruntime = $clock->time();
|
||||
$record->faildelay = 0;
|
||||
$record->nextruntime = $task->get_next_scheduled_time();
|
||||
$record->timestarted = null;
|
||||
@@ -1364,10 +1380,13 @@ class manager {
|
||||
*/
|
||||
public static function get_running_tasks($sort = ''): array {
|
||||
global $DB;
|
||||
|
||||
$clock = \core\di::get(\core\clock::class);
|
||||
|
||||
if (empty($sort)) {
|
||||
$sort = 'timestarted ASC, classname ASC';
|
||||
}
|
||||
$params = ['now1' => time(), 'now2' => time()];
|
||||
$params = ['now1' => $clock->time(), 'now2' => $clock->time()];
|
||||
|
||||
$sql = "SELECT subquery.*
|
||||
FROM (SELECT " . $DB->sql_concat("'s'", 'ts.id') . " as uniqueid,
|
||||
@@ -1402,11 +1421,13 @@ class manager {
|
||||
public static function cleanup_metadata() {
|
||||
global $DB;
|
||||
|
||||
$clock = \core\di::get(\core\clock::class);
|
||||
|
||||
$cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
|
||||
$runningtasks = self::get_running_tasks();
|
||||
|
||||
foreach ($runningtasks as $runningtask) {
|
||||
if ($runningtask->timestarted > time() - HOURSECS) {
|
||||
if ($runningtask->timestarted > $clock->time() - HOURSECS) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1468,15 +1489,18 @@ class manager {
|
||||
*/
|
||||
public static function clear_static_caches() {
|
||||
global $DB;
|
||||
|
||||
$clock = \core\di::get(\core\clock::class);
|
||||
|
||||
// Do not use get/set config here because the caches cannot be relied on.
|
||||
$record = $DB->get_record('config', array('name'=>'scheduledtaskreset'));
|
||||
if ($record) {
|
||||
$record->value = time();
|
||||
$record->value = $clock->time();
|
||||
$DB->update_record('config', $record);
|
||||
} else {
|
||||
$record = new \stdClass();
|
||||
$record->name = 'scheduledtaskreset';
|
||||
$record->value = time();
|
||||
$record->value = $clock->time();
|
||||
$DB->insert_record('config', $record);
|
||||
}
|
||||
}
|
||||
@@ -1776,12 +1800,15 @@ class manager {
|
||||
*/
|
||||
public static function clean_failed_adhoc_tasks(): void {
|
||||
global $CFG, $DB;
|
||||
|
||||
$clock = \core\di::get(\core\clock::class);
|
||||
|
||||
$difftime = !empty($CFG->task_adhoc_failed_retention) ?
|
||||
$CFG->task_adhoc_failed_retention : static::ADHOC_TASK_FAILED_RETENTION;
|
||||
$DB->delete_records_select(
|
||||
table: 'task_adhoc',
|
||||
select: 'attemptsavailable = 0 AND firststartingtime < :time',
|
||||
params: ['time' => time() - $difftime],
|
||||
params: ['time' => $clock->time() - $difftime],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user