Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core;
18
 
19
use coding_exception;
20
use core_php_time_limit;
21
use moodle_exception;
22
use stdClass;
23
 
24
// Disable the moodle.PHP.ForbiddenFunctions.FoundWithAlternative sniff for this file.
25
// It detects uses of error_log() which are valid in this file.
26
// phpcs:disable moodle.PHP.ForbiddenFunctions.FoundWithAlternative
27
 
28
/**
29
 * Cron and adhoc task functionality.
30
 *
31
 * @package    core
32
 * @copyright  2023 Andrew Lyons <andrew@nicols.co.uk>
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class cron {
36
 
37
    /** @var ?stdClass A copy of the standard cron 'user' */
38
    protected static ?stdClass $cronuser = null;
39
 
40
    /** @var ?stdClass The cron user's session data */
41
    protected static ?stdClass $cronsession = null;
42
 
43
    /**
44
     * Use a default value of 3 minutes.
45
     * The recommended cron frequency is every minute, and the default adhoc concurrency is 3.
46
     * A default value of 3 minutes allows all adhoc tasks to be run concurrently at their default value.
47
     *
48
     * @var int The default keepalive value for the main cron runner
49
     */
50
    public const DEFAULT_MAIN_PROCESS_KEEPALIVE = 3 * MINSECS;
51
 
52
    /**
53
     * @var int The max keepalive value for the main cron runner
54
     */
55
    public const MAX_MAIN_PROCESS_KEEPALIVE = 15 * MINSECS;
56
 
57
    /**
58
     * Execute cron tasks
59
     *
60
     * @param int|null $keepalive The keepalive time for this cron run.
61
     */
62
    public static function run_main_process(?int $keepalive = null): void {
63
        global $CFG, $DB;
64
 
65
        if (CLI_MAINTENANCE) {
66
            echo "CLI maintenance mode active, cron execution suspended.\n";
67
            exit(1);
68
        }
69
 
70
        if (moodle_needs_upgrading()) {
71
            echo "Moodle upgrade pending, cron execution suspended.\n";
72
            exit(1);
73
        }
74
 
75
        require_once($CFG->libdir . '/adminlib.php');
76
 
77
        if (!empty($CFG->showcronsql)) {
78
            $DB->set_debug(true);
79
        }
80
        if (!empty($CFG->showcrondebugging)) {
81
            set_debugging(DEBUG_DEVELOPER, true);
82
        }
83
 
84
        core_php_time_limit::raise();
85
 
86
        // Increase memory limit.
87
        raise_memory_limit(MEMORY_EXTRA);
88
 
89
        // Emulate normal session. - we use admin account by default.
90
        self::setup_user();
91
 
92
        // Start output log.
93
        $timenow = time();
94
        mtrace("Server Time: " . date('r', $timenow) . "\n\n");
95
 
96
        // Record start time and interval between the last cron runs.
97
        $laststart = get_config('tool_task', 'lastcronstart');
98
        set_config('lastcronstart', $timenow, 'tool_task');
99
        if ($laststart) {
100
            // Record the interval between last two runs (always store at least 1 second).
101
            set_config('lastcroninterval', max(1, $timenow - $laststart), 'tool_task');
102
        }
103
 
104
        // Determine the time when the cron should finish.
105
        if ($keepalive === null) {
106
            $keepalive = get_config('core', 'cron_keepalive');
107
            if ($keepalive === false) {
108
                $keepalive = self::DEFAULT_MAIN_PROCESS_KEEPALIVE;
109
            }
110
        }
111
 
112
        if ($keepalive > self::MAX_MAIN_PROCESS_KEEPALIVE) {
113
            // Attempt to prevent abnormally long keepalives.
114
            mtrace("Cron keepalive time is too long, reducing to 15 minutes.");
115
            $keepalive = self::MAX_MAIN_PROCESS_KEEPALIVE;
116
        }
117
 
118
        // Calculate the finish time based on the start time and keepalive.
119
        $finishtime = $timenow + $keepalive;
120
 
121
        do {
122
            $startruntime = microtime();
123
 
124
            // Run all scheduled tasks.
125
            self::run_scheduled_tasks(time(), $timenow);
126
 
127
            // Run adhoc tasks.
128
            self::run_adhoc_tasks(time(), 0, true, $timenow);
129
 
130
            mtrace("Cron run completed correctly");
131
 
132
            gc_collect_cycles();
133
 
134
            $completiontime = date('H:i:s');
135
            $difftime = microtime_diff($startruntime, microtime());
136
            $memoryused = display_size(memory_get_usage());
137
 
138
            $message = "Cron completed at {$completiontime} in {$difftime} seconds. Memory used: {$memoryused}.";
139
 
140
            // Check if we should continue to run.
141
            // Only continue to run if:
142
            // - The finish time has not been reached; and
143
            // - The graceful exit flag has not been set; and
144
            // - The static caches have not been cleared since the start of the cron run.
145
            $remaining = $finishtime - time();
146
            $runagain = $remaining > 0;
147
            $runagain = $runagain && !\core\local\cli\shutdown::should_gracefully_exit();
148
            $runagain = $runagain && !\core\task\manager::static_caches_cleared_since($timenow);
149
 
150
            if ($runagain) {
151
                $message .= " Continuing to check for tasks for {$remaining} more seconds.";
152
                mtrace($message);
153
                sleep(1);
154
 
155
                // Re-check the graceful exit and cache clear flags after sleeping as these may have changed.
156
                $runagain = $runagain && !\core\local\cli\shutdown::should_gracefully_exit();
157
                $runagain = $runagain && !\core\task\manager::static_caches_cleared_since($timenow);
158
            } else {
159
                mtrace($message);
160
            }
161
        } while ($runagain);
162
    }
