Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
use core\check\result;
30
 
31
/**
32
 * Abstract class for common properties of scheduled_task and adhoc_task.
33
 *
34
 * @copyright  2013 Damyon Wiese
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
abstract class task_base {
38
 
39
    /** @var \core\lock\lock $lock - The lock controlling this task. */
40
    private $lock = null;
41
 
42
    /** @var \core\lock\lock $cronlock - The lock controlling the entire cron process. */
43
    private $cronlock = null;
44
 
45
    /** @var string $component - The component this task belongs to. */
46
    private $component = '';
47
 
48
    /** @var int $faildelay - Exponentially increasing fail delay */
49
    private $faildelay = 0;
50
 
51
    /** @var int $nextruntime - When this task is due to run next */
52
    private $nextruntime = 0;
53
 
54
    /** @var int $timestarted - When this task was started */
55
    private $timestarted = null;
56
 
57
    /** @var string $hostname - Hostname where this task was started and PHP process ID */
58
    private $hostname = null;
59
 
60
    /** @var int $pid - PHP process ID that is running the task */
61
    private $pid = null;
62
 
63
    /**
64
     * Get a descriptive name for the task (shown to admins)
65
     *
66
     * @return string
67
     */
68
    abstract public function get_name();
69
 
70
    /**
71
     * Set the current lock for this task.
72
     * @param \core\lock\lock $lock
73
     */
74
    public function set_lock(\core\lock\lock $lock) {
75
        $this->lock = $lock;
76
    }
77
 
78
    /**
79
     * Set the current lock for the entire cron process.
80
     * @param \core\lock\lock $lock
81
     */
82
    public function set_cron_lock(\core\lock\lock $lock) {
83
        $this->cronlock = $lock;
84
    }
85
 
86
    /**
87
     * Get the current lock for this task.
88
     * @return \core\lock\lock
89
     */
90
    public function get_lock() {
91
        return $this->lock;
92
    }
93
 
94
    /**
95
     * Get the next run time for this task.
96
     * @return int timestamp
97
     */
98
    public function get_next_run_time() {
99
        return $this->nextruntime;
100
    }
101
 
102
    /**
103
     * Set the next run time for this task.
104
     * @param int $nextruntime
105
     */
106
    public function set_next_run_time($nextruntime) {
107
        $this->nextruntime = $nextruntime;
108
    }
109
 
110
    /**
111
     * Get the current lock for the entire cron.
112
     * @return \core\lock\lock
113
     */
114
    public function get_cron_lock() {
115
        return $this->cronlock;
116
    }
117
 
118
    /**
119
     * Setter for $blocking.
120
     *
121
     * Please note that task blocking is no longer supported.
122
     * If you are using it in older versions of Moodle you are strongly advised to rewrite your code
123
     * as has a detrimental impact upon performance.
124
     *
125
     * @deprecated since Moodle 4.4 See MDL-67667
126
     * @todo Remove in MDL-81509
127
     */
128
    #[\core\attribute\deprecated(
129
        replacement: null,
130
        since: '4.4',
131
        reason: 'Blocking tasks are no longer supported',
132
    )]
133
    public function set_blocking($blocking) {
134
        \core\deprecation::emit_deprecation_if_present([$this, __FUNCTION__]);
135
    }
136
 
137
    /**
138
     * Getter for $blocking.
139
     *
140
     * @return bool
141
     * @deprecated since Moodle 4.4 See MDL-67667
142
     * @todo Remove in MDL-81509
143
     */
144
    #[\core\attribute\deprecated(
145
        replacement: null,
146
        since: '4.4',
147
        reason: 'Blocking tasks are no longer supported',
148
    )]
149
    public function is_blocking() {
150
        \core\deprecation::emit_deprecation_if_present([$this, __FUNCTION__]);
151
        return false;
152
    }
153
 
154
    /**
155
     * Setter for $component.
156
     * @param string $component
157
     */
158
    public function set_component($component) {
159
        $this->component = $component;
160
    }
161
 
162
    /**
163
     * Getter for $component.
164
     * @return string
165
     */
