2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Adhoc task abstract class.
20 * All background tasks should extend this class.
24 * @copyright 2013 Damyon Wiese
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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. */
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;
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)
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));
70 public function set_id($id) {
76 * @return int|null $userid
78 public function get_userid() {
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);
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;
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
;
116 * @return int|null $id
118 public function get_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 {
138 if (isset($CFG->task_concurrency_limit_default
)) {
139 return (int) $CFG->task_concurrency_limit_default
;
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 {
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 {