MDL-81000 core: Update "attemptsavailable" value and remove "MAX_RETRY"
[moodle.git] / lib / classes / task / adhoc_task.php
blobd537dd9f76e440c1fef17561939b6154c22c87f1
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.
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/>.
17 /**
18 * Adhoc task abstract class.
20 * All background tasks should extend this class.
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
28 namespace core\task;
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
35 abstract class adhoc_task extends task_base {
37 /** @var string $customdata - Custom data required for when this task is executed. */
38 private $customdata = '';
40 /** @var integer|null $id - Adhoc tasks each have their own database record id. */
41 private $id = null;
43 /** @var integer|null $userid - Adhoc tasks may choose to run as a specific user. */
44 private $userid = null;
46 /** @var \core\lock\lock The concurrency task lock for this task. */
47 private $concurrencylock = null;
49 /** @var int $attemptsavailable - The remaining attempts of the task. */
50 private $attemptsavailable = 12;
52 /**
53 * Provide default implementation of the task name for backward compatibility. Extending classes are expected to implement
54 * this method to provide a descriptive name for the task (shown to admins)
56 * @return string
58 public function get_name() {
59 $classparts = explode('\\', get_called_class());
60 $classname = end($classparts);
62 // Try to make human readable, capitalized and with spaces.
63 return ucfirst(str_replace('_', ' ', $classname));
66 /**
67 * Setter for $id.
68 * @param int|null $id
70 public function set_id($id) {
71 $this->id = $id;
74 /**
75 * Getter for $userid.
76 * @return int|null $userid
78 public function get_userid() {
79 return $this->userid;
82 /**
83 * Setter for $customdata.
84 * @param mixed $customdata (anything that can be handled by json_encode)
86 public function set_custom_data($customdata) {
87 $this->customdata = json_encode($customdata);
90 /**
91 * Alternate setter for $customdata. Expects the data as a json_encoded string.
92 * @param string $customdata json_encoded string
94 public function set_custom_data_as_string($customdata) {
95 $this->customdata = $customdata;
98 /**
99 * Getter for $customdata.
100 * @return mixed (anything that can be handled by json_decode).
102 public function get_custom_data() {
103 return json_decode($this->customdata);
107 * Alternate getter for $customdata.
108 * @return string this is the raw json encoded version.
110 public function get_custom_data_as_string() {
111 return $this->customdata;
115 * Getter for $id.
116 * @return int|null $id
118 public function get_id() {
119 return $this->id;
123 * Setter for $userid.
124 * @param int|null $userid
126 public function set_userid($userid) {
127 $this->userid = $userid;
131 * Returns default concurrency limit for this task.
133 * @return int default concurrency limit
135 protected function get_default_concurrency_limit(): int {
136 global $CFG;
138 if (isset($CFG->task_concurrency_limit_default)) {
139 return (int) $CFG->task_concurrency_limit_default;
141 return 0;
145 * Returns effective concurrency limit for this task.
147 * @return int effective concurrency limit for this task
149 final public function get_concurrency_limit(): int {
150 global $CFG;
152 $classname = get_class($this);
154 if (isset($CFG->task_concurrency_limit[$classname])) {
155 return (int) $CFG->task_concurrency_limit[$classname];
157 return $this->get_default_concurrency_limit();
161 * Sets concurrency task lock.
163 * @param \core\lock\lock $lock concurrency lock to be set
165 final public function set_concurrency_lock(\core\lock\lock $lock): void {
166 $this->concurrencylock = $lock;
170 * Release the concurrency lock for this task type.
172 final public function release_concurrency_lock(): void {
173 if ($this->concurrencylock) {
174 $this->concurrencylock->release();
179 * Set the remaining attempts of the task.
181 * @param int $attemptsavailable Number of the remaining attempts of the task.
183 public function set_attempts_available(int $attemptsavailable): void {
184 $this->attemptsavailable = $attemptsavailable;
188 * Get the remaining attempts of the task.
190 * @return int Number of the remaining attempts of the task.
192 public function get_attempts_available(): int {
193 return $this->attemptsavailable;
197 * Used to indicate if the task should be re-run if it fails.
198 * By default, tasks will be retried until they succeed, other tasks can override this method to change this behaviour.
200 * @return bool true if the task should be retried until it succeeds, false otherwise.
202 public function retry_until_success(): bool {
203 return true;