Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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      array('help' => false, 'list' => false, 'execute' => false, 'showsql' => false, 'showdebugging' => false),
  34      array('h' => 'help')
  35  );
  36  
  37  if ($unrecognized) {
  38      $unrecognized = implode("\n  ", $unrecognized);
  39      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  40  }
  41  
  42  if ($options['help'] or (!$options['list'] and !$options['execute'])) {
  43      $help =
  44      "Scheduled cron tasks.
  45  
  46      Options:
  47      --execute=\\some\\task  Execute scheduled task manually
  48      --list                List all scheduled tasks
  49      --showsql             Show sql queries before they are executed
  50      --showdebugging       Show developer level debugging information
  51      -h, --help            Print out this help
  52  
  53      Example:
  54      \$sudo -u www-data /usr/bin/php admin/cli/scheduled_task.php --execute=\\core\\task\\session_cleanup_task
  55  
  56      ";
  57  
  58      echo $help;
  59      die;
  60  }
  61  
  62  if ($options['showdebugging']) {
  63      set_debugging(DEBUG_DEVELOPER, true);
  64  }
  65  
  66  if ($options['showsql']) {
  67      $DB->set_debug(true);
  68  }
  69  if ($options['list']) {
  70      cli_heading("List of scheduled tasks ($CFG->wwwroot)");
  71  
  72      $shorttime = get_string('strftimedatetimeshort');
  73  
  74      $tasks = \core\task\manager::get_all_scheduled_tasks();
  75      echo str_pad(get_string('scheduledtasks', 'tool_task'), 50, ' ') . ' ' . str_pad(get_string('runpattern', 'tool_task'), 17, ' ')
  76          . ' ' . str_pad(get_string('lastruntime', 'tool_task'), 40, ' ') . get_string('nextruntime', 'tool_task') . "\n";
  77      foreach ($tasks as $task) {
  78          $class = '\\' . get_class($task);
  79          $schedule = $task->get_minute() . ' '
  80              . $task->get_hour() . ' '
  81              . $task->get_day() . ' '
  82              . $task->get_day_of_week() . ' '
  83              . $task->get_month() . ' '
  84              . $task->get_day_of_week();
  85          $nextrun = $task->get_next_run_time();
  86          $lastrun = $task->get_last_run_time();
  87  
  88          $plugininfo = core_plugin_manager::instance()->get_plugin_info($task->get_component());
  89          $plugindisabled = $plugininfo && $plugininfo->is_enabled() === false && !$task->get_run_if_component_disabled();
  90  
  91          if ($plugindisabled) {
  92              $nextrun = get_string('plugindisabled', 'tool_task');
  93          } else if ($task->get_disabled()) {
  94              $nextrun = get_string('taskdisabled', 'tool_task');
  95          } else if ($nextrun > time()) {
  96              $nextrun = userdate($nextrun);
  97          } else {
  98              $nextrun = get_string('asap', 'tool_task');
  99          }
 100  
 101          if ($lastrun) {
 102              $lastrun = userdate($lastrun);
 103          } else {
 104              $lastrun = get_string('never');
 105          }
 106  
 107          echo str_pad($class, 50, ' ') . ' ' . str_pad($schedule, 17, ' ') .
 108              ' ' . str_pad($lastrun, 40, ' ') . ' ' . $nextrun . "\n";
 109      }
 110      exit(0);
 111  }
 112  
 113  if ($execute = $options['execute']) {
 114      if (!$task = \core\task\manager::get_scheduled_task($execute)) {
 115          mtrace("Task '$execute' not found");
 116          exit(1);
 117      }
 118  
 119      if (moodle_needs_upgrading()) {
 120          mtrace("Moodle upgrade pending, cannot execute tasks.");
 121          exit(1);
 122      }
 123  
 124      // Increase memory limit.
 125      raise_memory_limit(MEMORY_EXTRA);
 126  
 127      // Emulate normal session - we use admin account by default.
 128      cron_setup_user();
 129  
 130      // Execute the task.
 131      \core\local\cli\shutdown::script_supports_graceful_exit();
 132      $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
 133      if (!$cronlock = $cronlockfactory->get_lock('core_cron', 10)) {
 134          mtrace('Cannot obtain cron lock');
 135          exit(129);
 136      }
 137      if (!$lock = $cronlockfactory->get_lock('\\' . get_class($task), 10)) {
 138          $cronlock->release();
 139          mtrace('Cannot obtain task lock');
 140          exit(130);
 141      }
 142  
 143      $task->set_lock($lock);
 144      if (!$task->is_blocking()) {
 145          $cronlock->release();
 146      } else {
 147          $task->set_cron_lock($cronlock);
 148      }
 149  
 150      cron_run_inner_scheduled_task($task);
 151  }