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