Merge branch 'MDL-29276' of git://github.com/mouneyrac/moodle
[moodle.git] / lib / cronlib.php
blobdc944a4228d7c0752559991a20a280faa56d268d
1 <?php
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.
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/>.
18 /**
19 * Cron functions.
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
27 function cron_run() {
28 global $DB, $CFG, $OUTPUT;
30 if (CLI_MAINTENANCE) {
31 echo "CLI maintenance mode active, cron execution suspended.\n";
32 exit(1);
35 if (moodle_needs_upgrading()) {
36 echo "Moodle upgrade pending, cron execution suspended.\n";
37 exit(1);
40 require_once($CFG->libdir.'/adminlib.php');
41 require_once($CFG->libdir.'/gradelib.php');
43 if (!empty($CFG->showcronsql)) {
44 $DB->set_debug(true);
46 if (!empty($CFG->showcrondebugging)) {
47 $CFG->debug = DEBUG_DEVELOPER;
48 $CFG->debugdisplay = true;
51 set_time_limit(0);
52 $starttime = microtime();
54 /// increase memory limit
55 raise_memory_limit(MEMORY_EXTRA);
57 /// emulate normal session
58 cron_setup_user();
60 /// Start output log
62 $timenow = time();
64 mtrace("Server Time: ".date('r',$timenow)."\n\n");
67 /// Session gc
69 mtrace("Cleaning up stale sessions");
70 session_gc();
72 /// Run all cron jobs for each module
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()) {
88 $DB->set_field("modules", "lastcron", $timenow, array("id"=>$mod->id));
90 if (isset($pre_dbqueries)) {
91 mtrace("... used " . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries");
92 mtrace("... used " . (microtime(1) - $pre_time) . " seconds");
94 /// Reset possible changes by modules to time_limit. MDL-11597
95 @set_time_limit(0);
96 mtrace("done.");
101 get_mailer('close');
102 mtrace("Finished activity modules");
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()) {
117 $DB->set_field('block', 'lastcron', $timenow, array('id'=>$block->id));
119 /// Reset possible changes by blocks to time_limit. MDL-11597
120 @set_time_limit(0);
121 mtrace('done.');
127 mtrace('Finished blocks');
129 //now do plagiarism checks
130 require_once($CFG->libdir.'/plagiarismlib.php');
131 plagiarism_cron();
133 mtrace("Starting quiz reports");
134 if ($reports = $DB->get_records_select('quiz_reports', "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()) {
146 $DB->set_field('quiz_reports', "lastcron", $timenow, array("id"=>$report->id));
148 if (isset($pre_dbqueries)) {
149 mtrace("... used " . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries");
150 mtrace("... used " . (microtime(1) - $pre_time) . " seconds");
152 mtrace("done.");
157 mtrace("Finished quiz reports");
159 mtrace('Starting admin reports');
160 cron_execute_plugin_type('report');
161 mtrace('Finished admin reports');
163 mtrace('Starting main gradebook job ...');
164 grade_cron();
165 mtrace('done.');
168 mtrace('Starting processing the event queue...');
169 events_cron();
170 mtrace('done.');
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');
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');
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.
194 srand ((double) microtime() * 10000000);
195 $random100 = rand(0,100);
197 if ($random100 < 20) { // Approximately 20% of the time.
198 mtrace("Running clean-up tasks...");
200 /// Delete users who haven't confirmed within required period
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)");
213 $rs->close();
215 flush();
218 /// Delete users who haven't completed profile within required period
220 if (!empty($CFG->deleteincompleteusers)) {
221 $cuttime = $timenow - ($CFG->deleteincompleteusers * 3600);
222 $rs = $DB->get_recordset_sql ("SELECT *
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)");
233 $rs->close();
235 flush();
238 /// Delete old logs to save space (this might need a timer to slow it down...)
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");
246 flush();
248 // Delete old backup_controllers and logs
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");
264 flush();
268 /// Delete old cached texts
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");
276 flush();
278 if (!empty($CFG->notifyloginfailures)) {
279 notify_login_failures();
280 mtrace('Notified login failured');
282 flush();
285 // generate new password emails for users
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_recordset_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 !='' ");
297 foreach ($newusers as $newuser) {
298 // email user
299 if (setnew_password_and_mail($newuser)) {
300 unset_user_preference('create_password', $newuser);
301 set_user_preference('auth_forcepasswordchange', 1, $newuser);
302 } else {
303 trigger_error("Could not create and mail new user password!");
306 $newusers->close();
309 if (!empty($CFG->usetags)) {
310 require_once($CFG->dirroot.'/tag/lib.php');
311 tag_cron();
312 mtrace ('Executed tag cron');
315 // Accesslib stuff
316 cleanup_contexts();
317 mtrace ('Cleaned up contexts');
318 gc_cache_flags();
319 mtrace ('Cleaned cache flags');
320 // If you suspect that the context paths are somehow corrupt
321 // replace the line below with: build_context_path(true);
322 build_context_path();
323 mtrace ('Built context paths');
325 if (!empty($CFG->messagingdeletereadnotificationsdelay)) {
326 $notificationdeletetime = time() - $CFG->messagingdeletereadnotificationsdelay;
327 $DB->delete_records_select('message_read', 'notification=1 AND timeread<:notificationdeletetime', array('notificationdeletetime'=>$notificationdeletetime));
328 mtrace('Cleaned up read notifications');
331 mtrace("Finished clean-up tasks...");
333 } // End of occasional clean-up tasks
335 // Run automated backups if required.
336 require_once($CFG->dirroot.'/backup/util/includes/backup_includes.php');
337 require_once($CFG->dirroot.'/backup/util/helper/backup_cron_helper.class.php');
338 backup_cron_automated_helper::run_automated_backup();
340 /// Run the auth cron, if any
341 /// before enrolments because it might add users that will be needed in enrol plugins
342 $auths = get_enabled_auth_plugins();
344 mtrace("Running auth crons if required...");
345 foreach ($auths as $auth) {
346 $authplugin = get_auth_plugin($auth);
347 if (method_exists($authplugin, 'cron')) {
348 mtrace("Running cron for auth/$auth...");
349 $authplugin->cron();
350 if (!empty($authplugin->log)) {
351 mtrace($authplugin->log);
354 unset($authplugin);
357 mtrace("Running enrol crons if required...");
358 $enrols = enrol_get_plugins(true);
359 foreach($enrols as $ename=>$enrol) {
360 // do this for all plugins, disabled plugins might want to cleanup stuff such as roles
361 if (!$enrol->is_cron_required()) {
362 continue;
364 mtrace("Running cron for enrol_$ename...");
365 $enrol->cron();
366 $enrol->set_config('lastcron', time());
369 if (!empty($CFG->enablestats) and empty($CFG->disablestatsprocessing)) {
370 require_once($CFG->dirroot.'/lib/statslib.php');
371 // check we're not before our runtime
372 $timetocheck = stats_get_base_daily() + $CFG->statsruntimestarthour*60*60 + $CFG->statsruntimestartminute*60;
374 if (time() > $timetocheck) {
375 // process configured number of days as max (defaulting to 31)
376 $maxdays = empty($CFG->statsruntimedays) ? 31 : abs($CFG->statsruntimedays);
377 if (stats_cron_daily($maxdays)) {
378 if (stats_cron_weekly()) {
379 if (stats_cron_monthly()) {
380 stats_clean_old();
384 @set_time_limit(0);
385 } else {
386 mtrace('Next stats run after:'. userdate($timetocheck));
391 mtrace('Starting course reports');
392 cron_execute_plugin_type('coursereport');
393 mtrace('Finished course reports');
395 // run gradebook import/export/report cron
396 mtrace('Starting gradebook plugins');
397 cron_execute_plugin_type('gradeimport');
398 cron_execute_plugin_type('gradeexport');
399 cron_execute_plugin_type('gradereport');
400 mtrace('Finished gradebook plugins');
402 // Run external blog cron if needed
403 if ($CFG->useexternalblogs) {
404 require_once($CFG->dirroot . '/blog/lib.php');
405 mtrace("Fetching external blog entries...", '');
406 $sql = "timefetched < ? OR timefetched = 0";
407 $externalblogs = $DB->get_records_select('blog_external', $sql, array(mktime() - $CFG->externalblogcrontime));
409 foreach ($externalblogs as $eb) {
410 blog_sync_external_entries($eb);
412 mtrace('done.');
415 // Run blog associations cleanup
416 if ($CFG->useblogassociations) {
417 require_once($CFG->dirroot . '/blog/lib.php');
418 // delete entries whose contextids no longer exists
419 mtrace("Deleting blog associations linked to non-existent contexts...", '');
420 $DB->delete_records_select('blog_association', 'contextid NOT IN (SELECT id FROM {context})');
421 mtrace('done.');
424 //Run registration updated cron
425 mtrace(get_string('siteupdatesstart', 'hub'));
426 require_once($CFG->dirroot . '/admin/registration/lib.php');
427 $registrationmanager = new registration_manager();
428 $registrationmanager->cron();
429 mtrace(get_string('siteupdatesend', 'hub'));
431 // cleanup file trash
432 $fs = get_file_storage();
433 $fs->cron();
435 //cleanup old session linked tokens
436 //deletes the session linked tokens that are over a day old.
437 mtrace("Deleting session linked tokens more than one day old...", '');
438 $DB->delete_records_select('external_tokens', 'lastaccess < :onedayago AND tokentype = :tokentype',
439 array('onedayago' => time() - DAYSECS, 'tokentype' => EXTERNAL_TOKEN_EMBEDDED));
440 mtrace('done.');
442 // all other plugins
443 cron_execute_plugin_type('message', 'message plugins');
444 cron_execute_plugin_type('filter', 'filters');
445 cron_execute_plugin_type('editor', 'editors');
446 cron_execute_plugin_type('format', 'course formats');
447 cron_execute_plugin_type('profilefield', 'profile fields');
448 cron_execute_plugin_type('webservice', 'webservices');
449 // TODO: Repository lib.php files are messed up (include many other files, etc), so it is
450 // currently not possible to implement repository plugin cron using this infrastructure
451 // cron_execute_plugin_type('repository', 'repository plugins');
452 cron_execute_plugin_type('qtype', 'question types');
453 cron_execute_plugin_type('plagiarism', 'plagiarism plugins');
454 cron_execute_plugin_type('theme', 'themes');
456 // and finally run any local cronjobs, if any
457 if ($locals = get_plugin_list('local')) {
458 mtrace('Processing customized cron scripts ...', '');
459 // new cron functions in lib.php first
460 cron_execute_plugin_type('local');
461 // legacy cron files are executed directly
462 foreach ($locals as $local => $localdir) {
463 if (file_exists("$localdir/cron.php")) {
464 include("$localdir/cron.php");
467 mtrace('done.');
470 mtrace("Cron script completed correctly");
472 $difftime = microtime_diff($starttime, microtime());
473 mtrace("Execution took ".$difftime." seconds");
477 * Executes cron functions for a specific type of plugin.
478 * @param string $plugintype Plugin type (e.g. 'report')
479 * @param string $description If specified, will display 'Starting (whatever)'
480 * and 'Finished (whatever)' lines, otherwise does not display
482 function cron_execute_plugin_type($plugintype, $description = null) {
483 global $DB;
485 // Get list from plugin => function for all plugins
486 $plugins = get_plugin_list_with_function($plugintype, 'cron');
488 // Modify list for backward compatibility (different files/names)
489 $plugins = cron_bc_hack_plugin_functions($plugintype, $plugins);
491 // Return if no plugins with cron function to process
492 if (!$plugins) {
493 return;
496 if ($description) {
497 mtrace('Starting '.$description);
500 foreach ($plugins as $component=>$cronfunction) {
501 $dir = get_component_directory($component);
503 // Get cron period if specified in version.php, otherwise assume every cron
504 $cronperiod = 0;
505 if (file_exists("$dir/version.php")) {
506 $plugin = new stdClass();
507 include("$dir/version.php");
508 if (isset($plugin->cron)) {
509 $cronperiod = $plugin->cron;
513 // Using last cron and cron period, don't run if it already ran recently
514 $lastcron = get_config($component, 'lastcron');
515 if ($cronperiod && $lastcron) {
516 if ($lastcron + $cronperiod > time()) {
517 // do not execute cron yet
518 continue;
522 mtrace('Processing cron function for ' . $component . '...');
523 $pre_dbqueries = $DB->perf_get_queries();
524 $pre_time = microtime(true);
526 $cronfunction();
528 mtrace("done. (" . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries, " .
529 round(microtime(true) - $pre_time, 2) . " seconds)");
531 set_config('lastcron', time(), $component);
534 if ($description) {
535 mtrace('Finished ' . $description);
540 * Used to add in old-style cron functions within plugins that have not been converted to the
541 * new standard API. (The standard API is frankenstyle_name_cron() in lib.php; some types used
542 * cron.php and some used a different name.)
543 * @param string $plugintype Plugin type e.g. 'report'
544 * @param array $plugins Array from plugin name (e.g. 'report_frog') to function name (e.g.
545 * 'report_frog_cron') for plugin cron functions that were already found using the new API
546 * @return array Revised version of $plugins that adds in any extra plugin functions found by
547 * looking in the older location
549 function cron_bc_hack_plugin_functions($plugintype, $plugins) {
550 global $CFG; // mandatory in case it is referenced by include()d PHP script
552 if ($plugintype === 'report') {
553 // Admin reports only - not course report because course report was
554 // never implemented before, so doesn't need BC
555 foreach (get_plugin_list($plugintype) as $pluginname=>$dir) {
556 $component = $plugintype . '_' . $pluginname;
557 if (isset($plugins[$component])) {
558 // We already have detected the function using the new API
559 continue;
561 if (!file_exists("$dir/cron.php")) {
562 // No old style cron file present
563 continue;
565 include_once("$dir/cron.php");
566 $cronfunction = $component . '_cron';
567 if (function_exists($cronfunction)) {
568 $plugins[$component] = $cronfunction;
569 } else {
570 debugging("Invalid legacy cron.php detected in $component, " .
571 "please use lib.php instead");
574 } else if (strpos($plugintype, 'grade') === 0) {
575 // Detect old style cron function names
576 // Plugin gradeexport_frog used to use grade_export_frog_cron() instead of
577 // new standard API gradeexport_frog_cron(). Also applies to gradeimport, gradereport
578 foreach(get_plugin_list($plugintype) as $pluginname=>$dir) {
579 $component = $plugintype.'_'.$pluginname;
580 if (isset($plugins[$component])) {
581 // We already have detected the function using the new API
582 continue;
584 if (!file_exists("$dir/lib.php")) {
585 continue;
587 include_once("$dir/lib.php");
588 $cronfunction = str_replace('grade', 'grade_', $plugintype) . '_' .
589 $pluginname . '_cron';
590 if (function_exists($cronfunction)) {
591 $plugins[$component] = $cronfunction;
596 return $plugins;