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]

   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   * Adhoc task abstract class.
  19   *
  20   * All background tasks should extend this class.
  21   *
  22   * @package    core
  23   * @category   task
  24   * @copyright  2013 Damyon Wiese
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  namespace core\task;
  29  
  30  /**
  31   * Abstract class defining an adhoc task.
  32   * @copyright  2013 Damyon Wiese
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  abstract class adhoc_task extends task_base {
  36  
  37      /** @var string $customdata - Custom data required for when this task is executed. */
  38      private $customdata = '';
  39  
  40      /** @var integer|null $id - Adhoc tasks each have their own database record id. */
  41      private $id = null;
  42  
  43      /** @var integer|null $userid - Adhoc tasks may choose to run as a specific user. */
  44      private $userid = null;
  45  
  46      /** @var \core\lock\lock The concurrency task lock for this task. */
  47      private $concurrencylock = null;
  48  
  49      /**
  50       * Provide default implementation of the task name for backward compatibility. Extending classes are expected to implement
  51       * this method to provide a descriptive name for the task (shown to admins)
  52       *
  53       * @return string
  54       */
  55      public function get_name() {
  56          $classparts = explode('\\', get_called_class());
  57          $classname = end($classparts);
  58  
  59          // Try to make human readable, capitalized and with spaces.
  60          return ucfirst(str_replace('_', ' ', $classname));
  61      }
  62  
  63      /**
  64       * Setter for $id.
  65       * @param int|null $id
  66       */
  67      public function set_id($id) {
  68          $this->id = $id;
  69      }
  70  
  71      /**
  72       * Getter for $userid.
  73       * @return int|null $userid
  74       */
  75      public function get_userid() {
  76          return $this->userid;
  77      }
  78  
  79      /**
  80       * Setter for $customdata.
  81       * @param mixed $customdata (anything that can be handled by json_encode)
  82       */
  83      public function set_custom_data($customdata) {
  84          $this->customdata = json_encode($customdata);
  85      }
  86  
  87      /**
  88       * Alternate setter for $customdata. Expects the data as a json_encoded string.
  89       * @param string $customdata json_encoded string
  90       */
  91      public function set_custom_data_as_string($customdata) {
  92          $this->customdata = $customdata;
  93      }
  94  
  95      /**
  96       * Getter for $customdata.
  97       * @return mixed (anything that can be handled by json_decode).
  98       */
  99      public function get_custom_data() {
 100          return json_decode($this->customdata);
 101      }
 102  
 103      /**
 104       * Alternate getter for $customdata.
 105       * @return string this is the raw json encoded version.
 106       */
 107      public function get_custom_data_as_string() {
 108          return $this->customdata;
 109      }
 110  
 111      /**
 112       * Getter for $id.
 113       * @return int|null $id
 114       */
 115      public function get_id() {
 116          return $this->id;
 117      }
 118  
 119      /**
 120       * Setter for $userid.
 121       * @param int|null $userid
 122       */
 123      public function set_userid($userid) {
 124          $this->userid = $userid;
 125      }
 126  
 127      /**
 128       * Returns default concurrency limit for this task.
 129       *
 130       * @return int default concurrency limit
 131       */
 132      protected function get_default_concurrency_limit(): int {
 133          global $CFG;
 134  
 135          if (isset($CFG->task_concurrency_limit_default)) {
 136              return (int) $CFG->task_concurrency_limit_default;
 137          }
 138          return 0;
 139      }
 140  
 141      /**
 142       * Returns effective concurrency limit for this task.
 143       *
 144       * @return int effective concurrency limit for this task
 145       */
 146      final public function get_concurrency_limit(): int {
 147          global $CFG;
 148  
 149          $classname = get_class($this);
 150  
 151          if (isset($CFG->task_concurrency_limit[$classname])) {
 152              return (int) $CFG->task_concurrency_limit[$classname];
 153          }
 154          return $this->get_default_concurrency_limit();
 155      }
 156  
 157      /**
 158       * Sets concurrency task lock.
 159       *
 160       * @param   \core\lock\lock $lock concurrency lock to be set
 161       */
 162      final public function set_concurrency_lock(\core\lock\lock $lock): void {
 163          $this->concurrencylock = $lock;
 164      }
 165  
 166      /**
 167       * Release the concurrency lock for this task type.
 168       */
 169      final public function release_concurrency_lock(): void {
 170          if ($this->concurrencylock) {
 171              $this->concurrencylock->release();
 172          }
 173      }
 174  }