MDL-25370 general cron support for nearly all plugin types
[moodle.git] / lib / cronlib.php
CommitLineData
28bd3d9a
PS
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Cron functions.
20 *
21 * @package core
22 * @subpackage admin
23 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27function cron_run() {
28 global $DB, $CFG, $OUTPUT;
29
9957af68
PS
30 if (CLI_MAINTENANCE) {
31 echo "CLI maintenance mode active, cron execution suspended.\n";
32 exit(1);
33 }
34
4c56c7bf
PS
35 if (moodle_needs_upgrading()) {
36 echo "Moodle upgrade pending, cron execution suspended.\n";
37 exit(1);
38 }
39
28bd3d9a
PS
40 require_once($CFG->libdir.'/adminlib.php');
41 require_once($CFG->libdir.'/gradelib.php');
42
43 if (!empty($CFG->showcronsql)) {
44 $DB->set_debug(true);
45 }
46 if (!empty($CFG->showcrondebugging)) {
47 $CFG->debug = DEBUG_DEVELOPER;
48 $CFG->debugdisplay = true;
49 }
50
51 set_time_limit(0);
52 $starttime = microtime();
53
41209c1e 54/// increase memory limit
346c5887 55 raise_memory_limit(MEMORY_EXTRA);
28bd3d9a
PS
56
57/// emulate normal session
58 cron_setup_user();
59
60/// Start output log
61
62 $timenow = time();
63
64 mtrace("Server Time: ".date('r',$timenow)."\n\n");
65
66
67/// Session gc
68
69 mtrace("Cleaning up stale sessions");
70 session_gc();
71
72/// Run all cron jobs for each module
73
74 mtrace("Starting activity modules");
75 get_mailer('buffer');
76 if ($mods = $DB->get_records_select("modules", "cron > 0 AND ((? - lastcron) > cron) AND visible = 1", array($timenow))) {
77 foreach ($mods as $mod) {
78 $libfile = "$CFG->dirroot/mod/$mod->name/lib.php";
79 if (file_exists($libfile)) {
80 include_once($libfile);
81 $cron_function = $mod->name."_cron";
82 if (function_exists($cron_function)) {
83 mtrace("Processing module function $cron_function ...", '');
84 $pre_dbqueries = null;
85 $pre_dbqueries = $DB->perf_get_queries();
86 $pre_time = microtime(1);
87 if ($cron_function()) {
f685e830 88 $DB->set_field("modules", "lastcron", $timenow, array("id"=>$mod->id));
28bd3d9a
PS
89 }
90 if (isset($pre_dbqueries)) {
91 mtrace("... used " . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries");
92 mtrace("... used " . (microtime(1) - $pre_time) . " seconds");
93 }
94 /// Reset possible changes by modules to time_limit. MDL-11597
95 @set_time_limit(0);
96 mtrace("done.");
97 }
98 }
99 }
100 }
101 get_mailer('close');
102 mtrace("Finished activity modules");
103
104 mtrace("Starting blocks");
105 if ($blocks = $DB->get_records_select("block", "cron > 0 AND ((? - lastcron) > cron) AND visible = 1", array($timenow))) {
106 // we will need the base class.
107 require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
108 foreach ($blocks as $block) {
109 $blockfile = $CFG->dirroot.'/blocks/'.$block->name.'/block_'.$block->name.'.php';
110 if (file_exists($blockfile)) {
111 require_once($blockfile);
112 $classname = 'block_'.$block->name;
113 $blockobj = new $classname;
114 if (method_exists($blockobj,'cron')) {
115 mtrace("Processing cron function for ".$block->name.'....','');
116 if ($blockobj->cron()) {
f685e830 117 $DB->set_field('block', 'lastcron', $timenow, array('id'=>$block->id));
28bd3d9a
PS
118 }
119 /// Reset possible changes by blocks to time_limit. MDL-11597
120 @set_time_limit(0);
121 mtrace('done.');
122 }
123 }
124
125 }
126 }
127 mtrace('Finished blocks');
128
bce59524
DM
129 //now do plagiarism checks
130 require_once($CFG->libdir.'/plagiarismlib.php');
131 plagiarism_cron();
132
28bd3d9a
PS
133 mtrace("Starting quiz reports");
134 if ($reports = $DB->get_records_select('quiz_report', "cron > 0 AND ((? - lastcron) > cron)", array($timenow))) {
135 foreach ($reports as $report) {
136 $cronfile = "$CFG->dirroot/mod/quiz/report/$report->name/cron.php";
137 if (file_exists($cronfile)) {
138 include_once($cronfile);
139 $cron_function = 'quiz_report_'.$report->name."_cron";
140 if (function_exists($cron_function)) {
141 mtrace("Processing quiz report cron function $cron_function ...", '');
142 $pre_dbqueries = null;
143 $pre_dbqueries = $DB->perf_get_queries();
144 $pre_time = microtime(1);
145 if ($cron_function()) {
f685e830 146 $DB->set_field('quiz_report', "lastcron", $timenow, array("id"=>$report->id));
28bd3d9a
PS
147 }
148 if (isset($pre_dbqueries)) {
149 mtrace("... used " . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries");
150 mtrace("... used " . (microtime(1) - $pre_time) . " seconds");
151 }
152 mtrace("done.");
153 }
154 }
155 }
156 }
157 mtrace("Finished quiz reports");
158
b5541735
PS
159 mtrace('Starting admin reports');
160 cron_execute_plugin_type('report');
161 mtrace('Finished admin reports');
162
28bd3d9a
PS
163 mtrace('Starting main gradebook job ...');
164 grade_cron();
165 mtrace('done.');
166
167
168 mtrace('Starting processing the event queue...');
169 events_cron();
170 mtrace('done.');
171
172
173 if ($CFG->enablecompletion) {
174 // Completion cron
175 mtrace('Starting the completion cron...');
176 require_once($CFG->libdir . '/completion/cron.php');
177 completion_cron();
178 mtrace('done');
179 }
180
181
182 if ($CFG->enableportfolios) {
183 // Portfolio cron
184 mtrace('Starting the portfolio cron...');
185 require_once($CFG->libdir . '/portfoliolib.php');
186 portfolio_cron();
187 mtrace('done');
188 }
189
190/// Run all core cron jobs, but not every time since they aren't too important.
191/// These don't have a timer to reduce load, so we'll use a random number
192/// to randomly choose the percentage of times we should run these jobs.
193
194 srand ((double) microtime() * 10000000);
195 $random100 = rand(0,100);
196
197 if ($random100 < 20) { // Approximately 20% of the time.
198 mtrace("Running clean-up tasks...");
199
200 /// Delete users who haven't confirmed within required period
201
202 if (!empty($CFG->deleteunconfirmed)) {
203 $cuttime = $timenow - ($CFG->deleteunconfirmed * 3600);
204 $rs = $DB->get_recordset_sql ("SELECT id, firstname, lastname
205 FROM {user}
206 WHERE confirmed = 0 AND firstaccess > 0
207 AND firstaccess < ?", array($cuttime));
208 foreach ($rs as $user) {
209 if ($DB->delete_records('user', array('id'=>$user->id))) {
210 mtrace("Deleted unconfirmed user for ".fullname($user, true)." ($user->id)");
211 }
212 }
213 $rs->close();
214 }
215 flush();
216
217
218 /// Delete users who haven't completed profile within required period
219
220 if (!empty($CFG->deleteincompleteusers)) {
221 $cuttime = $timenow - ($CFG->deleteincompleteusers * 3600);
222 $rs = $DB->get_recordset_sql ("SELECT id, username
223 FROM {user}
224 WHERE confirmed = 1 AND lastaccess > 0
225 AND lastaccess < ? AND deleted = 0
226 AND (lastname = '' OR firstname = '' OR email = '')",
227 array($cuttime));
228 foreach ($rs as $user) {
229 if (delete_user($user)) {
230 mtrace("Deleted not fully setup user $user->username ($user->id)");
231 }
232 }
233 $rs->close();
234 }
235 flush();
236
237
238 /// Delete old logs to save space (this might need a timer to slow it down...)
239
240 if (!empty($CFG->loglifetime)) { // value in days
241 $loglifetime = $timenow - ($CFG->loglifetime * 3600 * 24);
242 if ($DB->delete_records_select("log", "time < ?", array($loglifetime))) {
243 mtrace("Deleted old log records");
244 }
245 }
246 flush();
247
cbb9e9b8
EL
248 // Delete old backup_controllers and logs
249
250 if (!empty($CFG->loglifetime)) { // value in days
251 $loglifetime = $timenow - ($CFG->loglifetime * 3600 * 24);
252 // Delete child records from backup_logs
253 $DB->execute("DELETE FROM {backup_logs}
254 WHERE EXISTS (
255 SELECT 'x'
256 FROM {backup_controllers} bc
257 WHERE bc.backupid = {backup_logs}.backupid
258 AND bc.timecreated < ?)", array($loglifetime));
259 // Delete records from backup_controllers
260 $DB->execute("DELETE FROM {backup_controllers}
261 WHERE timecreated < ?", array($loglifetime));
262 mtrace("Deleted old backup records");
263 }
264 flush();
265
266
28bd3d9a
PS
267
268 /// Delete old cached texts
269
270 if (!empty($CFG->cachetext)) { // Defined in config.php
271 $cachelifetime = time() - $CFG->cachetext - 60; // Add an extra minute to allow for really heavy sites
272 if ($DB->delete_records_select('cache_text', "timemodified < ?", array($cachelifetime))) {
273 mtrace("Deleted old cache_text records");
274 }
275 }
276 flush();
277
278 if (!empty($CFG->notifyloginfailures)) {
279 notify_login_failures();
280 mtrace('Notified login failured');
281 }
282 flush();
283
284 //
285 // generate new password emails for users
286 //
287 mtrace('checking for create_password');
288 if ($DB->count_records('user_preferences', array('name'=>'create_password', 'value'=>'1'))) {
289 mtrace('creating passwords for new users');
290 $newusers = $DB->get_records_sql("SELECT u.id as id, u.email, u.firstname,
291 u.lastname, u.username,
292 p.id as prefid
293 FROM {user} u
294 JOIN {user_preferences} p ON u.id=p.userid
295 WHERE p.name='create_password' AND p.value='1' AND u.email !='' ");
296
297 foreach ($newusers as $newuserid => $newuser) {
28bd3d9a
PS
298 // email user
299 if (setnew_password_and_mail($newuser)) {
300 // remove user pref
301 $DB->delete_records('user_preferences', array('id'=>$newuser->prefid));
302 } else {
303 trigger_error("Could not create and mail new user password!");
304 }
305 }
306 }
307
308 if (!empty($CFG->usetags)) {
309 require_once($CFG->dirroot.'/tag/lib.php');
310 tag_cron();
311 mtrace ('Executed tag cron');
312 }
313
314 // Accesslib stuff
315 cleanup_contexts();
316 mtrace ('Cleaned up contexts');
317 gc_cache_flags();
318 mtrace ('Cleaned cache flags');
319 // If you suspect that the context paths are somehow corrupt
320 // replace the line below with: build_context_path(true);
321 build_context_path();
322 mtrace ('Built context paths');
323
212badff 324 if (!empty($CFG->messagingdeletereadnotificationsdelay)) {
7868480a 325 $notificationdeletetime = time() - $CFG->messagingdeletereadnotificationsdelay;
212badff
AD
326 $DB->delete_records_select('message_read', 'notification=1 AND timeread<:notificationdeletetime', array('notificationdeletetime'=>$notificationdeletetime));
327 mtrace('Cleaned up read notifications');
328 }
329
28bd3d9a
PS
330 mtrace("Finished clean-up tasks...");
331
332 } // End of occasional clean-up tasks
333
bac233d3
SH
334 // Run automated backups if required.
335 require_once($CFG->dirroot.'/backup/util/includes/backup_includes.php');
336 require_once($CFG->dirroot.'/backup/util/helper/backup_cron_helper.class.php');
337 backup_cron_automated_helper::run_automated_backup();
28bd3d9a
PS
338
339/// Run the auth cron, if any
340/// before enrolments because it might add users that will be needed in enrol plugins
341 $auths = get_enabled_auth_plugins();
342
343 mtrace("Running auth crons if required...");
344 foreach ($auths as $auth) {
345 $authplugin = get_auth_plugin($auth);
346 if (method_exists($authplugin, 'cron')) {
347 mtrace("Running cron for auth/$auth...");
348 $authplugin->cron();
349 if (!empty($authplugin->log)) {
350 mtrace($authplugin->log);
351 }
352 }
353 unset($authplugin);
354 }
355
356 mtrace("Running enrol crons if required...");
357 $enrols = enrol_get_plugins(true);
358 foreach($enrols as $ename=>$enrol) {
359 // do this for all plugins, disabled plugins might want to cleanup stuff such as roles
360 if (!$enrol->is_cron_required()) {
361 continue;
362 }
363 mtrace("Running cron for enrol_$ename...");
364 $enrol->cron();
365 $enrol->set_config('lastcron', time());
366 }
367
368 if (!empty($CFG->enablestats) and empty($CFG->disablestatsprocessing)) {
369 require_once($CFG->dirroot.'/lib/statslib.php');
370 // check we're not before our runtime
371 $timetocheck = stats_get_base_daily() + $CFG->statsruntimestarthour*60*60 + $CFG->statsruntimestartminute*60;
372
373 if (time() > $timetocheck) {
374 // process configured number of days as max (defaulting to 31)
375 $maxdays = empty($CFG->statsruntimedays) ? 31 : abs($CFG->statsruntimedays);
376 if (stats_cron_daily($maxdays)) {
377 if (stats_cron_weekly()) {
378 if (stats_cron_monthly()) {
379 stats_clean_old();
380 }
381 }
382 }
383 @set_time_limit(0);
384 } else {
385 mtrace('Next stats run after:'. userdate($timetocheck));
386 }
387 }
388
28bd3d9a 389
b5541735
PS
390 mtrace('Starting course reports');
391 cron_execute_plugin_type('coursereport');
392 mtrace('Finished course reports');
28bd3d9a 393
b5541735
PS
394 // run gradebook import/export/report cron
395 mtrace('Starting gradebook plugins');
396 cron_execute_plugin_type('gradeimport');
397 cron_execute_plugin_type('gradeexport');
398 cron_execute_plugin_type('gradereport');
399 mtrace('Finished gradebook plugins');
28bd3d9a
PS
400
401 // Run external blog cron if needed
402 if ($CFG->useexternalblogs) {
403 require_once($CFG->dirroot . '/blog/lib.php');
404 mtrace("Fetching external blog entries...", '');
405 $sql = "timefetched < ? OR timefetched = 0";
406 $externalblogs = $DB->get_records_select('blog_external', $sql, array(mktime() - $CFG->externalblogcrontime));
407
408 foreach ($externalblogs as $eb) {
409 blog_sync_external_entries($eb);
410 }
b5541735 411 mtrace('done.');
28bd3d9a
PS
412 }
413
414 // Run blog associations cleanup
415 if ($CFG->useblogassociations) {
416 require_once($CFG->dirroot . '/blog/lib.php');
417 // delete entries whose contextids no longer exists
418 mtrace("Deleting blog associations linked to non-existent contexts...", '');
419 $DB->delete_records_select('blog_association', 'contextid NOT IN (SELECT id FROM {context})');
b5541735 420 mtrace('done.');
28bd3d9a
PS
421 }
422
423 //Run registration updated cron
424 mtrace(get_string('siteupdatesstart', 'hub'));
425 require_once($CFG->dirroot . '/admin/registration/lib.php');
426 $registrationmanager = new registration_manager();
427 $registrationmanager->cron();
428 mtrace(get_string('siteupdatesend', 'hub'));
429
430 // cleanup file trash
431 $fs = get_file_storage();
432 $fs->cron();
433
434 //cleanup old session linked tokens
435 //deletes the session linked tokens that are over a day old.
436 mtrace("Deleting session linked tokens more than one day old...", '');
437 $DB->delete_records_select('external_tokens', 'lastaccess < :onedayago AND tokentype = :tokentype',
438 array('onedayago' => time() - DAYSECS, 'tokentype' => EXTERNAL_TOKEN_EMBEDDED));
439 mtrace('done.');
440
b5541735
PS
441 // all other plugins
442 cron_execute_plugin_type('message', 'message plugins');
443 cron_execute_plugin_type('filter', 'filters');
444 cron_execute_plugin_type('editor', 'editor');
445 cron_execute_plugin_type('format', 'course formats');
446 cron_execute_plugin_type('profilefield', 'profile fields');
447 cron_execute_plugin_type('webservice', 'webservices');
448 //cron_execute_plugin_type('repository', 'repositories'); TODO: repository lib.php files are messed up, sorry
449 cron_execute_plugin_type('qtype', 'question types');
450 cron_execute_plugin_type('plagiarism', 'plagiarism plugins');
451 cron_execute_plugin_type('theme', 'themes');
452
453 // and finally run any local cronjobs, if any
28bd3d9a
PS
454 if ($locals = get_plugin_list('local')) {
455 mtrace('Processing customized cron scripts ...', '');
b5541735
PS
456 // new cron functions in lib.php first
457 cron_execute_plugin_type('local');
458 // legacy cron files are executed directly
28bd3d9a
PS
459 foreach ($locals as $local => $localdir) {
460 if (file_exists("$localdir/cron.php")) {
461 include("$localdir/cron.php");
462 }
463 }
464 mtrace('done.');
465 }
466
54638024 467 // NOTE: Other types of plugin could also be supported in this loop, which
468 // can handle generic cron function.
469 foreach (array('report', 'coursereport') as $plugintype) {
470 mtrace('Starting plugin type: ' . $plugintype);
471 // These reports do not have a database table that lists them. Instead
472 // a report has cron.php with function report_reportname_cron() if it
473 // wishes to be cronned. It is up to this function to handle e.g. if it
474 // only needs to actually do anything occasionally.
475 $reports = get_plugin_list_with_function(
476 $plugintype, 'cron', 'cron.php');
477 foreach($reports as $report => $cronfunction) {
478 mtrace('Processing cron function for ' . $report . '...', '');
479 $pre_dbqueries = null;
480 $pre_dbqueries = $DB->perf_get_queries();
481 $pre_time = microtime(true);
482 $cronfunction();
483 if (isset($pre_dbqueries)) {
484 mtrace("... used " .
485 ($DB->perf_get_queries() - $pre_dbqueries) .
486 " dbqueries");
487 mtrace("... used " . round(microtime(true) - $pre_time, 2) .
488 " seconds");
489 }
490 mtrace('done.');
491 }
492 mtrace('Finished plugin type: ' . $plugintype);
493 }
28bd3d9a
PS
494
495 mtrace("Cron script completed correctly");
496
497 $difftime = microtime_diff($starttime, microtime());
498 mtrace("Execution took ".$difftime." seconds");
499
cbb9e9b8 500}
b5541735
PS
501
502function cron_execute_plugin_type($plugintype, $description = null) {
503 global $DB;
504
505 $allplugins = get_plugin_list($plugintype);
506 $plugins = get_plugin_list_with_function($plugintype, 'cron', 'lib.php');
507
508 // add BC compatible file locations and old cron function names
509 $plugins = cron_plugins_functions_bc_hacks($plugintype, $plugins);
510
511 if (!$plugins) {
512 return;
513 }
514
515 if ($description) {
516 mtrace('Starting '.$description);
517 }
518
519 foreach ($plugins as $component=>$cronfunction) {
520 $dir = $allplugins[$component];
521 $cronperiod = 0;
522 if (file_exists("$dir/version.php")) {
523 $plugin = new stdClass();
524 include("$dir/version.php");
525 if (isset($plugin->cron)) {
526 $cronperiod = $plugin->cron;
527 }
528 }
529
530 $lastcron = get_config($component, 'lastcron');
531 if ($cronperiod and $lastcron) {
532 if ($lastcron + $cronperiod > time()) {
533 // do not execute cron yet
534 continue;
535 }
536 }
537
538 mtrace('Processing cron function for '.$component.'...');
539 $pre_dbqueries = $DB->perf_get_queries();
540 $pre_time = microtime(true);
541
542 $cronfunction();
543
544 mtrace("... used " . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries");
545 mtrace("... used " . round(microtime(true) - $pre_time, 2) . " seconds");
546 mtrace('done.');
547
548 set_config('lastcron', time(), $component);
549 }
550
551 if ($description) {
552 mtrace('Finished '.$description);
553 }
554}
555
556function cron_plugins_functions_bc_hacks($plugintype, $plugins) {
557 global $CFG; // mandatory because the lib functions do not define it global!
558
559 if ($plugintype === 'report' or $plugintype === 'coursereport') {
560 // admin and course reports
561 foreach(get_plugin_list($plugintype) as $pluginname=>$dir) {
562 $component = $plugintype.'_'.$pluginname;
563 if (isset($plugins[$component])) {
564 // we already have the new type
565 continue;
566 }
567 if (!file_exists("$dir/cron.php")) {
568 //no old style corn file present
569 continue;
570 }
571 include_once("$dir/cron.php");
572 $cronfunction = $component.'_cron';
573 if (function_exists($cronfunction)) {
574 $plugins[$component] = $cronfunction;
575 } else {
576 debugging("Invalid legacy cron.php detected in $component, please use lib.php instead");
577 }
578 }
579
580 } else if (strpos($plugintype, 'grade') === 0) {
581 // detect old style cron functio names
582 foreach(get_plugin_list($plugintype) as $pluginname=>$dir) {
583 $component = $plugintype.'_'.$pluginname;
584 if (isset($plugins[$component])) {
585 // we already have the new type
586 continue;
587 }
588 if (!file_exists("$dir/lib.php")) {
589 continue;
590 }
591 include_once("$dir/lib.php");
592 $cronfunction = str_replace('grade', 'grade_', $plugintype).'_'.$pluginname.'_cron';
593 if (function_exists($cronfunction)) {
594 $plugins[$component] = $cronfunction;
595 }
596 }
597 }
598
599 return $plugins;
600}