163
 
164
    /**
165
     * Execute all queued scheduled tasks, applying necessary concurrency limits and time limits.
166
     *
167
     * @param   int       $startruntime The time this run started.
168
     * @param   null|int  $startprocesstime The time the process that owns this runner started.
169
     * @throws \moodle_exception
170
     */
171
    public static function run_scheduled_tasks(
172
        int $startruntime,
173
        ?int $startprocesstime = null,
174
    ): void {
175
        // Allow a restriction on the number of scheduled task runners at once.
176
        $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
177
        $maxruns = get_config('core', 'task_scheduled_concurrency_limit');
178
        $maxruntime = get_config('core', 'task_scheduled_max_runtime');
179
 
180
        if ($startprocesstime === null) {
181
            $startprocesstime = $startruntime;
182
        }
183
 
184
        $scheduledlock = null;
185
        for ($run = 0; $run < $maxruns; $run++) {
186
            // If we can't get a lock instantly it means runner N is already running
187
            // so fail as fast as possible and try N+1 so we don't limit the speed at
188
            // which we bring new runners into the pool.
189
            if ($scheduledlock = $cronlockfactory->get_lock("scheduled_task_runner_{$run}", 0)) {
190
                break;
191
            }
192
        }
193
 
194
        if (!$scheduledlock) {
195
            mtrace("Skipping processing of scheduled tasks. Concurrency limit reached.");
196
            return;
197
        }
198
 
199
        $starttime = time();
200
 
201
        // Run all scheduled tasks.
202
        try {
203
            while (
204
                !\core\local\cli\shutdown::should_gracefully_exit() &&
205
                !\core\task\manager::static_caches_cleared_since($startprocesstime) &&
206
                $task = \core\task\manager::get_next_scheduled_task($startruntime)
207
            ) {
208
                self::run_inner_scheduled_task($task);
209
                unset($task);
210
 
211
                if ((time() - $starttime) > $maxruntime) {
212
                    mtrace("Stopping processing of scheduled tasks as time limit has been reached.");
213
                    break;
214
                }
215
            }
216
        } finally {
217
            // Release the scheduled task runner lock.
218
            $scheduledlock->release();
219
        }
220
    }
221
 
222
    /**
223
     * Execute all queued adhoc tasks, applying necessary concurrency limits and time limits.
224
     *
225
     * @param   int     $startruntime The time this run started.
226
     * @param   int     $keepalive Keep this public static function alive for N seconds and poll for new adhoc tasks.
227
     * @param   bool    $checklimits Should we check limits?
228
     * @param   null|int $startprocesstime The time this process started.
229
     * @param   int|null $maxtasks Limit number of tasks to run`
230
     * @param   null|string $classname Run only tasks of this class
231
     * @throws \moodle_exception
232
     */
