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