Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 192... Línea 192...
192
     * Checks if the task with the same classname, component and customdata is already scheduled
192
     * Checks if the task with the same classname, component and customdata is already scheduled
193
     *
193
     *
194
     * @param adhoc_task $task
194
     * @param adhoc_task $task
195
     * @return \stdClass|false
195
     * @return \stdClass|false
196
     */
196
     */
197
    protected static function get_queued_adhoc_task_record($task) {
197
    public static function get_queued_adhoc_task_record($task) {
198
        global $DB;
198
        global $DB;
Línea 199... Línea 199...
199
 
199
 
200
        $record = self::record_from_adhoc_task($task);
200
        $record = self::record_from_adhoc_task($task);
201
        $params = [$record->classname, $record->component, $record->customdata];
201
        $params = [$record->classname, $record->component, $record->customdata];
Línea 242... Línea 242...
242
     * @return boolean - True if the config was saved.
242
     * @return boolean - True if the config was saved.
243
     */
243
     */
244
    public static function queue_adhoc_task(adhoc_task $task, $checkforexisting = false) {
244
    public static function queue_adhoc_task(adhoc_task $task, $checkforexisting = false) {
245
        global $DB;
245
        global $DB;
Línea -... Línea 246...
-
 
246
 
-
 
247
        $clock = \core\di::get(\core\clock::class);
-
 
248
 
-
 
249
        // Don't queue tasks for deprecated components.
-
 
250
        if (self::task_component_is_deprecated($task)) {
-
 
251
            return false;
-
 
252
        }
246
 
253
 
247
        if ($userid = $task->get_userid()) {
254
        if ($userid = $task->get_userid()) {
248
            // User found. Check that they are suitable.
255
            // User found. Check that they are suitable.
249
            \core_user::require_active_user(\core_user::get_user($userid, '*', MUST_EXIST), true, true);
256
            \core_user::require_active_user(\core_user::get_user($userid, '*', MUST_EXIST), true, true);
Línea 250... Línea 257...
250
        }
257
        }
251
 
258
 
252
        $record = self::record_from_adhoc_task($task);
259
        $record = self::record_from_adhoc_task($task);
253
        // Schedule it immediately if nextruntime not explicitly set.
260
        // Schedule it immediately if nextruntime not explicitly set.
254
        if (!$task->get_next_run_time()) {
261
        if (!$task->get_next_run_time()) {
Línea 255... Línea 262...
255
            $record->nextruntime = time() - 1;
262
            $record->nextruntime = $clock->time() - 1;
256
        }
263
        }
257
 
264
 
258
        // Check if the task is allowed to be retried or not.
265
        // Check if the task is allowed to be retried or not.
Línea 259... Línea 266...
259
        $record->attemptsavailable = $task->retry_until_success() ? $record->attemptsavailable : 1;
266
        $record->attemptsavailable = $task->retry_until_success() ? $record->attemptsavailable : 1;
260
        // Set the time the task was created.
267
        // Set the time the task was created.
261
        $record->timecreated = time();
268
        $record->timecreated = $clock->time();
262
 
269
 
Línea 505... Línea 512...
505
 
512
 
506
    /**
513
    /**
507
     * This function load the adhoc tasks for a given classname.
514
     * This function load the adhoc tasks for a given classname.
508
     *
515
     *
509
     * @param string $classname
516
     * @param string $classname
510
     * @param bool $failedonly
517
     * @param bool $failedonly Return only failed tasks
-
 
518
     * @param bool $skiprunning Do not return tasks that are in the running state
511
     * @param bool $skiprunning do not return tasks that are in the running state
519
     * @param bool $dueonly Return only tasks that are due to run (nextruntime < now)
512
     * @return array
520
     * @return array
513
     */
521
     */
-
 
522
    public static function get_adhoc_tasks(
-
 
523
        string $classname,
-
 
524
        bool $failedonly = false,
-
 
525
        bool $skiprunning = false,
-
 
526
        bool $dueonly = false
514
    public static function get_adhoc_tasks(string $classname, bool $failedonly = false, bool $skiprunning = false): array {
527
    ): array {
Línea 515... Línea 528...
515
        global $DB;
528
        global $DB;
516
 
529
 
Línea 517... Línea 530...
517
        $conds[] = 'classname = ?';
530
        $conds[] = 'classname = ?';
518
        $params[] = self::get_canonical_class_name($classname);
531
        $params[] = self::get_canonical_class_name($classname);
-
 
532
 
-
 
533
        if ($failedonly) {
519
 
534
            $conds[] = 'faildelay > 0';
520
        if ($failedonly) {
535
        } else if ($dueonly) {
521
            $conds[] = 'faildelay > 0';
536
            $conds[] = 'faildelay = 0';
522
        }
537
        }
Línea 541... Línea 556...
541
     * @return array
556
     * @return array
542
     */
557
     */
543
    public static function get_adhoc_tasks_summary(): array {
558
    public static function get_adhoc_tasks_summary(): array {
544
        global $DB;
559
        global $DB;
Línea -... Línea 560...
-
 
560
 
-
 
561
        $clock = \core\di::get(\core\clock::class);
545
 
562
 
546
        $now = time();
563
        $now = $clock->time();
547
        $records = $DB->get_records('task_adhoc');
564
        $records = $DB->get_records('task_adhoc');
548
        $summary = [];
565
        $summary = [];
549
        foreach ($records as $r) {
566
        foreach ($records as $r) {
550
            if (!isset($summary[$r->component])) {
567
            if (!isset($summary[$r->component])) {
Línea 630... Línea 647...
630
        $records = $DB->get_records('task_scheduled', null, 'component, classname', '*', IGNORE_MISSING);
647
        $records = $DB->get_records('task_scheduled', null, 'component, classname', '*', IGNORE_MISSING);
631
        $tasks = array();
648
        $tasks = array();
Línea 632... Línea 649...
632
 
649
 
633
        foreach ($records as $record) {
650
        foreach ($records as $record) {
-
 
651
            $task = self::scheduled_task_from_record($record);
634
            $task = self::scheduled_task_from_record($record);
652
 
635
            // Safety check in case the task in the DB does not match a real class (maybe something was uninstalled).
653
            // Safety check in case the task in the DB does not match a real class (maybe something was uninstalled).
636
            if ($task) {
654
            if (!$task) {
637
                $tasks[] = $task;
655
                continue;
-
 
656
            }
-
 
657
 
-
 
658
            // Tasks belonging to deprecated plugin types are excluded.
-
 
659
            if (self::task_component_is_deprecated($task)) {
-
 
660
                continue;
-
 
661
            }
-
 
662
 
638
            }
663
            $tasks[] = $task;
Línea 639... Línea 664...
639
        }
664
        }
640
 
665
 
Línea 662... Línea 687...
662
        }
687
        }
663
        return $tasks;
688
        return $tasks;
664
    }
689
    }
Línea 665... Línea 690...
665
 
690
 
666
    /**
-
 
667
     * Ensure quality of service for the ad hoc task queue.
-
 
668
     *
-
 
669
     * This reshuffles the adhoc tasks queue to balance by type to ensure a
-
 
670
     * level of quality of service per type, while still maintaining the
-
 
671
     * relative order of tasks queued by timestamp.
-
 
672
     *
-
 
673
     * @param array $records array of task records
-
 
674
     * @param array $records array of same task records shuffled
691
    /**
675
     * @deprecated since Moodle 4.1 MDL-67648 - please do not use this method anymore.
-
 
676
     * @todo MDL-74843 This method will be deleted in Moodle 4.5
-
 
677
     * @see \core\task\manager::get_next_adhoc_task
692
     * @deprecated since Moodle 4.1 MDL-67648
678
     */
-
 
679
    public static function ensure_adhoc_task_qos(array $records): array {
-
 
680
        debugging('The method \core\task\manager::ensure_adhoc_task_qos is deprecated.
693
     */
681
             Please use \core\task\manager::get_next_adhoc_task instead.', DEBUG_DEVELOPER);
-
 
682
 
-
 
683
        $count = count($records);
-
 
684
        if ($count == 0) {
-
 
685
            return $records;
-
 
686
        }
-
 
687
 
-
 
688
        $queues = []; // This holds a queue for each type of adhoc task.
-
 
689
        $limits = []; // The relative limits of each type of task.
-
 
690
        $limittotal = 0;
-
 
691
 
694
    #[\core\attribute\deprecated('\core\task\manager::get_next_adhoc_task()', since: '4.1', mdl: 'MDL-67648', final: true)]
692
        // Split the single queue up into queues per type.
-
 
693
        foreach ($records as $record) {
-
 
694
            $type = $record->classname;
-
 
695
            if (!array_key_exists($type, $queues)) {
-
 
696
                $queues[$type] = [];
-
 
697
            }
-
 
698
            if (!array_key_exists($type, $limits)) {
-
 
699
                $limits[$type] = 1;
-
 
700
                $limittotal += 1;
-
 
701
            }
-
 
702
            $queues[$type][] = $record;
-
 
703
        }
-
 
704
 
-
 
705
        $qos = []; // Our new queue with ensured quality of service.
-
 
706
        $seed = $count % $limittotal; // Which task queue to shuffle from first?
-
 
707
 
-
 
708
        $move = 1; // How many tasks to shuffle at a time.
-
 
709
        do {
-
 
710
            $shuffled = 0;
-
 
711
 
-
 
712
            // Now cycle through task type queues and interleaving the tasks
-
 
713
            // back into a single queue.
-
 
714
            foreach ($limits as $type => $limit) {
-
 
715
 
-
 
716
                // Just interleaving the queue is not enough, because after
-
 
717
                // any task is processed the whole queue is rebuilt again. So
-
 
718
                // we need to deterministically start on different types of
-
 
719
                // tasks so that *on average* we rotate through each type of task.
-
 
720
                //
-
 
721
                // We achieve this by using a $seed to start moving tasks off a
695
    public static function ensure_adhoc_task_qos(): void {
722
                // different queue each time. The seed is based on the task count
-
 
723
                // modulo the number of types of tasks on the queue. As we count
-
 
724
                // down this naturally cycles through each type of record.
-
 
725
                if ($seed < 1) {
-
 
726
                    $shuffled = 1;
-
 
727
                    $seed += 1;
-
 
728
                    continue;
-
 
729
                }
-
 
730
                $tasks = array_splice($queues[$type], 0, $move);
-
 
731
                $qos = array_merge($qos, $tasks);
-
 
732
 
-
 
733
                // Stop if we didn't move any tasks onto the main queue.
-
 
734
                $shuffled += count($tasks);
-
 
735
            }
-
 
736
            // Generally the only tasks that matter are those that are near the start so
-
 
737
            // after we have shuffled the first few 1 by 1, start shuffling larger groups.
-
 
738
            if (count($qos) >= (4 * count($limits))) {
-
 
739
                $move *= 2;
-
 
740
            }
-
 
741
        } while ($shuffled > 0);
-
 
742
 
-
 
743
        return $qos;
696
        \core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
Línea 744... Línea 697...
744
    }
697
    }
745
 
698
 
746
    /**
699
    /**
Línea 1021... Línea 974...
1021
 
974
 
1022
        return null;
975
        return null;
Línea 1023... Línea 976...
1023
    }
976
    }
-
 
977
 
-
 
978
    /**
-
 
979
     * This function will delete an adhoc task by id. The task will be removed
-
 
980
     * from the database.
-
 
981
     *
-
 
982
     * @param int $taskid
-
 
983
     */
-
 
984
    public static function delete_adhoc_task(int $taskid): void {
-
 
985
        global $DB;
-
 
986
        $DB->delete_records('task_adhoc', ['id' => $taskid]);
-
 
987
    }
1024
 
988
 
1025
    /**
989
    /**
1026
     * This function will set locks on the task.
990
     * This function will set locks on the task.
1027
     *
991
     *
1028
     * @param adhoc_task    $task
992
     * @param adhoc_task    $task
Línea 1041... Línea 1005...
1041
        $task->set_lock($lock);
1005
        $task->set_lock($lock);
1042
        $cronlock->release();
1006
        $cronlock->release();
1043
    }
1007
    }
Línea 1044... Línea 1008...
1044
 
1008
 
-
 
1009
    /**
-
 
1010
     * Helper to check whether a task's component is deprecated.
-
 
1011
     *
-
 
1012
     * @param task_base $task the task instance
-
 
1013
     * @return bool true if deprecated, false otherwise.
-
 
1014
     */
-
 
1015
    private static function task_component_is_deprecated(task_base $task): bool {
-
 
1016
        // Only supports plugin type deprecation. Info will be null for other, non-plugin components.
-
 
1017
        if ($info = \core_plugin_manager::instance()->get_plugin_info($task->get_component())) {
-
 
1018
            if ($info->is_deprecated() || $info->is_deleted()) {
-
 
1019
                return true;
-
 
1020
            }
-
 
1021
        }
-
 
1022
        return false;
-
 
1023
    }
-
 
1024
 
1045
    /**
1025
    /**
1046
     * This function will dispatch the next scheduled task in the queue. The task will be handed out
1026
     * This function will dispatch the next scheduled task in the queue. The task will be handed out
1047
     * with an open lock - possibly on the entire cron process. Make sure you call either
1027
     * with an open lock - possibly on the entire cron process. Make sure you call either
1048
     * {@link scheduled_task_failed} or {@link scheduled_task_complete} to release the lock and reschedule the task.
1028
     * {@link scheduled_task_failed} or {@link scheduled_task_complete} to release the lock and reschedule the task.
1049
     *
1029
     *
Línea 1065... Línea 1045...
1065
 
1045
 
Línea 1066... Línea 1046...
1066
        foreach ($records as $record) {
1046
        foreach ($records as $record) {
1067
 
1047
 
1068
            $task = self::scheduled_task_from_record($record);
1048
            $task = self::scheduled_task_from_record($record);
1069
            // Safety check in case the task in the DB does not match a real class (maybe something was uninstalled).
1049
            // Safety check in case the task in the DB does not match a real class (maybe something was uninstalled).
1070
            // Also check to see if task is disabled or enabled after applying overrides.
1050
            // Also check to see if task is disabled or enabled after applying overrides, or if the plugintype is deprecated.
1071
            if (!$task || $task->get_disabled()) {
1051
            if (!$task || $task->get_disabled() || self::task_component_is_deprecated($task)) {
Línea 1072... Línea 1052...
1072
                continue;
1052
                continue;
1073
            }
1053
            }
Línea 1148... Línea 1128...
1148
 
1128
 
1149
    /**
1129
    /**
1150
     * This function indicates that an adhoc task was not completed successfully and should be retried.
1130
     * This function indicates that an adhoc task was not completed successfully and should be retried.
1151
     *
1131
     *
-
 
1132
     * @param \core\task\adhoc_task $task
1152
     * @param \core\task\adhoc_task $task
1133
     * @param bool $finaliselog finalise the log of the current running task
1153
     */
1134
     */
1154
    public static function adhoc_task_failed(adhoc_task $task) {
1135
    public static function adhoc_task_failed(adhoc_task $task, bool $finaliselog = true) {
-
 
1136
        global $DB;
-
 
1137
 
-
 
1138
        $clock = \core\di::get(\core\clock::class);
1155
        global $DB;
1139
 
-
 
1140
        // Finalise the log output.
1156
        // Finalise the log output.
1141
        if ($finaliselog) {
-
 
1142
            logmanager::finalise_log(true);
Línea 1157... Línea 1143...
1157
        logmanager::finalise_log(true);
1143
        }
Línea 1158... Línea 1144...
1158
 
1144
 
1159
        $delay = $task->get_fail_delay();
1145
        $delay = $task->get_fail_delay();
Línea 1178... Línea 1164...
1178
 
1164
 
1179
        // Reschedule and then release the locks.
1165
        // Reschedule and then release the locks.
1180
        $task->set_timestarted();
1166
        $task->set_timestarted();
1181
        $task->set_hostname();
1167
        $task->set_hostname();
1182
        $task->set_pid();
1168
        $task->set_pid();
1183
        $task->set_next_run_time(time() + $delay);
1169
        $task->set_next_run_time($clock->time() + $delay);
1184
        $task->set_fail_delay($delay);
1170
        $task->set_fail_delay($delay);
1185
        if ($task->get_attempts_available() > 0) {
1171
        if ($task->get_attempts_available() > 0) {
1186
            $task->set_attempts_available($task->get_attempts_available() - 1);
1172
            $task->set_attempts_available($task->get_attempts_available() - 1);
1187
        }
1173
        }
Línea 1206... Línea 1192...
1206
        global $DB;
1192
        global $DB;
1207
        $pid = (int)getmypid();
1193
        $pid = (int)getmypid();
1208
        $hostname = (string)gethostname();
1194
        $hostname = (string)gethostname();
Línea 1209... Línea 1195...
1209
 
1195
 
-
 
1196
        if (empty($time)) {
1210
        if (empty($time)) {
1197
            $clock = \core\di::get(\core\clock::class);
1211
            $time = time();
1198
            $time = $clock->time();
Línea 1212... Línea 1199...
1212
        }
1199
        }
1213
 
1200
 
1214
        $task->set_timestarted($time);
1201
        $task->set_timestarted($time);
Línea 1254... Línea 1241...
1254
 
1241
 
1255
    /**
1242
    /**
1256
     * This function indicates that a scheduled task was not completed successfully and should be retried.
1243
     * This function indicates that a scheduled task was not completed successfully and should be retried.
1257
     *
1244
     *
-
 
1245
     * @param \core\task\scheduled_task $task
1258
     * @param \core\task\scheduled_task $task
1246
     * @param bool $finaliselog finalise the log of the current running task
1259
     */
1247
     */
1260
    public static function scheduled_task_failed(scheduled_task $task) {
1248
    public static function scheduled_task_failed(scheduled_task $task, bool $finaliselog = true) {
-
 
1249
        global $DB;
-
 
1250
 
-
 
1251
        $clock = \core\di::get(\core\clock::class);
1261
        global $DB;
1252
 
-
 
1253
        // Finalise the log output.
1262
        // Finalise the log output.
1254
        if ($finaliselog) {
-
 
1255
            logmanager::finalise_log(true);
Línea 1263... Línea 1256...
1263
        logmanager::finalise_log(true);
1256
        }
Línea 1264... Línea 1257...
1264
 
1257
 
1265
        $delay = $task->get_fail_delay();
1258
        $delay = $task->get_fail_delay();
Línea 1287... Línea 1280...
1287
        $task->set_pid();
1280
        $task->set_pid();
Línea 1288... Línea 1281...
1288
 
1281
 
Línea 1289... Línea 1282...
1289
        $classname = self::get_canonical_class_name($task);
1282
        $classname = self::get_canonical_class_name($task);
1290
 
1283
 
1291
        $record = $DB->get_record('task_scheduled', array('classname' => $classname));
1284
        $record = $DB->get_record('task_scheduled', array('classname' => $classname));
1292
        $record->nextruntime = time() + $delay;
1285
        $record->nextruntime = $clock->time() + $delay;
1293
        $record->faildelay = $delay;
1286
        $record->faildelay = $delay;
1294
        $record->timestarted = null;
1287
        $record->timestarted = null;
1295
        $record->hostname = null;
1288
        $record->hostname = null;
Línea 1325... Línea 1318...
1325
     * @param int $time Start time (0 = current)
1318
     * @param int $time Start time (0 = current)
1326
     * @throws \dml_exception If the task doesn't exist
1319
     * @throws \dml_exception If the task doesn't exist
1327
     */
1320
     */
1328
    public static function scheduled_task_starting(scheduled_task $task, int $time = 0) {
1321
    public static function scheduled_task_starting(scheduled_task $task, int $time = 0) {
1329
        global $DB;
1322
        global $DB;
-
 
1323
 
-
 
1324
        $clock = \core\di::get(\core\clock::class);
-
 
1325
 
1330
        $pid = (int)getmypid();
1326
        $pid = (int)getmypid();
1331
        $hostname = (string)gethostname();
1327
        $hostname = (string)gethostname();
Línea 1332... Línea 1328...
1332
 
1328
 
1333
        if (!$time) {
1329
        if (!$time) {
1334
            $time = time();
1330
            $time = $clock->time();
Línea 1335... Línea 1331...
1335
        }
1331
        }
1336
 
1332
 
1337
        $task->set_timestarted($time);
1333
        $task->set_timestarted($time);
Línea 1354... Línea 1350...
1354
     * @param \core\task\scheduled_task $task
1350
     * @param \core\task\scheduled_task $task
1355
     */
1351
     */
1356
    public static function scheduled_task_complete(scheduled_task $task) {
1352
    public static function scheduled_task_complete(scheduled_task $task) {
1357
        global $DB;
1353
        global $DB;
Línea -... Línea 1354...
-
 
1354
 
-
 
1355
        $clock = \core\di::get(\core\clock::class);
1358
 
1356
 
1359
        // Finalise the log output.
1357
        // Finalise the log output.
1360
        logmanager::finalise_log();
1358
        logmanager::finalise_log();
1361
        $task->set_timestarted();
1359
        $task->set_timestarted();
1362
        $task->set_hostname();
1360
        $task->set_hostname();
Línea 1363... Línea 1361...
1363
        $task->set_pid();
1361
        $task->set_pid();
1364
 
1362
 
1365
        $classname = self::get_canonical_class_name($task);
1363
        $classname = self::get_canonical_class_name($task);
1366
        $record = $DB->get_record('task_scheduled', array('classname' => $classname));
1364
        $record = $DB->get_record('task_scheduled', array('classname' => $classname));
1367
        if ($record) {
1365
        if ($record) {
1368
            $record->lastruntime = time();
1366
            $record->lastruntime = $clock->time();
1369
            $record->faildelay = 0;
1367
            $record->faildelay = 0;
1370
            $record->nextruntime = $task->get_next_scheduled_time();
1368
            $record->nextruntime = $task->get_next_scheduled_time();
1371
            $record->timestarted = null;
1369
            $record->timestarted = null;
Línea 1388... Línea 1386...
1388
     * @return array Array of scheduled and adhoc tasks
1386
     * @return array Array of scheduled and adhoc tasks
1389
     * @throws \dml_exception
1387
     * @throws \dml_exception
1390
     */
1388
     */
1391
    public static function get_running_tasks($sort = ''): array {
1389
    public static function get_running_tasks($sort = ''): array {
1392
        global $DB;
1390
        global $DB;
-
 
1391
 
-
 
1392
        $clock = \core\di::get(\core\clock::class);
-
 
1393
 
1393
        if (empty($sort)) {
1394
        if (empty($sort)) {
1394
            $sort = 'timestarted ASC, classname ASC';
1395
            $sort = 'timestarted ASC, classname ASC';
1395
        }
1396
        }
1396
        $params = ['now1' => time(), 'now2' => time()];
1397
        $params = ['now1' => $clock->time(), 'now2' => $clock->time()];
Línea 1397... Línea 1398...
1397
 
1398
 
1398
        $sql = "SELECT subquery.*
1399
        $sql = "SELECT subquery.*
1399
                  FROM (SELECT " . $DB->sql_concat("'s'", 'ts.id') . " as uniqueid,
1400
                  FROM (SELECT " . $DB->sql_concat("'s'", 'ts.id') . " as uniqueid,
1400
                               ts.id,
1401
                               ts.id,
Línea 1426... Línea 1427...
1426
     * Cleanup stale task metadata.
1427
     * Cleanup stale task metadata.
1427
     */
1428
     */
1428
    public static function cleanup_metadata() {
1429
    public static function cleanup_metadata() {
1429
        global $DB;
1430
        global $DB;
Línea -... Línea 1431...
-
 
1431
 
-
 
1432
        $clock = \core\di::get(\core\clock::class);
1430
 
1433
 
1431
        $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
1434
        $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
Línea 1432... Línea 1435...
1432
        $runningtasks = self::get_running_tasks();
1435
        $runningtasks = self::get_running_tasks();
1433
 
1436
 
1434
        foreach ($runningtasks as $runningtask) {
1437
        foreach ($runningtasks as $runningtask) {
1435
            if ($runningtask->timestarted > time() - HOURSECS) {
1438
            if ($runningtask->timestarted > $clock->time() - HOURSECS) {
Línea 1436... Línea 1439...
1436
                continue;
1439
                continue;
1437
            }
1440
            }
Línea 1467... Línea 1470...
1467
                        continue;
1470
                        continue;
1468
                    }
1471
                    }
Línea 1469... Línea 1472...
1469
 
1472
 
1470
                    $task = self::scheduled_task_from_record($taskrecord);
1473
                    $task = self::scheduled_task_from_record($taskrecord);
-
 
1474
                    $task->set_lock($lock);
-
 
1475
 
-
 
1476
                    // We have to skip log finalisation when failing the task as the finalise_log method from
1471
                    $task->set_lock($lock);
1477
                    // the log manager is only aware of the current running task (i.e., the cleanup task).
1472
                    self::scheduled_task_failed($task);
1478
                    self::scheduled_task_failed($task, false);
1473
                } else if ($runningtask->type == 'adhoc') {
1479
                } else if ($runningtask->type == 'adhoc') {
1474
                    // Ad hoc tasks are removed from the DB if they finish successfully.
1480
                    // Ad hoc tasks are removed from the DB if they finish successfully.
1475
                    // If we can't re-get this task, that means it finished and was properly
1481
                    // If we can't re-get this task, that means it finished and was properly
1476
                    // cleaned up.
1482
                    // cleaned up.
Línea 1479... Línea 1485...
1479
                        continue;
1485
                        continue;
1480
                    }
1486
                    }
Línea 1481... Línea 1487...
1481
 
1487
 
1482
                    $task = self::adhoc_task_from_record($taskrecord);
1488
                    $task = self::adhoc_task_from_record($taskrecord);
-
 
1489
                    $task->set_lock($lock);
-
 
1490
 
-
 
1491
                    // We have to skip log finalisation when failing the task as the finalise_log method from
1483
                    $task->set_lock($lock);
1492
                    // the log manager is only aware of the current running task (i.e., the cleanup task).
1484
                    self::adhoc_task_failed($task);
1493
                    self::adhoc_task_failed($task, false);
1485
                }
1494
                }
1486
            }
1495
            }
1487
        }
1496
        }
Línea 1492... Línea 1501...
1492
     * next opportunity and restart. This is because something (e.g. DB changes) has changed and
1501
     * next opportunity and restart. This is because something (e.g. DB changes) has changed and
1493
     * the static caches may be stale.
1502
     * the static caches may be stale.
1494
     */
1503
     */
1495
    public static function clear_static_caches() {
1504
    public static function clear_static_caches() {
1496
        global $DB;
1505
        global $DB;
-
 
1506
 
-
 
1507
        $clock = \core\di::get(\core\clock::class);
-
 
1508
 
1497
        // Do not use get/set config here because the caches cannot be relied on.
1509
        // Do not use get/set config here because the caches cannot be relied on.
1498
        $record = $DB->get_record('config', array('name'=>'scheduledtaskreset'));
1510
        $record = $DB->get_record('config', array('name'=>'scheduledtaskreset'));
1499
        if ($record) {
1511
        if ($record) {
1500
            $record->value = time();
1512
            $record->value = $clock->time();
1501
            $DB->update_record('config', $record);
1513
            $DB->update_record('config', $record);
1502
        } else {
1514
        } else {
1503
            $record = new \stdClass();
1515
            $record = new \stdClass();
1504
            $record->name = 'scheduledtaskreset';
1516
            $record->name = 'scheduledtaskreset';
1505
            $record->value = time();
1517
            $record->value = $clock->time();
1506
            $DB->insert_record('config', $record);
1518
            $DB->insert_record('config', $record);
1507
        }
1519
        }
1508
    }
1520
    }
Línea 1509... Línea 1521...
1509
 
1521
 
Línea 1562... Línea 1574...
1562
     */
1574
     */
1563
    protected static function find_php_cli_path() {
1575
    protected static function find_php_cli_path() {
1564
        global $CFG;
1576
        global $CFG;
Línea 1565... Línea 1577...
1565
 
1577
 
1566
        if (!empty($CFG->pathtophp) && is_executable(trim($CFG->pathtophp))) {
1578
        if (!empty($CFG->pathtophp) && is_executable(trim($CFG->pathtophp))) {
1567
            return $CFG->pathtophp;
1579
            return trim($CFG->pathtophp);
Línea 1568... Línea 1580...
1568
        }
1580
        }
1569
 
1581
 
Línea 1800... Línea 1812...
1800
    /**
1812
    /**
1801
     * Clean up failed adhoc tasks.
1813
     * Clean up failed adhoc tasks.
1802
     */
1814
     */
1803
    public static function clean_failed_adhoc_tasks(): void {
1815
    public static function clean_failed_adhoc_tasks(): void {
1804
        global $CFG, $DB;
1816
        global $CFG, $DB;
-
 
1817
 
-
 
1818
        $clock = \core\di::get(\core\clock::class);
-
 
1819
 
1805
        $difftime = !empty($CFG->task_adhoc_failed_retention) ?
1820
        $difftime = !empty($CFG->task_adhoc_failed_retention) ?
1806
            $CFG->task_adhoc_failed_retention : static::ADHOC_TASK_FAILED_RETENTION;
1821
            $CFG->task_adhoc_failed_retention : static::ADHOC_TASK_FAILED_RETENTION;
1807
        $DB->delete_records_select(
1822
        $DB->delete_records_select(
1808
            table: 'task_adhoc',
1823
            table: 'task_adhoc',
1809
            select: 'attemptsavailable = 0 AND firststartingtime < :time',
1824
            select: 'attemptsavailable = 0 AND firststartingtime < :time',
1810
            params: ['time' => time() - $difftime],
1825
            params: ['time' => $clock->time() - $difftime],
1811
        );
1826
        );
1812
    }
1827
    }
1813
}
1828
}