233
    public static function run_adhoc_tasks(
234
        int $startruntime,
235
        $keepalive = 0,
236
        $checklimits = true,
237
        ?int $startprocesstime = null,
238
        ?int $maxtasks = null,
239
        ?string $classname = null,
240
    ): void {
241
        // Allow a restriction on the number of adhoc task runners at once.
242
        $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
243
        $maxruns = get_config('core', 'task_adhoc_concurrency_limit');
244
        $maxruntime = get_config('core', 'task_adhoc_max_runtime');
245
 
246
        if ($startprocesstime === null) {
247
            $startprocesstime = $startruntime;
248
        }
249
 
250
        $adhoclock = null;
251
        if ($checklimits) {
252
            for ($run = 0; $run < $maxruns; $run++) {
253
                // If we can't get a lock instantly it means runner N is already running
254
                // so fail as fast as possible and try N+1 so we don't limit the speed at
255
                // which we bring new runners into the pool.
256
                if ($adhoclock = $cronlockfactory->get_lock("adhoc_task_runner_{$run}", 0)) {
257
                    break;
258
                }
259
            }
260
 
261
            if (!$adhoclock) {
262
                mtrace("Skipping processing of adhoc tasks. Concurrency limit reached.");
263
                return;
264
            }
265
        }
266
 
267
        $humantimenow = date('r', $startruntime);
268
        $finishtime = $startruntime + $keepalive;
269
        $waiting = false;
270
        $taskcount = 0;
271
 
272
        // Run all adhoc tasks.
273
        while (
274
            !\core\local\cli\shutdown::should_gracefully_exit() &&
275
            !\core\task\manager::static_caches_cleared_since($startprocesstime)
276
        ) {
277
 
278
            if ($checklimits && (time() - $startruntime) >= $maxruntime) {
279
                if ($waiting) {
280
                    $waiting = false;
281
                    mtrace('');
282
                }
283
                mtrace("Stopping processing of adhoc tasks as time limit has been reached.");
284
                break;
285
            }
286
 
287
            try {
288
                $task = \core\task\manager::get_next_adhoc_task(time(), $checklimits, $classname);
289
            } catch (\Throwable $e) {
290
                if ($adhoclock) {
291
                    // Release the adhoc task runner lock.
292
                    $adhoclock->release();
293
                }
294
                throw $e;
295
            }
296
 
297
            if ($task) {
298
                if ($waiting) {
299
                    mtrace('');
300
                }
301
                $waiting = false;
302
                self::run_inner_adhoc_task($task);
303
                self::set_process_title("Waiting for next adhoc task");
304
                $taskcount++;
305
                if ($maxtasks && $taskcount >= $maxtasks) {
306
                    break;
307
                }
308
                unset($task);
309
            } else {
310
                $timeleft = $finishtime - time();
311
                if ($timeleft <= 0) {
312
                    break;
313
                }
314
                if (!$waiting) {
315
                    mtrace('Waiting for more adhoc tasks to be queued ', '');
316
                } else {
317
                    mtrace('.', '');
318
                }
319
                $waiting = true;
320
                self::set_process_title("Waiting {$timeleft}s for next adhoc task");
321
                sleep(1);
322
            }
323
        }
324
 
325
        if ($waiting) {
326
            mtrace('');
327
        }
328
 
329
        mtrace("Ran {$taskcount} adhoc tasks found at {$humantimenow}");
330
 
331
        if ($adhoclock) {
332
            // Release the adhoc task runner lock.
333
            $adhoclock->release();
334
        }
335
    }
336
 
337
    /**
338
     * Execute an adhoc task.
339
     *
340
     * @param   int       $taskid
341
     */
342
    public static function run_adhoc_task(int $taskid): void {
343
        $task = \core\task\manager::get_adhoc_task($taskid);
344
        if (!$task->get_fail_delay() && $task->get_next_run_time() > time()) {
345
            throw new \moodle_exception('wontrunfuturescheduledtask');
346
        }
347
 
348
        self::run_inner_adhoc_task($task);
349
        self::set_process_title("Running adhoc task $taskid");
350
    }
351
 
352
    /**
353
     * Execute all failed adhoc tasks.
354
     *
355
     * @param string|null  $classname Run only tasks of this class
356
     */
357
    public static function run_failed_adhoc_tasks(?string $classname = null): void {
358
        global $DB;
359
 
360
        $where = 'faildelay > 0';
361
        $params = [];
362
        if ($classname) {
363
            $where .= ' AND classname = :classname';
364
            $params['classname'] = \core\task\manager::get_canonical_class_name($classname);
365
        }
366
 
367
        // Only rerun the failed tasks that allow to be re-tried or have the remaining attempts available.
368
        $where .= ' AND (attemptsavailable > 0 OR attemptsavailable IS NULL)';
369
        $tasks = $DB->get_records_sql("SELECT * from {task_adhoc} WHERE $where", $params);
370
        foreach ($tasks as $t) {
371
            self::run_adhoc_task($t->id);
372
        }
373
    }