166
    public function get_component() {
167
        if (empty($this->component)) {
168
            // The component should be the root of the class namespace.
169
            $classname = get_class($this);
170
            $parts = explode('\\', $classname);
171
 
172
            if (count($parts) === 1) {
173
                $component = substr($classname, 0, strpos($classname, '_task'));
174
            } else {
175
                [$component] = $parts;
176
            }
177
 
178
            // Load component information from plugin manager.
179
            if ($component !== 'core' && strpos($component, 'core_') !== 0) {
180
                $plugininfo = \core_plugin_manager::instance()->get_plugin_info($component);
181
                if ($plugininfo && $plugininfo->component) {
182
                    $this->set_component($plugininfo->component);
183
                } else {
184
                    debugging("Component not set and the class namespace does not match a valid component ({$component}).");
185
                }
186
            }
187
        }
188
 
189
        return $this->component;
190
    }
191
 
192
    /**
193
     * Setter for $faildelay.
194
     * @param int $faildelay
195
     */
196
    public function set_fail_delay($faildelay) {
197
        $this->faildelay = $faildelay;
198
    }
199
 
200
    /**
201
     * Getter for $faildelay.
202
     * @return int
203
     */
204
    public function get_fail_delay() {
205
        return $this->faildelay;
206
    }
207
 
208
    /**
209
     * Do the job.
210
     * Throw exceptions on errors (the job will be retried).
211
     */
212
    abstract public function execute();
213
 
214
    /**
215
     * Setter for $timestarted.
216
     * @param int $timestarted
217
     */
218
    public function set_timestarted($timestarted = null) {
219
        $this->timestarted = $timestarted;
220
    }
221
 
222
    /**
223
     * Getter for $timestarted.
224
     * @return int
225
     */
226
    public function get_timestarted() {
227
        return $this->timestarted;
228
    }
229
 
230
    /**
231
     * Setter for $hostname.
232
     * @param string $hostname
233
     */
234
    public function set_hostname($hostname = null) {
235
        $this->hostname = $hostname;
236
    }
237
 
238
    /**
239
     * Getter for $hostname.
240
     * @return string
241
     */
242
    public function get_hostname() {
243
        return $this->hostname;
244
    }
245
 
246
    /**
247
     * Setter for $pid.
248
     * @param int $pid
249
     */
250
    public function set_pid($pid = null) {
251
        $this->pid = $pid;
252
    }
253
 
254
    /**
255
     * Getter for $pid.
256
     * @return int
257
     */
258
    public function get_pid() {
259
        return $this->pid;
260
    }
261
 
262
    /**
263
     * Informs whether the task's component is enabled.
264
     * @return bool true when enabled. false otherwise.
265
     */
266
    public function is_component_enabled(): bool {
267
        $component = $this->get_component();
268
 
269
        // An entire core component type cannot be explicitly disabled.
270
        [$componenttype] = core_component::normalize_component($component);
271
        if ($componenttype === 'core') {
272
            return true;
273
        } else {
274
            $plugininfo = core_plugin_manager::instance()->get_plugin_info($component);
275
            return $plugininfo && ($plugininfo->is_enabled() !== false);
276
        }
277
    }
278
 
279
    /**
280
     * Returns task runtime
281
     * @return int
282
     */
283
    public function get_runtime() {
284
        return time() - $this->timestarted;
285
    }
286
 
287
    /**
288
     * Returns if the task has been running for too long
289
     * @return result
290
     */
291
    public function get_runtime_result() {
292
        global $CFG;
293
        $runtime = $this->get_runtime();
294
        $runtimeerror = $CFG->taskruntimeerror;
295
        $runtimewarn = $CFG->taskruntimewarn;
296
 
297
        $status = result::OK;
298
        $details = '';
299
 
300
        if ($runtime > $runtimewarn) {
301
            $status = result::WARNING;
302
            $details = get_string('slowtask', 'tool_task', format_time($runtimewarn));
303
        }
304
 
305
        if ($runtime > $runtimeerror) {
306
            $status = result::ERROR;
307
            $details = get_string('slowtask', 'tool_task', format_time($runtimeerror));
308
        }
309
 
310
        // This result is aggregated with other running tasks checks before display.
311
        return new result($status, '', $details);
312
    }
313
 
314
}