[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/task/cli/ -> schedule_task.php (source)

   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    tool_task
  21   * @copyright  2014 Petr Skoda
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  define('CLI_SCRIPT', true);
  26  
  27  require(__DIR__ . '/../../../../config.php');
  28  require_once("$CFG->libdir/clilib.php");
  29  require_once("$CFG->libdir/cronlib.php");
  30  
  31  list($options, $unrecognized) = cli_get_params(
  32      array('help' => false, 'list' => false, 'execute' => false, 'showsql' => false, 'showdebugging' => false),
  33      array('h' => 'help')
  34  );
  35  
  36  if ($unrecognized) {
  37      $unrecognized = implode("\n  ", $unrecognized);
  38      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  39  }
  40  
  41  if ($options['help'] or (!$options['list'] and !$options['execute'])) {
  42      $help =
  43  "Scheduled cron tasks.
  44  
  45  Options:
  46  --execute=\\\\some\\\\task  Execute scheduled task manually
  47  --list                List all scheduled tasks
  48  --showsql             Show sql queries before they are executed
  49  --showdebugging       Show developer level debugging information
  50  -h, --help            Print out this help
  51  
  52  Example:
  53  \$sudo -u www-data /usr/bin/php admin/tool/task/cli/scheduled_task.php --execute=\\\\core\\\\task\\\\session_cleanup_task
  54  
  55  ";
  56  
  57      echo $help;
  58      die;
  59  }
  60  
  61  if ($options['showdebugging']) {
  62      set_debugging(DEBUG_DEVELOPER, true);
  63  }
  64  
  65  if ($options['showsql']) {
  66      $DB->set_debug(true);
  67  }
  68  if ($options['list']) {
  69      cli_heading("List of scheduled tasks ($CFG->wwwroot)");
  70  
  71      $shorttime = get_string('strftimedatetimeshort');
  72  
  73      $tasks = \core\task\manager::get_all_scheduled_tasks();
  74      foreach ($tasks as $task) {
  75          $class = '\\' . get_class($task);
  76          $schedule = $task->get_minute() . ' '
  77              . $task->get_hour() . ' '
  78              . $task->get_day() . ' '
  79              . $task->get_day_of_week() . ' '
  80              . $task->get_month() . ' '
  81              . $task->get_day_of_week();
  82          $nextrun = $task->get_next_run_time();
  83  
  84          $plugininfo = core_plugin_manager::instance()->get_plugin_info($task->get_component());
  85          $plugindisabled = $plugininfo && $plugininfo->is_enabled() === false && !$task->get_run_if_component_disabled();
  86  
  87          if ($plugindisabled) {
  88              $nextrun = get_string('plugindisabled', 'tool_task');
  89          } else if ($task->get_disabled()) {
  90              $nextrun = get_string('taskdisabled', 'tool_task');
  91          } else if ($nextrun > time()) {
  92              $nextrun = userdate($nextrun);
  93          } else {
  94              $nextrun = get_string('asap', 'tool_task');
  95          }
  96  
  97          echo str_pad($class, 50, ' ') . ' ' . str_pad($schedule, 17, ' ') . ' ' . $nextrun . "\n";
  98      }
  99      exit(0);
 100  }
 101  
 102  if ($execute = $options['execute']) {
 103      if (!$task = \core\task\manager::get_scheduled_task($execute)) {
 104          mtrace("Task '$execute' not found");
 105          exit(1);
 106      }
 107  
 108      if (moodle_needs_upgrading()) {
 109          mtrace("Moodle upgrade pending, cannot execute tasks.");
 110          exit(1);
 111      }
 112  
 113      // Increase memory limit.
 114      raise_memory_limit(MEMORY_EXTRA);
 115  
 116      // Emulate normal session - we use admin account by default.
 117      cron_setup_user();
 118  
 119      $predbqueries = $DB->perf_get_queries();
 120      $pretime = microtime(true);
 121  
 122      $fullname = $task->get_name() . ' (' . get_class($task) . ')';
 123      mtrace('Execute scheduled task: ' . $fullname);
 124      // NOTE: it would be tricky to move this code to \core\task\manager class,
 125      //       because we want to do detailed error reporting.
 126      $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
 127      if (!$cronlock = $cronlockfactory->get_lock('core_cron', 10)) {
 128          mtrace('Cannot obtain cron lock');
 129          exit(129);
 130      }
 131      if (!$lock = $cronlockfactory->get_lock('\\' . get_class($task), 10)) {
 132          $cronlock->release();
 133          mtrace('Cannot obtain task lock');
 134          exit(130);
 135      }
 136  
 137      $task->set_lock($lock);
 138      if (!$task->is_blocking()) {
 139          $cronlock->release();
 140      } else {
 141          $task->set_cron_lock($cronlock);
 142      }
 143  
 144      try {
 145          get_mailer('buffer');
 146          $task->execute();
 147          if (isset($predbqueries)) {
 148              mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
 149              mtrace("... used " . (microtime(1) - $pretime) . " seconds");
 150          }
 151          mtrace('Scheduled task complete: ' . $fullname);
 152          \core\task\manager::scheduled_task_complete($task);
 153          get_mailer('close');
 154          exit(0);
 155      } catch (Exception $e) {
 156          if ($DB->is_transaction_started()) {
 157              $DB->force_transaction_rollback();
 158          }
 159          mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
 160          mtrace("... used " . (microtime(true) - $pretime) . " seconds");
 161          mtrace('Scheduled task failed: ' . $fullname . ',' . $e->getMessage());
 162          if ($CFG->debugdeveloper) {
 163              if (!empty($e->debuginfo)) {
 164                  mtrace("Debug info:");
 165                  mtrace($e->debuginfo);
 166              }
 167              mtrace("Backtrace:");
 168              mtrace(format_backtrace($e->getTrace(), true));
 169          }
 170          \core\task\manager::scheduled_task_failed($task);
 171          get_mailer('close');
 172          exit(1);
 173      }
 174  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1