Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 39 and 311]

   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   * @deprecated since Moodle 3.9 MDL-63580. Please use the admin/cli/adhoc_task.php.
  21   * @todo final deprecation. To be removed in Moodle 4.1 MDL-63594.
  22   *
  23   * @package    tool_task
  24   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  define('CLI_SCRIPT', true);
  29  
  30  require(__DIR__ . '/../../../../config.php');
  31  require_once("{$CFG->libdir}/clilib.php");
  32  require_once("{$CFG->libdir}/cronlib.php");
  33  
  34  list($options, $unrecognized) = cli_get_params(
  35      [
  36          'execute' => false,
  37          'help' => false,
  38          'keep-alive' => 0,
  39          'showsql' => false,
  40          'showdebugging' => false,
  41          'ignorelimits' => false,
  42      ], [
  43          'h' => 'help',
  44          'e' => 'execute',
  45          'k' => 'keep-alive',
  46          'i' => 'ignorelimits',
  47      ]
  48  );
  49  
  50  debugging('admin/tool/task/cli/adhoc_task.php is deprecated. Please use admin/cli/adhoc_task.php instead.', DEBUG_DEVELOPER);
  51  
  52  if ($unrecognized) {
  53      $unrecognized = implode("\n  ", $unrecognized);
  54      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  55  }
  56  
  57  if ($options['help'] or empty($options['execute'])) {
  58      $help = <<<EOT
  59  Ad hoc cron tasks.
  60  
  61  Options:
  62   -h, --help                Print out this help
  63       --showsql             Show sql queries before they are executed
  64       --showdebugging       Show developer level debugging information
  65   -e, --execute             Run all queued adhoc tasks
  66   -k, --keep-alive=N        Keep this script alive for N seconds and poll for new adhoc tasks
  67   -i  --ignorelimits        Ignore task_adhoc_concurrency_limit and task_adhoc_max_runtime limits
  68  
  69  Example:
  70  \$sudo -u www-data /usr/bin/php admin/tool/task/cli/adhoc_task.php --execute
  71  
  72  EOT;
  73  
  74      echo $help;
  75      die;
  76  }
  77  
  78  if ($options['showdebugging']) {
  79      set_debugging(DEBUG_DEVELOPER, true);
  80  }
  81  
  82  if ($options['showsql']) {
  83      $DB->set_debug(true);
  84  }
  85  
  86  if (CLI_MAINTENANCE) {
  87      echo "CLI maintenance mode active, cron execution suspended.\n";
  88      exit(1);
  89  }
  90  
  91  if (moodle_needs_upgrading()) {
  92      echo "Moodle upgrade pending, cron execution suspended.\n";
  93      exit(1);
  94  }
  95  
  96  if (empty($options['execute'])) {
  97      exit(0);
  98  }
  99  if (empty($options['keep-alive'])) {
 100      $options['keep-alive'] = 0;
 101  }
 102  
 103  if (!empty($CFG->showcronsql)) {
 104      $DB->set_debug(true);
 105  }
 106  if (!empty($CFG->showcrondebugging)) {
 107      set_debugging(DEBUG_DEVELOPER, true);
 108  }
 109  
 110  $checklimits = empty($options['ignorelimits']);
 111  
 112  core_php_time_limit::raise();
 113  
 114  // Increase memory limit.
 115  raise_memory_limit(MEMORY_EXTRA);
 116  
 117  // Emulate normal session - we use admin account by default.
 118  cron_setup_user();
 119  
 120  $humantimenow = date('r', time());
 121  $keepalive = (int)$options['keep-alive'];
 122  
 123  \core\local\cli\shutdown::script_supports_graceful_exit();
 124  
 125  mtrace("Server Time: {$humantimenow}\n");
 126  cron_run_adhoc_tasks(time(), $keepalive, $checklimits);