2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
22 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 global $DB, $CFG, $OUTPUT;
32 if (CLI_MAINTENANCE
) {
33 echo "CLI maintenance mode active, cron execution suspended.\n";
37 if (moodle_needs_upgrading()) {
38 echo "Moodle upgrade pending, cron execution suspended.\n";
42 require_once($CFG->libdir
.'/adminlib.php');
44 if (!empty($CFG->showcronsql
)) {
47 if (!empty($CFG->showcrondebugging
)) {
48 set_debugging(DEBUG_DEVELOPER
, true);
51 core_php_time_limit
::raise();
52 $starttime = microtime();
54 // Increase memory limit
55 raise_memory_limit(MEMORY_EXTRA
);
57 // Emulate normal session - we use admin accoutn by default
62 mtrace("Server Time: ".date('r', $timenow)."\n\n");
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)) {
67 mtrace("Execute scheduled task: " . $task->get_name());
68 cron_trace_time_and_memory();
70 $predbqueries = $DB->perf_get_queries();
71 $pretime = microtime(1);
75 if ($DB->is_transaction_started()) {
76 throw new coding_exception("Task left transaction open");
78 if (isset($predbqueries)) {
79 mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
80 mtrace("... used " . (microtime(1) - $pretime) . " seconds");
82 mtrace("Scheduled task complete: " . $task->get_name());
83 \core\task\manager
::scheduled_task_complete($task);
84 } catch (Exception
$e) {
85 if ($DB && $DB->is_transaction_started()) {
86 error_log('Database transaction aborted automatically in ' . get_class($task));
87 $DB->force_transaction_rollback();
89 if (isset($predbqueries)) {
90 mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
91 mtrace("... used " . (microtime(1) - $pretime) . " seconds");
93 mtrace("Scheduled task failed: " . $task->get_name() . "," . $e->getMessage());
94 \core\task\manager
::scheduled_task_failed($task);
100 // Run all adhoc tasks.
101 while (!\core\task\manager
::static_caches_cleared_since($timenow) &&
102 $task = \core\task\manager
::get_next_adhoc_task($timenow)) {
103 mtrace("Execute adhoc task: " . get_class($task));
104 cron_trace_time_and_memory();
105 $predbqueries = null;
106 $predbqueries = $DB->perf_get_queries();
107 $pretime = microtime(1);
109 get_mailer('buffer');
111 if ($DB->is_transaction_started()) {
112 throw new coding_exception("Task left transaction open");
114 if (isset($predbqueries)) {
115 mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
116 mtrace("... used " . (microtime(1) - $pretime) . " seconds");
118 mtrace("Adhoc task complete: " . get_class($task));
119 \core\task\manager
::adhoc_task_complete($task);
120 } catch (Exception
$e) {
121 if ($DB && $DB->is_transaction_started()) {
122 error_log('Database transaction aborted automatically in ' . get_class($task));
123 $DB->force_transaction_rollback();
125 if (isset($predbqueries)) {
126 mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
127 mtrace("... used " . (microtime(1) - $pretime) . " seconds");
129 mtrace("Adhoc task failed: " . get_class($task) . "," . $e->getMessage());
130 \core\task\manager
::adhoc_task_failed($task);
136 mtrace("Cron script completed correctly");
139 mtrace('Cron completed at ' . date('H:i:s') . '. Memory used ' . display_size(memory_get_usage()) . '.');
140 $difftime = microtime_diff($starttime, microtime());
141 mtrace("Execution took ".$difftime." seconds");
145 * Output some standard information during cron runs. Specifically current time
146 * and memory usage. This method also does gc_collect_cycles() (before displaying
147 * memory usage) to try to help PHP manage memory better.
149 function cron_trace_time_and_memory() {
151 mtrace('... started ' . date('H:i:s') . '. Current memory use ' . display_size(memory_get_usage()) . '.');
155 * Executes cron functions for a specific type of plugin.
157 * @param string $plugintype Plugin type (e.g. 'report')
158 * @param string $description If specified, will display 'Starting (whatever)'
159 * and 'Finished (whatever)' lines, otherwise does not display
161 function cron_execute_plugin_type($plugintype, $description = null) {
164 // Get list from plugin => function for all plugins
165 $plugins = get_plugin_list_with_function($plugintype, 'cron');
167 // Modify list for backward compatibility (different files/names)
168 $plugins = cron_bc_hack_plugin_functions($plugintype, $plugins);
170 // Return if no plugins with cron function to process
176 mtrace('Starting '.$description);
179 foreach ($plugins as $component=>$cronfunction) {
180 $dir = core_component
::get_component_directory($component);
182 // Get cron period if specified in version.php, otherwise assume every cron
184 if (file_exists("$dir/version.php")) {
185 $plugin = new stdClass();
186 include("$dir/version.php");
187 if (isset($plugin->cron
)) {
188 $cronperiod = $plugin->cron
;
192 // Using last cron and cron period, don't run if it already ran recently
193 $lastcron = get_config($component, 'lastcron');
194 if ($cronperiod && $lastcron) {
195 if ($lastcron +
$cronperiod > time()) {
196 // do not execute cron yet
201 mtrace('Processing cron function for ' . $component . '...');
202 cron_trace_time_and_memory();
203 $pre_dbqueries = $DB->perf_get_queries();
204 $pre_time = microtime(true);
208 mtrace("done. (" . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries, " .
209 round(microtime(true) - $pre_time, 2) . " seconds)");
211 set_config('lastcron', time(), $component);
212 core_php_time_limit
::raise();
216 mtrace('Finished ' . $description);
221 * Used to add in old-style cron functions within plugins that have not been converted to the
222 * new standard API. (The standard API is frankenstyle_name_cron() in lib.php; some types used
223 * cron.php and some used a different name.)
225 * @param string $plugintype Plugin type e.g. 'report'
226 * @param array $plugins Array from plugin name (e.g. 'report_frog') to function name (e.g.
227 * 'report_frog_cron') for plugin cron functions that were already found using the new API
228 * @return array Revised version of $plugins that adds in any extra plugin functions found by
229 * looking in the older location
231 function cron_bc_hack_plugin_functions($plugintype, $plugins) {
232 global $CFG; // mandatory in case it is referenced by include()d PHP script
234 if ($plugintype === 'report') {
235 // Admin reports only - not course report because course report was
236 // never implemented before, so doesn't need BC
237 foreach (core_component
::get_plugin_list($plugintype) as $pluginname=>$dir) {
238 $component = $plugintype . '_' . $pluginname;
239 if (isset($plugins[$component])) {
240 // We already have detected the function using the new API
243 if (!file_exists("$dir/cron.php")) {
244 // No old style cron file present
247 include_once("$dir/cron.php");
248 $cronfunction = $component . '_cron';
249 if (function_exists($cronfunction)) {
250 $plugins[$component] = $cronfunction;
252 debugging("Invalid legacy cron.php detected in $component, " .
253 "please use lib.php instead");
256 } else if (strpos($plugintype, 'grade') === 0) {
257 // Detect old style cron function names
258 // Plugin gradeexport_frog used to use grade_export_frog_cron() instead of
259 // new standard API gradeexport_frog_cron(). Also applies to gradeimport, gradereport
260 foreach(core_component
::get_plugin_list($plugintype) as $pluginname=>$dir) {
261 $component = $plugintype.'_'.$pluginname;
262 if (isset($plugins[$component])) {
263 // We already have detected the function using the new API
266 if (!file_exists("$dir/lib.php")) {
269 include_once("$dir/lib.php");
270 $cronfunction = str_replace('grade', 'grade_', $plugintype) . '_' .
271 $pluginname . '_cron';
272 if (function_exists($cronfunction)) {
273 $plugins[$component] = $cronfunction;