Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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   * Task executor for adhoc tasks.
  19   *
  20   * @package    core
  21   * @subpackage cli
  22   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  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  
  31  list($options, $unrecognized) = cli_get_params(
  32      [
  33          'help' => false,
  34          'showsql' => false,
  35          'showdebugging' => false,
  36          'execute' => false,
  37          'keep-alive' => 0,
  38          'ignorelimits' => false,
  39          'force' => false,
  40          'id' => null,
  41          'classname' => null,
  42          'taskslimit' => null,
  43          'failed' => false,
  44      ], [
  45          'h' => 'help',
  46          'e' => 'execute',
  47          'k' => 'keep-alive',
  48          'i' => 'ignorelimits',
  49          'f' => 'force',
  50          'c' => 'classname',
  51          'l' => 'taskslimit',
  52      ]
  53  );
  54  
  55  if ($unrecognized) {
  56      $unrecognized = implode("\n  ", $unrecognized);
  57      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  58  }
  59  
  60  $help = <<<EOT
  61  Ad hoc cron tasks.
  62  
  63  Options:
  64   -h, --help                Print out this help
  65       --showsql             Show sql queries before they are executed
  66       --showdebugging       Show developer level debugging information
  67   -e, --execute             Run all queued adhoc tasks
  68   -k, --keep-alive=N        Keep this script alive for N seconds and poll for new adhoc tasks
  69   -i  --ignorelimits        Ignore task_adhoc_concurrency_limit and task_adhoc_max_runtime limits
  70   -f, --force               Run even if cron is disabled
  71       --id                  Run (failed) task with id
  72   -c, --classname           Run tasks with a certain classname (FQN)
  73   -l, --taskslimit=N        Run at most N tasks
  74       --failed              Run only tasks that failed, ie those with a fail delay
  75  
  76  Run all queued tasks:
  77  \$sudo -u www-data /usr/bin/php admin/cli/adhoc_task.php --execute
  78  
  79  Run all queued tasks of specific class:
  80  \$sudo -u www-data /usr/bin/php admin/cli/adhoc_task.php --classname=\\\\core_course\\\\task\\\\course_delete_modules
  81  
  82  Double backslash for the shell escape reasons.
  83  Run a specific task:
  84  \$sudo -u www-data /usr/bin/php admin/cli/adhoc_task.php --id=123456
  85  
  86  Run a specific task with debugging:
  87  \$sudo -u www-data /usr/bin/php admin/cli/adhoc_task.php --id=123456 --showsql --showdebugging
  88  
  89  To profile a long running task:
  90  \$sudo -u www-data /usr/bin/php admin/cli/adhoc_task.php --taskslimit=1 --classname='\\some\\class\\name' --ignorelimits
  91  
  92  EOT;
  93  
  94  if ($options['help']) {
  95      echo $help;
  96      exit(0);
  97  }
  98  
  99  if (CLI_MAINTENANCE) {
 100      echo "CLI maintenance mode active, cron execution suspended.\n";
 101      exit(1);
 102  }
 103  
 104  if (moodle_needs_upgrading()) {
 105      echo "Moodle upgrade pending, cron execution suspended.\n";
 106      exit(1);
 107  }
 108  
 109  if (!get_config('core', 'cron_enabled') && !$options['force']) {
 110      mtrace('Cron is disabled. Use --force to override.');
 111      exit(1);
 112  }
 113  
 114  // Common debugging options.
 115  if ($options['showdebugging']) {
 116      set_debugging(DEBUG_DEVELOPER, true);
 117  }
 118  
 119  if ($options['showsql']) {
 120      $DB->set_debug(true);
 121  }
 122  
 123  if (!empty($CFG->showcronsql)) {
 124      $DB->set_debug(true);
 125  }
 126  if (!empty($CFG->showcrondebugging)) {
 127      set_debugging(DEBUG_DEVELOPER, true);
 128  }
 129  
 130  // Process params.
 131  core_php_time_limit::raise();
 132  
 133  // Increase memory limit.
 134  raise_memory_limit(MEMORY_EXTRA);
 135  
 136  // Emulate normal session - we use admin account by default.
 137  \core\cron::setup_user();
 138  
 139  \core\local\cli\shutdown::script_supports_graceful_exit();
 140  $humantimenow = date('r', time());
 141  mtrace("Server Time: {$humantimenow}\n");
 142  
 143  $classname = $options['classname'];
 144  
 145  // Run a single adhoc task only, if requested.
 146  if (!empty($options['id'])) {
 147      $taskid = (int) $options['id'];
 148      \core\cron::run_adhoc_task($taskid);
 149      exit(0);
 150  }
 151  
 152  // Run all failed tasks.
 153  if (!empty($options['failed'])) {
 154      \core\cron::run_failed_adhoc_tasks($classname);
 155      exit(0);
 156  }
 157  
 158  // Examine params and determine if we should run.
 159  $execute = (bool) $options['execute'];
 160  $keepalive = empty($options['keep-alive']) ? 0 : (int) $options['keep-alive'];
 161  $taskslimit = empty($options['taskslimit']) ? null : (int) $options['taskslimit'];
 162  $checklimits = empty($options['ignorelimits']);
 163  
 164  if ($classname || $keepalive || $taskslimit) {
 165      $execute = true;
 166  }
 167  
 168  // Output the help text if no criteria for running the adhoc tasks are given.
 169  if (!$execute) {
 170      echo $help;
 171      exit(0);
 172  }
 173  
 174  \core\cron::run_adhoc_tasks(time(), $keepalive, $checklimits, null, $taskslimit, $classname);