374
 
375
    /**
376
     * Shared code that handles running of a single scheduled task within the cron.
377
     *
378
     * Not intended for calling directly outside of this library!
379
     *
380
     * @param \core\task\task_base $task
381
     */
382
    public static function run_inner_scheduled_task(\core\task\task_base $task) {
383
        global $CFG, $DB;
384
        $debuglevel = $CFG->debug;
385
        $debugdisplay = $CFG->debugdisplay;
386
        $CFG->debugdisplay = 1;
387
 
388
        \core\task\manager::scheduled_task_starting($task);
389
        \core\task\logmanager::start_logging($task);
390
 
391
        $fullname = $task->get_name() . ' (' . get_class($task) . ')';
392
        mtrace('Execute scheduled task: ' . $fullname);
393
        self::set_process_title('Scheduled task: ' . get_class($task));
394
        self::trace_time_and_memory();
395
        $predbqueries = null;
396
        $predbqueries = $DB->perf_get_queries();
397
        $pretime = microtime(1);
398
 
399
        // Ensure that we have a clean session with the correct cron user.
400
        self::setup_user();
401
 
402
        try {
403
            get_mailer('buffer');
404
            self::prepare_core_renderer();
405
            // Temporarily increase debug level if task has failed and debugging isn't already at maximum.
406
            if ($debuglevel !== DEBUG_DEVELOPER && $faildelay = $task->get_fail_delay()) {
407
                mtrace('Debugging increased temporarily due to faildelay of ' . $faildelay);
408
                set_debugging(DEBUG_DEVELOPER, 1);
409
            }
410
            $task->execute();
411
            if ($DB->is_transaction_started()) {
412
                throw new coding_exception("Task left transaction open");
413
            }
414
            if (isset($predbqueries)) {
415
                mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
416
                mtrace("... used " . (microtime(1) - $pretime) . " seconds");
417
            }
418
            mtrace('Scheduled task complete: ' . $fullname);
419
            \core\task\manager::scheduled_task_complete($task);
420
        } catch (\Throwable $e) {
421
            if ($DB && $DB->is_transaction_started()) {
422
                error_log('Database transaction aborted automatically in ' . get_class($task));
423
                $DB->force_transaction_rollback();
424
            }
425
            if (isset($predbqueries)) {
426
                mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
427
                mtrace("... used " . (microtime(1) - $pretime) . " seconds");
428
            }
429
            mtrace('Scheduled task failed: ' . $fullname . ',' . $e->getMessage());
430
            if ($CFG->debugdeveloper) {
431
                if (!empty($e->debuginfo)) {
432
                    mtrace("Debug info:");
433
                    mtrace($e->debuginfo);
434
                }
435
                mtrace("Backtrace:");
436
                mtrace(format_backtrace($e->getTrace(), true));
437
            }
438
            \core\task\manager::scheduled_task_failed($task);
439
        } finally {
440
            // Reset debugging if it changed.
441
            if ($CFG->debug !== $debuglevel) {
442
                set_debugging($debuglevel);
443
            }
444
 
445
            // Reset debugdisplay back.
446
            $CFG->debugdisplay = $debugdisplay;
447
 
448
            // Reset back to the standard admin user.
449
            self::setup_user();
450
            self::set_process_title('Waiting for next scheduled task');
451
            self::prepare_core_renderer(true);
452
        }
453
        get_mailer('close');
454
    }
455
 
456
    /**
457
     * Shared code that handles running of a single adhoc task within the cron.
458
     *
459
     * @param \core\task\adhoc_task $task
460
     */
