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 400] [Versions 310 and 401] [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   * Abstract class for common properties of scheduled_task and adhoc_task.
  19   *
  20   * @package    core
  21   * @category   task
  22   * @copyright  2013 Damyon Wiese
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace core\task;
  26  
  27  /**
  28   * Abstract class for common properties of scheduled_task and adhoc_task.
  29   *
  30   * @copyright  2013 Damyon Wiese
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  abstract class task_base {
  34  
  35      /** @var \core\lock\lock $lock - The lock controlling this task. */
  36      private $lock = null;
  37  
  38      /** @var \core\lock\lock $cronlock - The lock controlling the entire cron process. */
  39      private $cronlock = null;
  40  
  41      /** @var $string $component - The component this task belongs to. */
  42      private $component = '';
  43  
  44      /** @var bool $blocking - Does this task block the entire cron process. */
  45      private $blocking = false;
  46  
  47      /** @var int $faildelay - Exponentially increasing fail delay */
  48      private $faildelay = 0;
  49  
  50      /** @var int $nextruntime - When this task is due to run next */
  51      private $nextruntime = 0;
  52  
  53      /** @var int $timestarted - When this task was started */
  54      private $timestarted = null;
  55  
  56      /** @var string $hostname - Hostname where this task was started and PHP process ID */
  57      private $hostname = null;
  58  
  59      /** @var int $pid - PHP process ID that is running the task */
  60      private $pid = null;
  61  
  62      /**
  63       * Set the current lock for this task.
  64       * @param \core\lock\lock $lock
  65       */
  66      public function set_lock(\core\lock\lock $lock) {
  67          $this->lock = $lock;
  68      }
  69  
  70      /**
  71       * Set the current lock for the entire cron process.
  72       * @param \core\lock\lock $lock
  73       */
  74      public function set_cron_lock(\core\lock\lock $lock) {
  75          $this->cronlock = $lock;
  76      }
  77  
  78      /**
  79       * Get the current lock for this task.
  80       * @return \core\lock\lock
  81       */
  82      public function get_lock() {
  83          return $this->lock;
  84      }
  85  
  86      /**
  87       * Get the next run time for this task.
  88       * @return int timestamp
  89       */
  90      public function get_next_run_time() {
  91          return $this->nextruntime;
  92      }
  93  
  94      /**
  95       * Set the next run time for this task.
  96       * @param int $nextruntime
  97       */
  98      public function set_next_run_time($nextruntime) {
  99          $this->nextruntime = $nextruntime;
 100      }
 101  
 102      /**
 103       * Get the current lock for the entire cron.
 104       * @return \core\lock\lock
 105       */
 106      public function get_cron_lock() {
 107          return $this->cronlock;
 108      }
 109  
 110      /**
 111       * Setter for $blocking.
 112       * @param bool $blocking
 113       */
 114      public function set_blocking($blocking) {
 115          $this->blocking = $blocking;
 116      }
 117  
 118      /**
 119       * Getter for $blocking.
 120       * @return bool
 121       */
 122      public function is_blocking() {
 123          return $this->blocking;
 124      }
 125  
 126      /**
 127       * Setter for $component.
 128       * @param string $component
 129       */
 130      public function set_component($component) {
 131          $this->component = $component;
 132      }
 133  
 134      /**
 135       * Getter for $component.
 136       * @return string
 137       */
 138      public function get_component() {
 139          if (empty($this->component)) {
 140              // The component should be the root of the class namespace.
 141              $classname = get_class($this);
 142              $parts = explode('\\', $classname);
 143  
 144              if (count($parts) === 1) {
 145                  $component = substr($classname, 0, strpos($classname, '_task'));
 146              } else {
 147                  [$component] = $parts;
 148              }
 149  
 150              // Load component information from plugin manager.
 151              if ($component !== 'core' && strpos($component, 'core_') !== 0) {
 152                  $plugininfo = \core_plugin_manager::instance()->get_plugin_info($component);
 153                  if ($plugininfo && $plugininfo->component) {
 154                      $this->set_component($plugininfo->component);
 155                  } else {
 156                      debugging("Component not set and the class namespace does not match a valid component ({$component}).");
 157                  }
 158              }
 159          }
 160  
 161          return $this->component;
 162      }
 163  
 164      /**
 165       * Setter for $faildelay.
 166       * @param int $faildelay
 167       */
 168      public function set_fail_delay($faildelay) {
 169          $this->faildelay = $faildelay;
 170      }
 171  
 172      /**
 173       * Getter for $faildelay.
 174       * @return int
 175       */
 176      public function get_fail_delay() {
 177          return $this->faildelay;
 178      }
 179  
 180      /**
 181       * Do the job.
 182       * Throw exceptions on errors (the job will be retried).
 183       */
 184      public abstract function execute();
 185  
 186      /**
 187       * Setter for $timestarted.
 188       * @param int $timestarted
 189       */
 190      public function set_timestarted($timestarted = null) {
 191          $this->timestarted = $timestarted;
 192      }
 193  
 194      /**
 195       * Getter for $timestarted.
 196       * @return int
 197       */
 198      public function get_timestarted() {
 199          return $this->timestarted;
 200      }
 201  
 202      /**
 203       * Setter for $hostname.
 204       * @param string $hostname
 205       */
 206      public function set_hostname($hostname = null) {
 207          $this->hostname = $hostname;
 208      }
 209  
 210      /**
 211       * Getter for $hostname.
 212       * @return string
 213       */
 214      public function get_hostname() {
 215          return $this->hostname;
 216      }
 217  
 218      /**
 219       * Setter for $pid.
 220       * @param int $pid
 221       */
 222      public function set_pid($pid = null) {
 223          $this->pid = $pid;
 224      }
 225  
 226      /**
 227       * Getter for $pid.
 228       * @return int
 229       */
 230      public function get_pid() {
 231          return $this->pid;
 232      }
 233  }