Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 39 and 401]

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