461
    public static function run_inner_adhoc_task(\core\task\adhoc_task $task) {
462
        global $CFG, $DB;
463
        $debuglevel = $CFG->debug;
464
        $debugdisplay = $CFG->debugdisplay;
465
        $CFG->debugdisplay = 1;
466
 
467
        \core\task\manager::adhoc_task_starting($task);
468
        \core\task\logmanager::start_logging($task);
469
 
470
        mtrace("Execute adhoc task: " . get_class($task));
471
        mtrace("Adhoc task id: " . $task->get_id());
472
        mtrace("Adhoc task custom data: " . $task->get_custom_data_as_string());
473
        self::set_process_title('Adhoc task: ' . $task->get_id() . ' ' . get_class($task));
474
        self::trace_time_and_memory();
475
        $predbqueries = null;
476
        $predbqueries = $DB->perf_get_queries();
477
        $pretime = microtime(1);
478
 
479
        if ($userid = $task->get_userid()) {
480
            // This task has a userid specified.
481
            if ($user = \core_user::get_user($userid)) {
482
                // User found. Check that they are suitable.
483
                try {
484
                    \core_user::require_active_user($user, true, true);
485
                } catch (moodle_exception $e) {
486
                    mtrace("User {$userid} cannot be used to run an adhoc task: " . get_class($task) . ". Cancelling task.");
487
                    $user = null;
488
                }
489
            } else {
490
                // Unable to find the user for this task.
491
                // A user missing in the database will never reappear.
492
                mtrace("User {$userid} could not be found for adhoc task: " . get_class($task) . ". Cancelling task.");
493
            }
494
 
495
            if (empty($user)) {
496
                // A user missing in the database will never reappear so the task needs to be failed to ensure that locks are
497
                // removed, and then removed to prevent future runs.
498
                // A task running as a user should only be run as that user.
499
                \core\task\manager::adhoc_task_failed($task);
500
                $DB->delete_records('task_adhoc', ['id' => $task->get_id()]);
501
 
502
                return;
503
            }
504
 
505
            self::setup_user($user);
506
        } else {
507
            // No user specified, ensure that we have a clean session with the correct cron user.
508
            self::setup_user();
509
        }
510
 
511
        try {
512
            get_mailer('buffer');
513
            self::prepare_core_renderer();
514
            // Temporarily increase debug level if task has failed and debugging isn't already at maximum.
515
            if ($debuglevel !== DEBUG_DEVELOPER && $faildelay = $task->get_fail_delay()) {
516
                mtrace('Debugging increased temporarily due to faildelay of ' . $faildelay);
517
                set_debugging(DEBUG_DEVELOPER, 1);
518
            }
519
            $task->execute();
520
            if ($DB->is_transaction_started()) {
521
                throw new coding_exception("Task left transaction open");
522
            }
523
            if (isset($predbqueries)) {
524
                mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
525
                mtrace("... used " . (microtime(1) - $pretime) . " seconds");
526
            }
527
            mtrace("Adhoc task complete: " . get_class($task));
528
            \core\task\manager::adhoc_task_complete($task);
529
        } catch (\Throwable $e) {
530
            if ($DB && $DB->is_transaction_started()) {
531
                error_log('Database transaction aborted automatically in ' . get_class($task));
532
                $DB->force_transaction_rollback();
533
            }
534
            if (isset($predbqueries)) {
535
                mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
536
                mtrace("... used " . (microtime(1) - $pretime) . " seconds");
537
            }
538
            mtrace("Adhoc task failed: " . get_class($task) . "," . $e->getMessage());
539
            if ($CFG->debugdeveloper) {
540
                if (!empty($e->debuginfo)) {
541
                    mtrace("Debug info:");
542
                    mtrace($e->debuginfo);
543
                }
544
                mtrace("Backtrace:");
545
                mtrace(format_backtrace($e->getTrace(), true));
546
            }
547
            \core\task\manager::adhoc_task_failed($task);
548
        } finally {
549
            // Reset debug level if it changed.
550
            if ($CFG->debug !== $debuglevel) {
551
                set_debugging($debuglevel);
552
            }
553
 
554
            // Reset debugdisplay back.
555
            $CFG->debugdisplay = $debugdisplay;
556
 
557
            // Reset back to the standard admin user.
558
            self::setup_user();
559
            self::prepare_core_renderer(true);
560
        }
561
        get_mailer('close');
562
    }
563
 
564
    /**
565
     * Sets the process title
566
     *
567
     * This makes it very easy for a sysadmin to immediately see what task
568
     * a cron process is running at any given moment.
569
     *
570
     * @param string $title process status title
571
     */
572
    public static function set_process_title(string $title) {
573
        global $CFG;
574
        if (CLI_SCRIPT) {
575
            require_once($CFG->libdir . '/clilib.php');
576
            $datetime = userdate(time(), '%b %d, %H:%M:%S');
577
            cli_set_process_title_suffix("$datetime $title");
578
        }
579
    }
580
 
581
    /**
582
     * Output some standard information during cron runs. Specifically current time
583
     * and memory usage. This method also does gc_collect_cycles() (before displaying
584
     * memory usage) to try to help PHP manage memory better.
585
     */
586
    public static function trace_time_and_memory() {
587
        gc_collect_cycles();
588
        mtrace('... started ' . date('H:i:s') . '. Current memory use ' . display_size(memory_get_usage()) . '.');
589
    }
