source: moodle/trunk/fuentes/lib/classes/task/legacy_plugin_cron_task.php @ 1331

Last change on this file since 1331 was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 6.9 KB
Line 
1<?php
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 * Scheduled task class.
19 *
20 * @package    core
21 * @copyright  2013 onwards Martin Dougiamas  http://dougiamas.com
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace core\task;
25
26/**
27 * Simple task to run cron for all plugins.
28 * Note - this is only for plugins using the legacy cron method,
29 * plugins can also now just add their own scheduled tasks which is the preferred method.
30 */
31class legacy_plugin_cron_task extends scheduled_task {
32
33    /**
34     * Get a descriptive name for this task (shown to admins).
35     *
36     * @return string
37     */
38    public function get_name() {
39        return get_string('tasklegacycron', 'admin');
40    }
41
42    /**
43     * Do the job.
44     * Throw exceptions on errors (the job will be retried).
45     */
46    public function execute() {
47        global $CFG, $DB;
48
49        $timenow = time();
50        // Run the auth cron, if any before enrolments
51        // because it might add users that will be needed in enrol plugins.
52        $auths = get_enabled_auth_plugins();
53        mtrace("Running auth crons if required...");
54        foreach ($auths as $auth) {
55            $authplugin = get_auth_plugin($auth);
56            if (method_exists($authplugin, 'cron')) {
57                mtrace("Running cron for auth/$auth...");
58                $authplugin->cron();
59                if (!empty($authplugin->log)) {
60                    mtrace($authplugin->log);
61                }
62            }
63            unset($authplugin);
64        }
65
66        // It is very important to run enrol early
67        // because other plugins depend on correct enrolment info.
68        mtrace("Running enrol crons if required...");
69        $enrols = enrol_get_plugins(true);
70        foreach ($enrols as $ename => $enrol) {
71            // Do this for all plugins, disabled plugins might want to cleanup stuff such as roles.
72            if (!$enrol->is_cron_required()) {
73                continue;
74            }
75            mtrace("Running cron for enrol_$ename...");
76            $enrol->cron();
77            $enrol->set_config('lastcron', time());
78        }
79
80        // Run all cron jobs for each module.
81        mtrace("Starting activity modules");
82        if ($mods = $DB->get_records_select("modules", "cron > 0 AND ((? - lastcron) > cron) AND visible = 1", array($timenow))) {
83            foreach ($mods as $mod) {
84                $libfile = "$CFG->dirroot/mod/$mod->name/lib.php";
85                if (file_exists($libfile)) {
86                    include_once($libfile);
87                    $cronfunction = $mod->name."_cron";
88                    if (function_exists($cronfunction)) {
89                        mtrace("Processing module function $cronfunction ...\n", '');
90                        $predbqueries = null;
91                        $predbqueries = $DB->perf_get_queries();
92                        $pretime      = microtime(1);
93                        if ($cronfunction()) {
94                            $DB->set_field("modules", "lastcron", $timenow, array("id" => $mod->id));
95                        }
96                        if (isset($predbqueries)) {
97                            mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
98                            mtrace("... used " . (microtime(1) - $pretime) . " seconds");
99                        }
100                        // Reset possible changes by modules to time_limit. MDL-11597.
101                        \core_php_time_limit::raise();
102                        mtrace("done.");
103                    }
104                }
105            }
106        }
107        mtrace("Finished activity modules");
108
109        mtrace("Starting blocks");
110        if ($blocks = $DB->get_records_select("block", "cron > 0 AND ((? - lastcron) > cron) AND visible = 1", array($timenow))) {
111            // We will need the base class.
112            require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
113            foreach ($blocks as $block) {
114                $blockfile = $CFG->dirroot.'/blocks/'.$block->name.'/block_'.$block->name.'.php';
115                if (file_exists($blockfile)) {
116                    require_once($blockfile);
117                    $classname = '\\block_'.$block->name;
118                    $blockobj = new $classname;
119                    if (method_exists($blockobj, 'cron')) {
120                        mtrace("Processing cron function for ".$block->name.'....', '');
121                        if ($blockobj->cron()) {
122                            $DB->set_field('block', 'lastcron', $timenow, array('id' => $block->id));
123                        }
124                        // Reset possible changes by blocks to time_limit. MDL-11597.
125                        \core_php_time_limit::raise();
126                        mtrace('done.');
127                    }
128                }
129
130            }
131        }
132        mtrace('Finished blocks');
133
134        mtrace('Starting admin reports');
135        cron_execute_plugin_type('report');
136        mtrace('Finished admin reports');
137
138        mtrace('Starting course reports');
139        cron_execute_plugin_type('coursereport');
140        mtrace('Finished course reports');
141
142        // Run gradebook import/export/report cron.
143        mtrace('Starting gradebook plugins');
144        cron_execute_plugin_type('gradeimport');
145        cron_execute_plugin_type('gradeexport');
146        cron_execute_plugin_type('gradereport');
147        mtrace('Finished gradebook plugins');
148
149        // All other plugins.
150        cron_execute_plugin_type('message', 'message plugins');
151        cron_execute_plugin_type('filter', 'filters');
152        cron_execute_plugin_type('editor', 'editors');
153        cron_execute_plugin_type('format', 'course formats');
154        cron_execute_plugin_type('profilefield', 'profile fields');
155        cron_execute_plugin_type('webservice', 'webservices');
156        cron_execute_plugin_type('repository', 'repository plugins');
157        cron_execute_plugin_type('qbehaviour', 'question behaviours');
158        cron_execute_plugin_type('qformat', 'question import/export formats');
159        cron_execute_plugin_type('qtype', 'question types');
160        cron_execute_plugin_type('plagiarism', 'plagiarism plugins');
161        cron_execute_plugin_type('theme', 'themes');
162        cron_execute_plugin_type('tool', 'admin tools');
163        cron_execute_plugin_type('local', 'local plugins');
164    }
165
166}
Note: See TracBrowser for help on using the repository browser.