3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
23 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 global $DB, $CFG, $OUTPUT;
30 if (CLI_MAINTENANCE
) {
31 echo "CLI maintenance mode active, cron execution suspended.\n";
35 if (moodle_needs_upgrading()) {
36 echo "Moodle upgrade pending, cron execution suspended.\n";
40 require_once($CFG->libdir
.'/adminlib.php');
41 require_once($CFG->libdir
.'/gradelib.php');
43 if (!empty($CFG->showcronsql
)) {
46 if (!empty($CFG->showcrondebugging
)) {
47 $CFG->debug
= DEBUG_DEVELOPER
;
48 $CFG->debugdisplay
= true;
52 $starttime = microtime();
54 /// increase memory limit
55 raise_memory_limit(MEMORY_EXTRA
);
57 /// emulate normal session
64 mtrace("Server Time: ".date('r',$timenow)."\n\n");
69 mtrace("Cleaning up stale sessions");
72 /// Run all cron jobs for each module
74 mtrace("Starting activity modules");
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
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
127 mtrace('Finished blocks');
129 mtrace("Starting quiz reports");
130 if ($reports = $DB->get_records_select('quiz_reports', "cron > 0 AND ((? - lastcron) > cron)", array($timenow))) {
131 foreach ($reports as $report) {
132 $cronfile = "$CFG->dirroot/mod/quiz/report/$report->name/cron.php";
133 if (file_exists($cronfile)) {
134 include_once($cronfile);
135 $cron_function = 'quiz_report_'.$report->name
."_cron";
136 if (function_exists($cron_function)) {
137 mtrace("Processing quiz report cron function $cron_function ...", '');
138 $pre_dbqueries = null;
139 $pre_dbqueries = $DB->perf_get_queries();
140 $pre_time = microtime(1);
141 if ($cron_function()) {
142 $DB->set_field('quiz_reports', "lastcron", $timenow, array("id"=>$report->id
));
144 if (isset($pre_dbqueries)) {
145 mtrace("... used " . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries");
146 mtrace("... used " . (microtime(1) - $pre_time) . " seconds");
153 mtrace("Finished quiz reports");
155 mtrace('Starting admin reports');
156 cron_execute_plugin_type('report');
157 mtrace('Finished admin reports');
159 mtrace('Starting main gradebook job ...');
164 mtrace('Starting processing the event queue...');
169 if ($CFG->enablecompletion
) {
171 mtrace('Starting the completion cron...');
172 require_once($CFG->libdir
. '/completion/cron.php');
178 if ($CFG->enableportfolios
) {
180 mtrace('Starting the portfolio cron...');
181 require_once($CFG->libdir
. '/portfoliolib.php');
186 //now do plagiarism checks
187 require_once($CFG->libdir
.'/plagiarismlib.php');
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
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)");
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 *
224 WHERE confirmed = 1 AND lastaccess > 0
225 AND lastaccess < ? AND deleted = 0
226 AND (lastname = '' OR firstname = '' OR email = '')",
228 foreach ($rs as $user) {
229 if (delete_user($user)) {
230 mtrace("Deleted not fully setup user $user->username ($user->id)");
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");
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}
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");
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");
278 if (!empty($CFG->notifyloginfailures
)) {
279 notify_login_failures();
280 mtrace('Notified login failured');
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,
294 JOIN {user_preferences} p ON u.id=p.userid
295 WHERE p.name='create_password' AND p.value='1' AND u.email !='' AND u.suspended = 0 AND u.auth != 'nologin'");
297 // note: we can not send emails to suspended accounts
298 foreach ($newusers as $newuser) {
300 if (setnew_password_and_mail($newuser)) {
301 unset_user_preference('create_password', $newuser);
302 set_user_preference('auth_forcepasswordchange', 1, $newuser);
304 trigger_error("Could not create and mail new user password!");
310 if (!empty($CFG->usetags
)) {
311 require_once($CFG->dirroot
.'/tag/lib.php');
313 mtrace ('Executed tag cron');
318 mtrace ('Cleaned up contexts');
320 mtrace ('Cleaned cache flags');
321 // If you suspect that the context paths are somehow corrupt
322 // replace the line below with: build_context_path(true);
323 build_context_path();
324 mtrace ('Built context paths');
326 if (!empty($CFG->messagingdeletereadnotificationsdelay
)) {
327 $notificationdeletetime = time() - $CFG->messagingdeletereadnotificationsdelay
;
328 $DB->delete_records_select('message_read', 'notification=1 AND timeread<:notificationdeletetime', array('notificationdeletetime'=>$notificationdeletetime));
329 mtrace('Cleaned up read notifications');
332 mtrace("Finished clean-up tasks...");
334 } // End of occasional clean-up tasks
336 // Run automated backups if required.
337 require_once($CFG->dirroot
.'/backup/util/includes/backup_includes.php');
338 require_once($CFG->dirroot
.'/backup/util/helper/backup_cron_helper.class.php');
339 backup_cron_automated_helper
::run_automated_backup();
341 /// Run the auth cron, if any
342 /// before enrolments because it might add users that will be needed in enrol plugins
343 $auths = get_enabled_auth_plugins();
345 mtrace("Running auth crons if required...");
346 foreach ($auths as $auth) {
347 $authplugin = get_auth_plugin($auth);
348 if (method_exists($authplugin, 'cron')) {
349 mtrace("Running cron for auth/$auth...");
351 if (!empty($authplugin->log
)) {
352 mtrace($authplugin->log
);
358 mtrace("Running enrol crons if required...");
359 $enrols = enrol_get_plugins(true);
360 foreach($enrols as $ename=>$enrol) {
361 // do this for all plugins, disabled plugins might want to cleanup stuff such as roles
362 if (!$enrol->is_cron_required()) {
365 mtrace("Running cron for enrol_$ename...");
367 $enrol->set_config('lastcron', time());
370 if (!empty($CFG->enablestats
) and empty($CFG->disablestatsprocessing
)) {
371 require_once($CFG->dirroot
.'/lib/statslib.php');
372 // check we're not before our runtime
373 $timetocheck = stats_get_base_daily() +
$CFG->statsruntimestarthour
*60*60 +
$CFG->statsruntimestartminute
*60;
375 if (time() > $timetocheck) {
376 // process configured number of days as max (defaulting to 31)
377 $maxdays = empty($CFG->statsruntimedays
) ?
31 : abs($CFG->statsruntimedays
);
378 if (stats_cron_daily($maxdays)) {
379 if (stats_cron_weekly()) {
380 if (stats_cron_monthly()) {
387 mtrace('Next stats run after:'. userdate($timetocheck));
392 mtrace('Starting course reports');
393 cron_execute_plugin_type('coursereport');
394 mtrace('Finished course reports');
396 // run gradebook import/export/report cron
397 mtrace('Starting gradebook plugins');
398 cron_execute_plugin_type('gradeimport');
399 cron_execute_plugin_type('gradeexport');
400 cron_execute_plugin_type('gradereport');
401 mtrace('Finished gradebook plugins');
403 // Run external blog cron if needed
404 if ($CFG->useexternalblogs
) {
405 require_once($CFG->dirroot
. '/blog/lib.php');
406 mtrace("Fetching external blog entries...", '');
407 $sql = "timefetched < ? OR timefetched = 0";
408 $externalblogs = $DB->get_records_select('blog_external', $sql, array(mktime() - $CFG->externalblogcrontime
));
410 foreach ($externalblogs as $eb) {
411 blog_sync_external_entries($eb);
416 // Run blog associations cleanup
417 if ($CFG->useblogassociations
) {
418 require_once($CFG->dirroot
. '/blog/lib.php');
419 // delete entries whose contextids no longer exists
420 mtrace("Deleting blog associations linked to non-existent contexts...", '');
421 $DB->delete_records_select('blog_association', 'contextid NOT IN (SELECT id FROM {context})');
425 //Run registration updated cron
426 mtrace(get_string('siteupdatesstart', 'hub'));
427 require_once($CFG->dirroot
. '/admin/registration/lib.php');
428 $registrationmanager = new registration_manager();
429 $registrationmanager->cron();
430 mtrace(get_string('siteupdatesend', 'hub'));
432 // cleanup file trash
433 $fs = get_file_storage();
436 //cleanup old session linked tokens
437 //deletes the session linked tokens that are over a day old.
438 mtrace("Deleting session linked tokens more than one day old...", '');
439 $DB->delete_records_select('external_tokens', 'lastaccess < :onedayago AND tokentype = :tokentype',
440 array('onedayago' => time() - DAYSECS
, 'tokentype' => EXTERNAL_TOKEN_EMBEDDED
));
444 cron_execute_plugin_type('message', 'message plugins');
445 cron_execute_plugin_type('filter', 'filters');
446 cron_execute_plugin_type('editor', 'editors');
447 cron_execute_plugin_type('format', 'course formats');
448 cron_execute_plugin_type('profilefield', 'profile fields');
449 cron_execute_plugin_type('webservice', 'webservices');
450 // TODO: Repository lib.php files are messed up (include many other files, etc), so it is
451 // currently not possible to implement repository plugin cron using this infrastructure
452 // cron_execute_plugin_type('repository', 'repository plugins');
453 cron_execute_plugin_type('qtype', 'question types');
454 cron_execute_plugin_type('plagiarism', 'plagiarism plugins');
455 cron_execute_plugin_type('theme', 'themes');
457 // and finally run any local cronjobs, if any
458 if ($locals = get_plugin_list('local')) {
459 mtrace('Processing customized cron scripts ...', '');
460 // new cron functions in lib.php first
461 cron_execute_plugin_type('local');
462 // legacy cron files are executed directly
463 foreach ($locals as $local => $localdir) {
464 if (file_exists("$localdir/cron.php")) {
465 include("$localdir/cron.php");
471 mtrace("Cron script completed correctly");
473 $difftime = microtime_diff($starttime, microtime());
474 mtrace("Execution took ".$difftime." seconds");
478 * Executes cron functions for a specific type of plugin.
479 * @param string $plugintype Plugin type (e.g. 'report')
480 * @param string $description If specified, will display 'Starting (whatever)'
481 * and 'Finished (whatever)' lines, otherwise does not display
483 function cron_execute_plugin_type($plugintype, $description = null) {
486 // Get list from plugin => function for all plugins
487 $plugins = get_plugin_list_with_function($plugintype, 'cron');
489 // Modify list for backward compatibility (different files/names)
490 $plugins = cron_bc_hack_plugin_functions($plugintype, $plugins);
492 // Return if no plugins with cron function to process
498 mtrace('Starting '.$description);
501 foreach ($plugins as $component=>$cronfunction) {
502 $dir = get_component_directory($component);
504 // Get cron period if specified in version.php, otherwise assume every cron
506 if (file_exists("$dir/version.php")) {
507 $plugin = new stdClass();
508 include("$dir/version.php");
509 if (isset($plugin->cron
)) {
510 $cronperiod = $plugin->cron
;
514 // Using last cron and cron period, don't run if it already ran recently
515 $lastcron = get_config($component, 'lastcron');
516 if ($cronperiod && $lastcron) {
517 if ($lastcron +
$cronperiod > time()) {
518 // do not execute cron yet
523 mtrace('Processing cron function for ' . $component . '...');
524 $pre_dbqueries = $DB->perf_get_queries();
525 $pre_time = microtime(true);
529 mtrace("done. (" . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries, " .
530 round(microtime(true) - $pre_time, 2) . " seconds)");
532 set_config('lastcron', time(), $component);
536 mtrace('Finished ' . $description);
541 * Used to add in old-style cron functions within plugins that have not been converted to the
542 * new standard API. (The standard API is frankenstyle_name_cron() in lib.php; some types used
543 * cron.php and some used a different name.)
544 * @param string $plugintype Plugin type e.g. 'report'
545 * @param array $plugins Array from plugin name (e.g. 'report_frog') to function name (e.g.
546 * 'report_frog_cron') for plugin cron functions that were already found using the new API
547 * @return array Revised version of $plugins that adds in any extra plugin functions found by
548 * looking in the older location
550 function cron_bc_hack_plugin_functions($plugintype, $plugins) {
551 global $CFG; // mandatory in case it is referenced by include()d PHP script
553 if ($plugintype === 'report') {
554 // Admin reports only - not course report because course report was
555 // never implemented before, so doesn't need BC
556 foreach (get_plugin_list($plugintype) as $pluginname=>$dir) {
557 $component = $plugintype . '_' . $pluginname;
558 if (isset($plugins[$component])) {
559 // We already have detected the function using the new API
562 if (!file_exists("$dir/cron.php")) {
563 // No old style cron file present
566 include_once("$dir/cron.php");
567 $cronfunction = $component . '_cron';
568 if (function_exists($cronfunction)) {
569 $plugins[$component] = $cronfunction;
571 debugging("Invalid legacy cron.php detected in $component, " .
572 "please use lib.php instead");
575 } else if (strpos($plugintype, 'grade') === 0) {
576 // Detect old style cron function names
577 // Plugin gradeexport_frog used to use grade_export_frog_cron() instead of
578 // new standard API gradeexport_frog_cron(). Also applies to gradeimport, gradereport
579 foreach(get_plugin_list($plugintype) as $pluginname=>$dir) {
580 $component = $plugintype.'_'.$pluginname;
581 if (isset($plugins[$component])) {
582 // We already have detected the function using the new API
585 if (!file_exists("$dir/lib.php")) {
588 include_once("$dir/lib.php");
589 $cronfunction = str_replace('grade', 'grade_', $plugintype) . '_' .
590 $pluginname . '_cron';
591 if (function_exists($cronfunction)) {
592 $plugins[$component] = $cronfunction;