Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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