590
 
591
    /**
592
     * Prepare the output renderer for the cron run.
593
     *
594
     * This involves creating a new $PAGE, and $OUTPUT fresh for each task and prevents any one task from influencing
595
     * any other.
596
     *
597
     * @param   bool    $restore Whether to restore the original PAGE and OUTPUT
598
     */
599
    public static function prepare_core_renderer($restore = false) {
600
        global $OUTPUT, $PAGE;
601
 
602
        // Store the original PAGE and OUTPUT values so that they can be reset at a later point to the original.
603
        // This should not normally be required, but may be used in places such as the scheduled task tool's "Run now"
604
        // functionality.
605
        static $page = null;
606
        static $output = null;
607
 
608
        if (null === $page) {
609
            $page = $PAGE;
610
        }
611
 
612
        if (null === $output) {
613
            $output = $OUTPUT;
614
        }
615
 
616
        if (!empty($restore)) {
617
            $PAGE = $page;
618
            $page = null;
619
 
620
            $OUTPUT = $output;
621
            $output = null;
622
        } else {
623
            // Setup a new General renderer.
624
            // Cron tasks may produce output to be used in web, so we must use the appropriate renderer target.
625
            // This allows correct use of templates, etc.
626
            $PAGE = new \moodle_page();
627
            $OUTPUT = new \core_renderer($PAGE, RENDERER_TARGET_GENERAL);
628
        }
629
    }
630
 
631
    /**
632
     * Sets up a user and course environment in cron.
633
     *
634
     * Note: This function is intended only for use in:
635
     * - the cron runner scripts
636
     * - individual tasks which extend the adhoc_task and scheduled_task classes
637
     * - unit tests related to tasks
638
     * - other parts of the cron/task system
639
     *
640
     * Please note that this function stores cache data statically.
641
     * @see reset_user_cache() to reset this cache.
642
     *
643
     * @param null|stdClass $user full user object, null means default cron user (admin)
644
     * @param null|stdClass $course full course record, null means $SITE
645
     * @param null|bool $leavepagealone If specified, stops it messing with global page object
646
     */
647
    public static function setup_user(?stdClass $user = null, ?stdClass $course = null, bool $leavepagealone = false): void {
648
        // This function uses the $GLOBALS super global. Disable the VariableNameLowerCase sniff for this function.
649
        // phpcs:disable moodle.NamingConventions.ValidVariableName.VariableNameLowerCase
650
        global $CFG, $SITE, $PAGE;
651
 
652
        if (!CLI_SCRIPT && !$leavepagealone) {
653
            throw new coding_exception('It is not possible to use \core\cron\setup_user() in normal requests!');
654
        }
655
 
656
        if (empty(self::$cronuser)) {
657
            // The cron user is essentially the admin user, but with some value removed.
658
            // We ginore the timezone language, and locale preferences - use the site default instead.
659
            self::$cronuser = get_admin();
660
            self::$cronuser->timezone = $CFG->timezone;
661
            self::$cronuser->lang = '';
662
            self::$cronuser->theme = '';
663
            unset(self::$cronuser->description);
664
 
665
            self::$cronsession = new stdClass();
666
        }
667
 
668
        if (!$user) {
669
            // Cached default cron user (==modified admin for now).
670
            \core\session\manager::init_empty_session();
671
            \core\session\manager::set_user(self::$cronuser);
672
            $GLOBALS['SESSION'] = self::$cronsession;
673
        } else {
674
            // Emulate real user session - needed for caps in cron.
675
            if ($GLOBALS['USER']->id != $user->id) {
676
                \core\session\manager::init_empty_session();
677
                \core\session\manager::set_user($user);
678
            }
679
        }
680
 
681
        // TODO MDL-19774 relying on global $PAGE in cron is a bad idea.
682
        // Temporary hack so that cron does not give fatal errors.
683
        if (!$leavepagealone) {
684
            $PAGE = new \moodle_page();
685
            $PAGE->set_course($course ?? $SITE);
686
        }
687
 
688
        // TODO: it should be possible to improve perf by caching some limited number of users here.
689
        // phpcs:enable
690
    }
691
 
692
    /**
693
     * Resets the cache for the cron user used by `setup_user()`.
694
     */
695
    public static function reset_user_cache(): void {
696
        self::$cronuser = null;
697
        self::$cronsession = null;
698
        \core\session\manager::init_empty_session();
699
    }
700
}