weekly release 3.9dev
[moodle.git] / lib / cronlib.php
CommitLineData
28bd3d9a 1<?php
28bd3d9a
PS
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/**
18 * Cron functions.
19 *
20 * @package core
21 * @subpackage admin
22 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
50ed196e
PS
26/**
27 * Execute cron tasks
28 */
28bd3d9a
PS
29function cron_run() {
30 global $DB, $CFG, $OUTPUT;
31
9957af68
PS
32 if (CLI_MAINTENANCE) {
33 echo "CLI maintenance mode active, cron execution suspended.\n";
34 exit(1);
35 }
36
4c56c7bf
PS
37 if (moodle_needs_upgrading()) {
38 echo "Moodle upgrade pending, cron execution suspended.\n";
39 exit(1);
40 }
41
28bd3d9a 42 require_once($CFG->libdir.'/adminlib.php');
28bd3d9a
PS
43
44 if (!empty($CFG->showcronsql)) {
45 $DB->set_debug(true);
46 }
47 if (!empty($CFG->showcrondebugging)) {
96f81ea3 48 set_debugging(DEBUG_DEVELOPER, true);
28bd3d9a
PS
49 }
50
3ef7279f 51 core_php_time_limit::raise();
28bd3d9a
PS
52 $starttime = microtime();
53
50ed196e 54 // Increase memory limit
346c5887 55 raise_memory_limit(MEMORY_EXTRA);
28bd3d9a 56
50ed196e 57 // Emulate normal session - we use admin accoutn by default
28bd3d9a
PS
58 cron_setup_user();
59
50ed196e 60 // Start output log
28bd3d9a 61 $timenow = time();
658b9372 62 mtrace("Server Time: ".date('r', $timenow)."\n\n");
28bd3d9a 63
b18034ed 64 // Record start time and interval between the last cron runs.
65 $laststart = get_config('tool_task', 'lastcronstart');
66 set_config('lastcronstart', $timenow, 'tool_task');
67 if ($laststart) {
68 // Record the interval between last two runs (always store at least 1 second).
69 set_config('lastcroninterval', max(1, $timenow - $laststart), 'tool_task');
70 }
71
309ae892 72 // Run all scheduled tasks.
23d67562 73 cron_run_scheduled_tasks($timenow);
50ed196e 74
24bfd53c
AN
75 // Run adhoc tasks.
76 cron_run_adhoc_tasks($timenow);
50ed196e 77
28bd3d9a
PS
78 mtrace("Cron script completed correctly");
79
658b9372
TH
80 gc_collect_cycles();
81 mtrace('Cron completed at ' . date('H:i:s') . '. Memory used ' . display_size(memory_get_usage()) . '.');
28bd3d9a
PS
82 $difftime = microtime_diff($starttime, microtime());
83 mtrace("Execution took ".$difftime." seconds");
cbb9e9b8 84}
b5541735 85
23d67562
AN
86/**
87 * Execute all queued scheduled tasks, applying necessary concurrency limits and time limits.
88 *
89 * @param int $timenow The time this process started.
90 */
91function cron_run_scheduled_tasks(int $timenow) {
92 // Allow a restriction on the number of scheduled task runners at once.
93 $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
94 $maxruns = get_config('core', 'task_scheduled_concurrency_limit');
95 $maxruntime = get_config('core', 'task_scheduled_max_runtime');
96
97 $scheduledlock = null;
98 for ($run = 0; $run < $maxruns; $run++) {
99 if ($scheduledlock = $cronlockfactory->get_lock("scheduled_task_runner_{$run}", 1)) {
100 break;
101 }
102 }
103
104 if (!$scheduledlock) {
105 mtrace("Skipping processing of scheduled tasks. Concurrency limit reached.");
106 return;
107 }
108
109 $starttime = time();
110
111 // Run all scheduled tasks.
112 while (!\core\task\manager::static_caches_cleared_since($timenow) &&
113 $task = \core\task\manager::get_next_scheduled_task($timenow)) {
114 cron_run_inner_scheduled_task($task);
115 unset($task);
116
117 if ((time() - $starttime) > $maxruntime) {
118 mtrace("Stopping processing of scheduled tasks as time limit has been reached.");
119 break;
120 }
121 }
122
123 // Release the scheduled task runner lock.
124 $scheduledlock->release();
125}
126
24bfd53c
AN
127/**
128 * Execute all queued adhoc tasks, applying necessary concurrency limits and time limits.
129 *
130 * @param int $timenow The time this process started.
4234159e
MG
131 * @param int $keepalive Keep this function alive for N seconds and poll for new adhoc tasks.
132 * @param bool $checklimits Should we check limits?
24bfd53c 133 */
4234159e 134function cron_run_adhoc_tasks(int $timenow, $keepalive = 0, $checklimits = true) {
24bfd53c
AN
135 // Allow a restriction on the number of adhoc task runners at once.
136 $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
137 $maxruns = get_config('core', 'task_adhoc_concurrency_limit');
138 $maxruntime = get_config('core', 'task_adhoc_max_runtime');
139
4234159e
MG
140 if ($checklimits) {
141 $adhoclock = null;
142 for ($run = 0; $run < $maxruns; $run++) {
143 if ($adhoclock = $cronlockfactory->get_lock("adhoc_task_runner_{$run}", 1)) {
144 break;
145 }
24bfd53c 146 }
24bfd53c 147
4234159e
MG
148 if (!$adhoclock) {
149 mtrace("Skipping processing of adhoc tasks. Concurrency limit reached.");
150 return;
151 }
24bfd53c
AN
152 }
153
4234159e
MG
154 $humantimenow = date('r', $timenow);
155 $finishtime = $timenow + $keepalive;
156 $waiting = false;
157 $taskcount = 0;
24bfd53c
AN
158
159 // Run all adhoc tasks.
4234159e 160 while (!\core\task\manager::static_caches_cleared_since($timenow)) {
24bfd53c 161
4234159e
MG
162 if ($checklimits && (time() - $timenow) >= $maxruntime) {
163 if ($waiting) {
164 $waiting = false;
165 mtrace('');
166 }
24bfd53c
AN
167 mtrace("Stopping processing of adhoc tasks as time limit has been reached.");
168 break;
169 }
4234159e
MG
170
171 $task = \core\task\manager::get_next_adhoc_task(time());
172
173 if ($task) {
174 if ($waiting) {
175 mtrace('');
176 }
177 $waiting = false;
178 cron_run_inner_adhoc_task($task);
179 $taskcount++;
180 unset($task);
181 } else {
182 if (time() >= $finishtime) {
183 break;
184 }
185 if (!$waiting) {
186 mtrace('Waiting for more adhoc tasks to be queued ', '');
187 } else {
188 mtrace('.', '');
189 }
190 $waiting = true;
191 sleep(1);
192 }
24bfd53c
AN
193 }
194
4234159e
MG
195 if ($waiting) {
196 mtrace('');
197 }
198
199 mtrace("Ran {$taskcount} adhoc tasks found at {$humantimenow}");
200
201 if ($adhoclock) {
202 // Release the adhoc task runner lock.
203 $adhoclock->release();
204 }
24bfd53c
AN
205}
206
38fa1ca5 207/**
208 * Shared code that handles running of a single scheduled task within the cron.
209 *
210 * Not intended for calling directly outside of this library!
211 *
212 * @param \core\task\task_base $task
213 */
214function cron_run_inner_scheduled_task(\core\task\task_base $task) {
215 global $CFG, $DB;
216
4b71596f
AN
217 \core\task\logmanager::start_logging($task);
218
38fa1ca5 219 $fullname = $task->get_name() . ' (' . get_class($task) . ')';
220 mtrace('Execute scheduled task: ' . $fullname);
221 cron_trace_time_and_memory();
222 $predbqueries = null;
223 $predbqueries = $DB->perf_get_queries();
224 $pretime = microtime(1);
225 try {
226 get_mailer('buffer');
cd305dc2 227 cron_prepare_core_renderer();
38fa1ca5 228 $task->execute();
229 if ($DB->is_transaction_started()) {
230 throw new coding_exception("Task left transaction open");
231 }
232 if (isset($predbqueries)) {
233 mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
234 mtrace("... used " . (microtime(1) - $pretime) . " seconds");
235 }
236 mtrace('Scheduled task complete: ' . $fullname);
237 \core\task\manager::scheduled_task_complete($task);
238 } catch (Exception $e) {
239 if ($DB && $DB->is_transaction_started()) {
240 error_log('Database transaction aborted automatically in ' . get_class($task));
241 $DB->force_transaction_rollback();
242 }
243 if (isset($predbqueries)) {
244 mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
245 mtrace("... used " . (microtime(1) - $pretime) . " seconds");
246 }
247 mtrace('Scheduled task failed: ' . $fullname . ',' . $e->getMessage());
248 if ($CFG->debugdeveloper) {
249 if (!empty($e->debuginfo)) {
250 mtrace("Debug info:");
251 mtrace($e->debuginfo);
252 }
253 mtrace("Backtrace:");
254 mtrace(format_backtrace($e->getTrace(), true));
255 }
256 \core\task\manager::scheduled_task_failed($task);
cd305dc2
AN
257 } finally {
258 cron_prepare_core_renderer(true);
38fa1ca5 259 }
260 get_mailer('close');
261}
262
7e4c4b6f
AN
263/**
264 * Shared code that handles running of a single adhoc task within the cron.
265 *
266 * @param \core\task\adhoc_task $task
267 */
268function cron_run_inner_adhoc_task(\core\task\adhoc_task $task) {
269 global $DB, $CFG;
4b71596f
AN
270
271 \core\task\logmanager::start_logging($task);
272
7e4c4b6f
AN
273 mtrace("Execute adhoc task: " . get_class($task));
274 cron_trace_time_and_memory();
275 $predbqueries = null;
276 $predbqueries = $DB->perf_get_queries();
277 $pretime = microtime(1);
278
279 if ($userid = $task->get_userid()) {
280 // This task has a userid specified.
281 if ($user = \core_user::get_user($userid)) {
282 // User found. Check that they are suitable.
283 try {
284 \core_user::require_active_user($user, true, true);
285 } catch (moodle_exception $e) {
286 mtrace("User {$userid} cannot be used to run an adhoc task: " . get_class($task) . ". Cancelling task.");
287 $user = null;
288 }
289 } else {
290 // Unable to find the user for this task.
291 // A user missing in the database will never reappear.
292 mtrace("User {$userid} could not be found for adhoc task: " . get_class($task) . ". Cancelling task.");
293 }
294
295 if (empty($user)) {
296 // A user missing in the database will never reappear so the task needs to be failed to ensure that locks are removed,
297 // and then removed to prevent future runs.
298 // A task running as a user should only be run as that user.
299 \core\task\manager::adhoc_task_failed($task);
300 $DB->delete_records('task_adhoc', ['id' => $task->get_id()]);
301
302 return;
303 }
304
305 cron_setup_user($user);
306 }
307
308 try {
309 get_mailer('buffer');
cd305dc2 310 cron_prepare_core_renderer();
7e4c4b6f
AN
311 $task->execute();
312 if ($DB->is_transaction_started()) {
313 throw new coding_exception("Task left transaction open");
314 }
315 if (isset($predbqueries)) {
316 mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
317 mtrace("... used " . (microtime(1) - $pretime) . " seconds");
318 }
319 mtrace("Adhoc task complete: " . get_class($task));
320 \core\task\manager::adhoc_task_complete($task);
321 } catch (Exception $e) {
322 if ($DB && $DB->is_transaction_started()) {
323 error_log('Database transaction aborted automatically in ' . get_class($task));
324 $DB->force_transaction_rollback();
325 }
326 if (isset($predbqueries)) {
327 mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
328 mtrace("... used " . (microtime(1) - $pretime) . " seconds");
329 }
330 mtrace("Adhoc task failed: " . get_class($task) . "," . $e->getMessage());
331 if ($CFG->debugdeveloper) {
332 if (!empty($e->debuginfo)) {
333 mtrace("Debug info:");
334 mtrace($e->debuginfo);
335 }
336 mtrace("Backtrace:");
337 mtrace(format_backtrace($e->getTrace(), true));
338 }
339 \core\task\manager::adhoc_task_failed($task);
340 } finally {
341 // Reset back to the standard admin user.
342 cron_setup_user();
cd305dc2 343 cron_prepare_core_renderer(true);
7e4c4b6f
AN
344 }
345 get_mailer('close');
346}
347
38fa1ca5 348/**
349 * Runs a single cron task. This function assumes it is displaying output in pseudo-CLI mode.
350 *
351 * The function will fail if the task is disabled.
352 *
353 * Warning: Because this function closes the browser session, it may not be safe to continue
354 * with other processing (other than displaying the rest of the page) after using this function!
355 *
356 * @param \core\task\scheduled_task $task Task to run
357 * @return bool True if cron run successful
358 */
359function cron_run_single_task(\core\task\scheduled_task $task) {
360 global $CFG, $DB, $USER;
361
362 if (CLI_MAINTENANCE) {
363 echo "CLI maintenance mode active, cron execution suspended.\n";
364 return false;
365 }
366
367 if (moodle_needs_upgrading()) {
368 echo "Moodle upgrade pending, cron execution suspended.\n";
369 return false;
370 }
371
372 // Check task and component is not disabled.
373 $taskname = get_class($task);
374 if ($task->get_disabled()) {
375 echo "Task is disabled ($taskname).\n";
376 return false;
377 }
378 $component = $task->get_component();
379 if ($plugininfo = core_plugin_manager::instance()->get_plugin_info($component)) {
6d7c06a9 380 if ($plugininfo->is_enabled() === false && !$task->get_run_if_component_disabled()) {
38fa1ca5 381 echo "Component is not enabled ($component).\n";
382 return false;
383 }
384 }
385
386 // Enable debugging features as per config settings.
387 if (!empty($CFG->showcronsql)) {
388 $DB->set_debug(true);
389 }
390 if (!empty($CFG->showcrondebugging)) {
391 set_debugging(DEBUG_DEVELOPER, true);
392 }
393
394 // Increase time and memory limits.
395 core_php_time_limit::raise();
396 raise_memory_limit(MEMORY_EXTRA);
397
398 // Switch to admin account for cron tasks, but close the session so we don't send this stuff
399 // to the browser.
400 session_write_close();
401 $realuser = clone($USER);
402 cron_setup_user(null, null, true);
403
404 // Get lock for cron task.
405 $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
406 if (!$cronlock = $cronlockfactory->get_lock('core_cron', 1)) {
407 echo "Unable to get cron lock.\n";
408 return false;
409 }
410 if (!$lock = $cronlockfactory->get_lock($taskname, 1)) {
411 $cronlock->release();
412 echo "Unable to get task lock for $taskname.\n";
413 return false;
414 }
415 $task->set_lock($lock);
416 if (!$task->is_blocking()) {
417 $cronlock->release();
418 } else {
419 $task->set_cron_lock($cronlock);
420 }
421
422 // Run actual tasks.
423 cron_run_inner_scheduled_task($task);
424
425 // Go back to real user account.
426 cron_setup_user($realuser, null, true);
427
428 return true;
429}
430
309ae892
DW
431/**
432 * Output some standard information during cron runs. Specifically current time
433 * and memory usage. This method also does gc_collect_cycles() (before displaying
434 * memory usage) to try to help PHP manage memory better.
435 */
436function cron_trace_time_and_memory() {
437 gc_collect_cycles();
438 mtrace('... started ' . date('H:i:s') . '. Current memory use ' . display_size(memory_get_usage()) . '.');
439}
440
e4a626b9 441/**
442 * Executes cron functions for a specific type of plugin.
50ed196e 443 *
e4a626b9 444 * @param string $plugintype Plugin type (e.g. 'report')
445 * @param string $description If specified, will display 'Starting (whatever)'
446 * and 'Finished (whatever)' lines, otherwise does not display
447 */
b5541735
PS
448function cron_execute_plugin_type($plugintype, $description = null) {
449 global $DB;
450
e4a626b9 451 // Get list from plugin => function for all plugins
452 $plugins = get_plugin_list_with_function($plugintype, 'cron');
453
454 // Modify list for backward compatibility (different files/names)
455 $plugins = cron_bc_hack_plugin_functions($plugintype, $plugins);
b5541735 456
e4a626b9 457 // Return if no plugins with cron function to process
b5541735
PS
458 if (!$plugins) {
459 return;
460 }
461
462 if ($description) {
463 mtrace('Starting '.$description);
464 }
465
466 foreach ($plugins as $component=>$cronfunction) {
b0d1d941 467 $dir = core_component::get_component_directory($component);
e4a626b9 468
469 // Get cron period if specified in version.php, otherwise assume every cron
b5541735
PS
470 $cronperiod = 0;
471 if (file_exists("$dir/version.php")) {
472 $plugin = new stdClass();
473 include("$dir/version.php");
474 if (isset($plugin->cron)) {
475 $cronperiod = $plugin->cron;
476 }
477 }
478
e4a626b9 479 // Using last cron and cron period, don't run if it already ran recently
b5541735 480 $lastcron = get_config($component, 'lastcron');
e4a626b9 481 if ($cronperiod && $lastcron) {
b5541735
PS
482 if ($lastcron + $cronperiod > time()) {
483 // do not execute cron yet
484 continue;
485 }
486 }
487
e4a626b9 488 mtrace('Processing cron function for ' . $component . '...');
658b9372 489 cron_trace_time_and_memory();
b5541735 490 $pre_dbqueries = $DB->perf_get_queries();
e4a626b9 491 $pre_time = microtime(true);
b5541735
PS
492
493 $cronfunction();
494
e4a626b9 495 mtrace("done. (" . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries, " .
496 round(microtime(true) - $pre_time, 2) . " seconds)");
b5541735
PS
497
498 set_config('lastcron', time(), $component);
3ef7279f 499 core_php_time_limit::raise();
b5541735
PS
500 }
501
502 if ($description) {
e4a626b9 503 mtrace('Finished ' . $description);
b5541735
PS
504 }
505}
506
e4a626b9 507/**
508 * Used to add in old-style cron functions within plugins that have not been converted to the
509 * new standard API. (The standard API is frankenstyle_name_cron() in lib.php; some types used
510 * cron.php and some used a different name.)
50ed196e 511 *
e4a626b9 512 * @param string $plugintype Plugin type e.g. 'report'
513 * @param array $plugins Array from plugin name (e.g. 'report_frog') to function name (e.g.
514 * 'report_frog_cron') for plugin cron functions that were already found using the new API
515 * @return array Revised version of $plugins that adds in any extra plugin functions found by
516 * looking in the older location
517 */
518function cron_bc_hack_plugin_functions($plugintype, $plugins) {
519 global $CFG; // mandatory in case it is referenced by include()d PHP script
520
521 if ($plugintype === 'report') {
522 // Admin reports only - not course report because course report was
523 // never implemented before, so doesn't need BC
bd3b3bba 524 foreach (core_component::get_plugin_list($plugintype) as $pluginname=>$dir) {
e4a626b9 525 $component = $plugintype . '_' . $pluginname;
b5541735 526 if (isset($plugins[$component])) {
e4a626b9 527 // We already have detected the function using the new API
b5541735
PS
528 continue;
529 }
530 if (!file_exists("$dir/cron.php")) {
e4a626b9 531 // No old style cron file present
b5541735
PS
532 continue;
533 }
534 include_once("$dir/cron.php");
e4a626b9 535 $cronfunction = $component . '_cron';
b5541735
PS
536 if (function_exists($cronfunction)) {
537 $plugins[$component] = $cronfunction;
538 } else {
e4a626b9 539 debugging("Invalid legacy cron.php detected in $component, " .
540 "please use lib.php instead");
b5541735
PS
541 }
542 }
b5541735 543 } else if (strpos($plugintype, 'grade') === 0) {
e4a626b9 544 // Detect old style cron function names
545 // Plugin gradeexport_frog used to use grade_export_frog_cron() instead of
546 // new standard API gradeexport_frog_cron(). Also applies to gradeimport, gradereport
bd3b3bba 547 foreach(core_component::get_plugin_list($plugintype) as $pluginname=>$dir) {
b5541735
PS
548 $component = $plugintype.'_'.$pluginname;
549 if (isset($plugins[$component])) {
e4a626b9 550 // We already have detected the function using the new API
b5541735
PS
551 continue;
552 }
553 if (!file_exists("$dir/lib.php")) {
554 continue;
555 }
556 include_once("$dir/lib.php");
e4a626b9 557 $cronfunction = str_replace('grade', 'grade_', $plugintype) . '_' .
558 $pluginname . '_cron';
b5541735
PS
559 if (function_exists($cronfunction)) {
560 $plugins[$component] = $cronfunction;
561 }
562 }
563 }
564
565 return $plugins;
50ed196e 566}
cd305dc2
AN
567
568/**
569 * Prepare the output renderer for the cron run.
570 *
571 * This involves creating a new $PAGE, and $OUTPUT fresh for each task and prevents any one task from influencing
572 * any other.
573 *
574 * @param bool $restore Whether to restore the original PAGE and OUTPUT
575 */
576function cron_prepare_core_renderer($restore = false) {
577 global $OUTPUT, $PAGE;
578
579 // Store the original PAGE and OUTPUT values so that they can be reset at a later point to the original.
580 // This should not normally be required, but may be used in places such as the scheduled task tool's "Run now"
581 // functionality.
582 static $page = null;
583 static $output = null;
584
585 if (null === $page) {
586 $page = $PAGE;
587 }
588
589 if (null === $output) {
590 $output = $OUTPUT;
591 }
592
593 if (!empty($restore)) {
594 $PAGE = $page;
595 $page = null;
596
597 $OUTPUT = $output;
598 $output = null;
599 } else {
600 // Setup a new General renderer.
601 // Cron tasks may produce output to be used in web, so we must use the appropriate renderer target.
602 // This allows correct use of templates, etc.
603 $PAGE = new \moodle_page();
604 $OUTPUT = new \core_renderer($PAGE, RENDERER_TARGET_GENERAL);
605 }
606}