[ Index ]

PHP Cross Reference of moodle-2.8

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),
  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  -h, --help            Print out this help
  49  
  50  Example:
  51  \$sudo -u www-data /usr/bin/php admin/tool/task/cli/scheduled_task.php --execute=\\\\core\\\\task\\\\session_cleanup_task
  52  
  53  ";
  54  
  55      echo $help;
  56      die;
  57  }
  58  
  59  if ($options['list']) {
  60      cli_heading("List of scheduled tasks ($CFG->wwwroot)");
  61  
  62      $shorttime = get_string('strftimedatetimeshort');
  63  
  64      $tasks = \core\task\manager::get_all_scheduled_tasks();
  65      foreach ($tasks as $task) {
  66          $class = '\\' . get_class($task);
  67          $schedule = $task->get_minute() . ' '
  68              . $task->get_hour() . ' '
  69              . $task->get_day() . ' '
  70              . $task->get_day_of_week() . ' '
  71              . $task->get_month() . ' '
  72              . $task->get_day_of_week();
  73          $nextrun = $task->get_next_run_time();
  74  
  75          if ($task->get_disabled()) {
  76              $nextrun = get_string('disabled', 'tool_task');
  77          } else if ($nextrun > time()) {
  78              $nextrun = userdate($nextrun);
  79          } else {
  80              $nextrun = get_string('asap', 'tool_task');
  81          }
  82  
  83          echo str_pad($class, 50, ' ') . ' ' . str_pad($schedule, 17, ' ') . ' ' . $nextrun . "\n";
  84      }
  85      exit(0);
  86  }
  87  
  88  if ($execute = $options['execute']) {
  89      if (!$task = \core\task\manager::get_scheduled_task($execute)) {
  90          mtrace("Task '$execute' not found");
  91          exit(1);
  92      }
  93  
  94      if (moodle_needs_upgrading()) {
  95          mtrace("Moodle upgrade pending, cannot execute tasks.");
  96          exit(1);
  97      }
  98  
  99      // Increase memory limit.
 100      raise_memory_limit(MEMORY_EXTRA);
 101  
 102      // Emulate normal session - we use admin account by default.
 103      cron_setup_user();
 104  
 105      $predbqueries = $DB->perf_get_queries();
 106      $pretime = microtime(true);
 107      try {
 108          mtrace("Scheduled task: " . $task->get_name());
 109          // NOTE: it would be tricky to move this code to \core\task\manager class,
 110          //       because we want to do detailed error reporting.
 111          $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
 112          if (!$cronlock = $cronlockfactory->get_lock('core_cron', 10)) {
 113              mtrace('Cannot obtain cron lock');
 114              exit(129);
 115          }
 116          if (!$lock = $cronlockfactory->get_lock('\\' . get_class($task), 10)) {
 117              mtrace('Cannot obtain task lock');
 118              exit(130);
 119          }
 120          $task->set_lock($lock);
 121          if (!$task->is_blocking()) {
 122              $cronlock->release();
 123          } else {
 124              $task->set_cron_lock($cronlock);
 125          }
 126          $task->execute();
 127          if (isset($predbqueries)) {
 128              mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
 129              mtrace("... used " . (microtime(1) - $pretime) . " seconds");
 130          }
 131          mtrace("Task completed.");
 132          \core\task\manager::scheduled_task_complete($task);
 133          exit(0);
 134      } catch (Exception $e) {
 135          if ($DB->is_transaction_started()) {
 136              $DB->force_transaction_rollback();
 137          }
 138          mtrace("... used " . ($DB->perf_get_queries() - $predbqueries) . " dbqueries");
 139          mtrace("... used " . (microtime(true) - $pretime) . " seconds");
 140          mtrace("Task failed: " . $e->getMessage());
 141          \core\task\manager::scheduled_task_failed($task);
 142          exit(1);
 143      }
 144  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1