Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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   * 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       * Setter for $id.
  51       * @param int|null $id
  52       */
  53      public function set_id($id) {
  54          $this->id = $id;
  55      }
  56  
  57      /**
  58       * Getter for $userid.
  59       * @return int|null $userid
  60       */
  61      public function get_userid() {
  62          return $this->userid;
  63      }
  64  
  65      /**
  66       * Setter for $customdata.
  67       * @param mixed $customdata (anything that can be handled by json_encode)
  68       */
  69      public function set_custom_data($customdata) {
  70          $this->customdata = json_encode($customdata);
  71      }
  72  
  73      /**
  74       * Alternate setter for $customdata. Expects the data as a json_encoded string.
  75       * @param string $customdata json_encoded string
  76       */
  77      public function set_custom_data_as_string($customdata) {
  78          $this->customdata = $customdata;
  79      }
  80  
  81      /**
  82       * Getter for $customdata.
  83       * @return mixed (anything that can be handled by json_decode).
  84       */
  85      public function get_custom_data() {
  86          return json_decode($this->customdata);
  87      }
  88  
  89      /**
  90       * Alternate getter for $customdata.
  91       * @return string this is the raw json encoded version.
  92       */
  93      public function get_custom_data_as_string() {
  94          return $this->customdata;
  95      }
  96  
  97      /**
  98       * Getter for $id.
  99       * @return int|null $id
 100       */
 101      public function get_id() {
 102          return $this->id;
 103      }
 104  
 105      /**
 106       * Setter for $userid.
 107       * @param int|null $userid
 108       */
 109      public function set_userid($userid) {
 110          $this->userid = $userid;
 111      }
 112  
 113      /**
 114       * Returns default concurrency limit for this task.
 115       *
 116       * @return int default concurrency limit
 117       */
 118      protected function get_default_concurrency_limit(): int {
 119          global $CFG;
 120  
 121          if (isset($CFG->task_concurrency_limit_default)) {
 122              return (int) $CFG->task_concurrency_limit_default;
 123          }
 124          return 0;
 125      }
 126  
 127      /**
 128       * Returns effective concurrency limit for this task.
 129       *
 130       * @return int effective concurrency limit for this task
 131       */
 132      final public function get_concurrency_limit(): int {
 133          global $CFG;
 134  
 135          $classname = get_class($this);
 136  
 137          if (isset($CFG->task_concurrency_limit[$classname])) {
 138              return (int) $CFG->task_concurrency_limit[$classname];
 139          }
 140          return $this->get_default_concurrency_limit();
 141      }
 142  
 143      /**
 144       * Sets concurrency task lock.
 145       *
 146       * @param   \core\lock\lock $lock concurrency lock to be set
 147       */
 148      final public function set_concurrency_lock(\core\lock\lock $lock): void {
 149          $this->concurrencylock = $lock;
 150      }
 151  
 152      /**
 153       * Release the concurrency lock for this task type.
 154       */
 155      final public function release_concurrency_lock(): void {
 156          if ($this->concurrencylock) {
 157              $this->concurrencylock->release();
 158          }
 159      }
 160  }