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 310 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 and 403]

   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   * CLI task execution.
  19   *
  20   * @package    core
  21   * @subpackage cli
  22   * @copyright  2014 Petr Skoda
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  define('CLI_SCRIPT', true);
  27  
  28  require(__DIR__ . '/../../config.php');
  29  require_once("$CFG->libdir/clilib.php");
  30  require_once("$CFG->libdir/cronlib.php");
  31  
  32  list($options, $unrecognized) = cli_get_params(
  33      [
  34          'help' => false,
  35          'list' => false,
  36          'execute' => false,
  37          'showsql' => false,
  38          'showdebugging' => false,
  39          'force' => false,
  40      ], [
  41          'h' => 'help',
  42          'f' => 'force',
  43      ]
  44  );
  45  
  46  if ($unrecognized) {
  47      $unrecognized = implode("\n  ", $unrecognized);
  48      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  49  }
  50  
  51  if ($options['help'] or (!$options['list'] and !$options['execute'])) {
  52      $help =
  53      "Scheduled cron tasks.
  54  
  55      Options:
  56      --execute=\\some\\task  Execute scheduled task manually
  57      --list                List all scheduled tasks
  58      --showsql             Show sql queries before they are executed
  59      --showdebugging       Show developer level debugging information
  60      -h, --help            Print out this help
  61      -f, --force           Execute task even if cron is disabled
  62  
  63      Example:
  64      \$sudo -u www-data /usr/bin/php admin/cli/scheduled_task.php --execute=\\core\\task\\session_cleanup_task
  65  
  66      ";
  67  
  68      echo $help;
  69      die;
  70  }
  71  
  72  if ($options['showdebugging'] || !empty($CFG->showcrondebugging)) {
  73      set_debugging(DEBUG_DEVELOPER, true);
  74  }
  75  
  76  if ($options['showsql'] || !empty($CFG->showcronsql)) {
  77      $DB->set_debug(true);
  78  }
  79  if ($options['list']) {
  80      cli_heading("List of scheduled tasks ($CFG->wwwroot)");
  81  
  82      $shorttime = get_string('strftimedatetimeshort');
  83  
  84      $tasks = \core\task\manager::get_all_scheduled_tasks();
  85      echo str_pad(get_string('scheduledtasks', 'tool_task'), 50, ' ') . ' ' . str_pad(get_string('runpattern', 'tool_task'), 17, ' ')
  86          . ' ' . str_pad(get_string('lastruntime', 'tool_task'), 40, ' ') . get_string('nextruntime', 'tool_task') . "\n";
  87      foreach ($tasks as $task) {
  88          $class = '\\' . get_class($task);
  89          $schedule = $task->get_minute() . ' '
  90              . $task->get_hour() . ' '
  91              . $task->get_day() . ' '
  92              . $task->get_day_of_week() . ' '
  93              . $task->get_month() . ' '
  94              . $task->get_day_of_week();
  95          $nextrun = $task->get_next_run_time();
  96          $lastrun = $task->get_last_run_time();
  97  
  98          $plugininfo = core_plugin_manager::instance()->get_plugin_info($task->get_component());
  99          $plugindisabled = $plugininfo && $plugininfo->is_enabled() === false && !$task->get_run_if_component_disabled();
 100  
 101          if ($plugindisabled) {
 102              $nextrun = get_string('plugindisabled', 'tool_task');
 103          } else if ($task->get_disabled()) {
 104              $nextrun = get_string('taskdisabled', 'tool_task');
 105          } else if ($nextrun > time()) {
 106              $nextrun = userdate($nextrun);
 107          } else {
 108              $nextrun = get_string('asap', 'tool_task');
 109          }
 110  
 111          if ($lastrun) {
 112              $lastrun = userdate($lastrun);
 113          } else {
 114              $lastrun = get_string('never');
 115          }
 116  
 117          echo str_pad($class, 50, ' ') . ' ' . str_pad($schedule, 17, ' ') .
 118              ' ' . str_pad($lastrun, 40, ' ') . ' ' . $nextrun . "\n";
 119      }
 120      exit(0);
 121  }
 122  
 123  if ($execute = $options['execute']) {
 124      if (!$task = \core\task\manager::get_scheduled_task($execute)) {
 125          mtrace("Task '$execute' not found");
 126          exit(1);
 127      }
 128  
 129      if (moodle_needs_upgrading()) {
 130          mtrace("Moodle upgrade pending, cannot execute tasks.");
 131          exit(1);
 132      }
 133  
 134      if (!get_config('core', 'cron_enabled') && !$options['force']) {
 135          mtrace('Cron is disabled. Use --force to override.');
 136          exit(1);
 137      }
 138  
 139      \core\task\manager::scheduled_task_starting($task);
 140  
 141      // Increase memory limit.
 142      raise_memory_limit(MEMORY_EXTRA);
 143  
 144      // Emulate normal session - we use admin account by default.
 145      cron_setup_user();
 146  
 147      // Execute the task.
 148      \core\local\cli\shutdown::script_supports_graceful_exit();
 149      $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
 150      if (!$cronlock = $cronlockfactory->get_lock('core_cron', 10)) {
 151          mtrace('Cannot obtain cron lock');
 152          exit(129);
 153      }
 154      if (!$lock = $cronlockfactory->get_lock('\\' . get_class($task), 10)) {
 155          $cronlock->release();
 156          mtrace('Cannot obtain task lock');
 157          exit(130);
 158      }
 159  
 160      $task->set_lock($lock);
 161      if (!$task->is_blocking()) {
 162          $cronlock->release();
 163      } else {
 164          $task->set_cron_lock($cronlock);
 165      }
 166  
 167      cron_run_inner_scheduled_task($task);
 168  }