Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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  require_once("{$CFG->libdir}/cronlib.php");
  31  
  32  list($options, $unrecognized) = cli_get_params(
  33      [
  34          'execute' => false,
  35          'help' => false,
  36          'keep-alive' => 0,
  37          'showsql' => false,
  38          'showdebugging' => false,
  39          'ignorelimits' => false,
  40          'force' => false,
  41      ], [
  42          'h' => 'help',
  43          'e' => 'execute',
  44          'k' => 'keep-alive',
  45          'i' => 'ignorelimits',
  46          'f' => 'force',
  47      ]
  48  );
  49  
  50  if ($unrecognized) {
  51      $unrecognized = implode("\n  ", $unrecognized);
  52      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  53  }
  54  
  55  if ($options['help'] or empty($options['execute'])) {
  56      $help = <<<EOT
  57  Ad hoc cron tasks.
  58  
  59  Options:
  60   -h, --help                Print out this help
  61       --showsql             Show sql queries before they are executed
  62       --showdebugging       Show developer level debugging information
  63   -e, --execute             Run all queued adhoc tasks
  64   -k, --keep-alive=N        Keep this script alive for N seconds and poll for new adhoc tasks
  65   -i  --ignorelimits        Ignore task_adhoc_concurrency_limit and task_adhoc_max_runtime limits
  66   -f, --force               Run even if cron is disabled
  67  
  68  Example:
  69  \$sudo -u www-data /usr/bin/php admin/cli/adhoc_task.php --execute
  70  
  71  EOT;
  72  
  73      echo $help;
  74      die;
  75  }
  76  
  77  if ($options['showdebugging']) {
  78      set_debugging(DEBUG_DEVELOPER, true);
  79  }
  80  
  81  if ($options['showsql']) {
  82      $DB->set_debug(true);
  83  }
  84  
  85  if (CLI_MAINTENANCE) {
  86      echo "CLI maintenance mode active, cron execution suspended.\n";
  87      exit(1);
  88  }
  89  
  90  if (moodle_needs_upgrading()) {
  91      echo "Moodle upgrade pending, cron execution suspended.\n";
  92      exit(1);
  93  }
  94  
  95  if (empty($options['execute'])) {
  96      exit(0);
  97  }
  98  
  99  if (!get_config('core', 'cron_enabled') && !$options['force']) {
 100      mtrace('Cron is disabled. Use --force to override.');
 101      exit(1);
 102  }
 103  
 104  if (empty($options['keep-alive'])) {
 105      $options['keep-alive'] = 0;
 106  }
 107  
 108  if (!empty($CFG->showcronsql)) {
 109      $DB->set_debug(true);
 110  }
 111  if (!empty($CFG->showcrondebugging)) {
 112      set_debugging(DEBUG_DEVELOPER, true);
 113  }
 114  
 115  $checklimits = empty($options['ignorelimits']);
 116  
 117  core_php_time_limit::raise();
 118  
 119  // Increase memory limit.
 120  raise_memory_limit(MEMORY_EXTRA);
 121  
 122  // Emulate normal session - we use admin account by default.
 123  cron_setup_user();
 124  
 125  $humantimenow = date('r', time());
 126  $keepalive = (int)$options['keep-alive'];
 127  
 128  \core\local\cli\shutdown::script_supports_graceful_exit();
 129  
 130  mtrace("Server Time: {$humantimenow}\n");
 131  cron_run_adhoc_tasks(time(), $keepalive, $checklimits);