Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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       * Set the current lock for this task.
  67       * @param \core\lock\lock $lock
  68       */
  69      public function set_lock(\core\lock\lock $lock) {
  70          $this->lock = $lock;
  71      }
  72  
  73      /**
  74       * Set the current lock for the entire cron process.
  75       * @param \core\lock\lock $lock
  76       */
  77      public function set_cron_lock(\core\lock\lock $lock) {
  78          $this->cronlock = $lock;
  79      }
  80  
  81      /**
  82       * Get the current lock for this task.
  83       * @return \core\lock\lock
  84       */
  85      public function get_lock() {
  86          return $this->lock;
  87      }
  88  
  89      /**
  90       * Get the next run time for this task.
  91       * @return int timestamp
  92       */
  93      public function get_next_run_time() {
  94          return $this->nextruntime;
  95      }
  96  
  97      /**
  98       * Set the next run time for this task.
  99       * @param int $nextruntime
 100       */
 101      public function set_next_run_time($nextruntime) {
 102          $this->nextruntime = $nextruntime;
 103      }
 104  
 105      /**
 106       * Get the current lock for the entire cron.
 107       * @return \core\lock\lock
 108       */
 109      public function get_cron_lock() {
 110          return $this->cronlock;
 111      }
 112  
 113      /**
 114       * Setter for $blocking.
 115       * @param bool $blocking
 116       */
 117      public function set_blocking($blocking) {
 118          $this->blocking = $blocking;
 119      }
 120  
 121      /**
 122       * Getter for $blocking.
 123       * @return bool
 124       */
 125      public function is_blocking() {
 126          return $this->blocking;
 127      }
 128  
 129      /**
 130       * Setter for $component.
 131       * @param string $component
 132       */
 133      public function set_component($component) {
 134          $this->component = $component;
 135      }
 136  
 137      /**
 138       * Getter for $component.
 139       * @return string
 140       */
 141      public function get_component() {
 142          if (empty($this->component)) {
 143              // The component should be the root of the class namespace.
 144              $classname = get_class($this);
 145              $parts = explode('\\', $classname);
 146  
 147              if (count($parts) === 1) {
 148                  $component = substr($classname, 0, strpos($classname, '_task'));
 149              } else {
 150                  [$component] = $parts;
 151              }
 152  
 153              // Load component information from plugin manager.
 154              if ($component !== 'core' && strpos($component, 'core_') !== 0) {
 155                  $plugininfo = \core_plugin_manager::instance()->get_plugin_info($component);
 156                  if ($plugininfo && $plugininfo->component) {
 157                      $this->set_component($plugininfo->component);
 158                  } else {
 159                      debugging("Component not set and the class namespace does not match a valid component ({$component}).");
 160                  }
 161              }
 162          }
 163  
 164          return $this->component;
 165      }
 166  
 167      /**
 168       * Setter for $faildelay.
 169       * @param int $faildelay
 170       */
 171      public function set_fail_delay($faildelay) {
 172          $this->faildelay = $faildelay;
 173      }
 174  
 175      /**
 176       * Getter for $faildelay.
 177       * @return int
 178       */
 179      public function get_fail_delay() {
 180          return $this->faildelay;
 181      }
 182  
 183      /**
 184       * Do the job.
 185       * Throw exceptions on errors (the job will be retried).
 186       */
 187      public abstract function execute();
 188  
 189      /**
 190       * Setter for $timestarted.
 191       * @param int $timestarted
 192       */
 193      public function set_timestarted($timestarted = null) {
 194          $this->timestarted = $timestarted;
 195      }
 196  
 197      /**
 198       * Getter for $timestarted.
 199       * @return int
 200       */
 201      public function get_timestarted() {
 202          return $this->timestarted;
 203      }
 204  
 205      /**
 206       * Setter for $hostname.
 207       * @param string $hostname
 208       */
 209      public function set_hostname($hostname = null) {
 210          $this->hostname = $hostname;
 211      }
 212  
 213      /**
 214       * Getter for $hostname.
 215       * @return string
 216       */
 217      public function get_hostname() {
 218          return $this->hostname;
 219      }
 220  
 221      /**
 222       * Setter for $pid.
 223       * @param int $pid
 224       */
 225      public function set_pid($pid = null) {
 226          $this->pid = $pid;
 227      }
 228  
 229      /**
 230       * Getter for $pid.
 231       * @return int
 232       */
 233      public function get_pid() {
 234          return $this->pid;
 235      }
 236  
 237      /**
 238       * Informs whether the task's component is enabled.
 239       * @return bool true when enabled. false otherwise.
 240       */
 241      public function is_component_enabled(): bool {
 242          $component = $this->get_component();
 243  
 244          // An entire core component type cannot be explicitly disabled.
 245          [$componenttype] = core_component::normalize_component($component);
 246          if ($componenttype === 'core') {
 247              return true;
 248          } else {
 249              $plugininfo = core_plugin_manager::instance()->get_plugin_info($component);
 250              return $plugininfo && ($plugininfo->is_enabled() !== false);
 251          }
 252      }
 253  }