forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduled_task.php
414 lines (361 loc) · 11.9 KB
/
scheduled_task.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Scheduled task abstract class.
*
* @package core
* @category task
* @copyright 2013 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\task;
/**
* Abstract class defining a scheduled task.
* @copyright 2013 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class scheduled_task extends task_base {
/** Minimum minute value. */
const MINUTEMIN = 0;
/** Maximum minute value. */
const MINUTEMAX = 59;
/** Minimum hour value. */
const HOURMIN = 0;
/** Maximum hour value. */
const HOURMAX = 23;
/** Minimum dayofweek value. */
const DAYOFWEEKMIN = 0;
/** Maximum dayofweek value. */
const DAYOFWEEKMAX = 6;
/** @var string $hour - Pattern to work out the valid hours */
private $hour = '*';
/** @var string $minute - Pattern to work out the valid minutes */
private $minute = '*';
/** @var string $day - Pattern to work out the valid days */
private $day = '*';
/** @var string $month - Pattern to work out the valid months */
private $month = '*';
/** @var string $dayofweek - Pattern to work out the valid dayofweek */
private $dayofweek = '*';
/** @var int $lastruntime - When this task was last run */
private $lastruntime = 0;
/** @var boolean $customised - Has this task been changed from it's default schedule? */
private $customised = false;
/** @var int $disabled - Is this task disabled in cron? */
private $disabled = false;
/**
* Get the last run time for this scheduled task.
* @return int
*/
public function get_last_run_time() {
return $this->lastruntime;
}
/**
* Set the last run time for this scheduled task.
* @param int $lastruntime
*/
public function set_last_run_time($lastruntime) {
$this->lastruntime = $lastruntime;
}
/**
* Has this task been changed from it's default config?
* @return bool
*/
public function is_customised() {
return $this->customised;
}
/**
* Has this task been changed from it's default config?
* @param bool
*/
public function set_customised($customised) {
$this->customised = $customised;
}
/**
* Setter for $minute. Accepts a special 'R' value
* which will be translated to a random minute.
* @param string $minute
*/
public function set_minute($minute) {
if ($minute === 'R') {
$minute = mt_rand(self::HOURMIN, self::HOURMAX);
}
$this->minute = $minute;
}
/**
* Getter for $minute.
* @return string
*/
public function get_minute() {
return $this->minute;
}
/**
* Setter for $hour. Accepts a special 'R' value
* which will be translated to a random hour.
* @param string $hour
*/
public function set_hour($hour) {
if ($hour === 'R') {
$hour = mt_rand(self::HOURMIN, self::HOURMAX);
}
$this->hour = $hour;
}
/**
* Getter for $hour.
* @return string
*/
public function get_hour() {
return $this->hour;
}
/**
* Setter for $month.
* @param string $month
*/
public function set_month($month) {
$this->month = $month;
}
/**
* Getter for $month.
* @return string
*/
public function get_month() {
return $this->month;
}
/**
* Setter for $day.
* @param string $day
*/
public function set_day($day) {
$this->day = $day;
}
/**
* Getter for $day.
* @return string
*/
public function get_day() {
return $this->day;
}
/**
* Setter for $dayofweek.
* @param string $dayofweek
*/
public function set_day_of_week($dayofweek) {
if ($dayofweek === 'R') {
$dayofweek = mt_rand(self::DAYOFWEEKMIN, self::DAYOFWEEKMAX);
}
$this->dayofweek = $dayofweek;
}
/**
* Getter for $dayofweek.
* @return string
*/
public function get_day_of_week() {
return $this->dayofweek;
}
/**
* Setter for $disabled.
* @param bool $disabled
*/
public function set_disabled($disabled) {
$this->disabled = (bool)$disabled;
}
/**
* Getter for $disabled.
* @return bool
*/
public function get_disabled() {
return $this->disabled;
}
/**
* Override this function if you want this scheduled task to run, even if the component is disabled.
*
* @return bool
*/
public function get_run_if_component_disabled() {
return false;
}
/**
* Take a cron field definition and return an array of valid numbers with the range min-max.
*
* @param string $field - The field definition.
* @param int $min - The minimum allowable value.
* @param int $max - The maximum allowable value.
* @return array(int)
*/
public function eval_cron_field($field, $min, $max) {
// Cleanse the input.
$field = trim($field);
// Format for a field is:
// <fieldlist> := <range>(/<step>)(,<fieldlist>)
// <step> := int
// <range> := <any>|<int>|<min-max>
// <any> := *
// <min-max> := int-int
// End of format BNF.
// This function is complicated but is covered by unit tests.
$range = array();
$matches = array();
preg_match_all('@[0-9]+|\*|,|/|-@', $field, $matches);
$last = 0;
$inrange = false;
$instep = false;
foreach ($matches[0] as $match) {
if ($match == '*') {
array_push($range, range($min, $max));
} else if ($match == '/') {
$instep = true;
} else if ($match == '-') {
$inrange = true;
} else if (is_numeric($match)) {
if ($instep) {
$i = 0;
for ($i = 0; $i < count($range[count($range) - 1]); $i++) {
if (($i) % $match != 0) {
$range[count($range) - 1][$i] = -1;
}
}
$inrange = false;
} else if ($inrange) {
if (count($range)) {
$range[count($range) - 1] = range($last, $match);
}
$inrange = false;
} else {
if ($match >= $min && $match <= $max) {
array_push($range, $match);
}
$last = $match;
}
}
}
// Flatten the result.
$result = array();
foreach ($range as $r) {
if (is_array($r)) {
foreach ($r as $rr) {
if ($rr >= $min && $rr <= $max) {
$result[$rr] = 1;
}
}
} else if (is_numeric($r)) {
if ($r >= $min && $r <= $max) {
$result[$r] = 1;
}
}
}
$result = array_keys($result);
sort($result, SORT_NUMERIC);
return $result;
}
/**
* Assuming $list is an ordered list of items, this function returns the item
* in the list that is greater than or equal to the current value (or 0). If
* no value is greater than or equal, this will return the first valid item in the list.
* If list is empty, this function will return 0.
*
* @param int $current The current value
* @param int[] $list The list of valid items.
* @return int $next.
*/
private function next_in_list($current, $list) {
foreach ($list as $l) {
if ($l >= $current) {
return $l;
}
}
if (count($list)) {
return $list[0];
}
return 0;
}
/**
* Calculate when this task should next be run based on the schedule.
* @return int $nextruntime.
*/
public function get_next_scheduled_time() {
global $CFG;
$validminutes = $this->eval_cron_field($this->minute, self::MINUTEMIN, self::MINUTEMAX);
$validhours = $this->eval_cron_field($this->hour, self::HOURMIN, self::HOURMAX);
// We need to change to the server timezone before using php date() functions.
\core_date::set_default_server_timezone();
$daysinmonth = date("t");
$validdays = $this->eval_cron_field($this->day, 1, $daysinmonth);
$validdaysofweek = $this->eval_cron_field($this->dayofweek, 0, 7);
$validmonths = $this->eval_cron_field($this->month, 1, 12);
$nextvalidyear = date('Y');
$currentminute = date("i") + 1;
$currenthour = date("H");
$currentday = date("j");
$currentmonth = date("n");
$currentdayofweek = date("w");
$nextvalidminute = $this->next_in_list($currentminute, $validminutes);
if ($nextvalidminute < $currentminute) {
$currenthour += 1;
}
$nextvalidhour = $this->next_in_list($currenthour, $validhours);
if ($nextvalidhour < $currenthour) {
$currentdayofweek += 1;
$currentday += 1;
}
$nextvaliddayofmonth = $this->next_in_list($currentday, $validdays);
$nextvaliddayofweek = $this->next_in_list($currentdayofweek, $validdaysofweek);
$daysincrementbymonth = $nextvaliddayofmonth - $currentday;
if ($nextvaliddayofmonth < $currentday) {
$daysincrementbymonth += $daysinmonth;
}
$daysincrementbyweek = $nextvaliddayofweek - $currentdayofweek;
if ($nextvaliddayofweek < $currentdayofweek) {
$daysincrementbyweek += 7;
}
// Special handling for dayofmonth vs dayofweek:
// if either field is * - use the other field
// otherwise - choose the soonest (see man 5 cron).
if ($this->dayofweek == '*') {
$daysincrement = $daysincrementbymonth;
} else if ($this->day == '*') {
$daysincrement = $daysincrementbyweek;
} else {
// Take the smaller increment of days by month or week.
$daysincrement = $daysincrementbymonth;
if ($daysincrementbyweek < $daysincrementbymonth) {
$daysincrement = $daysincrementbyweek;
}
}
$nextvaliddayofmonth = $currentday + $daysincrement;
if ($nextvaliddayofmonth > $daysinmonth) {
$currentmonth += 1;
$nextvaliddayofmonth -= $daysinmonth;
}
$nextvalidmonth = $this->next_in_list($currentmonth, $validmonths);
if ($nextvalidmonth < $currentmonth) {
$nextvalidyear += 1;
}
// Work out the next valid time.
$nexttime = mktime($nextvalidhour,
$nextvalidminute,
0,
$nextvalidmonth,
$nextvaliddayofmonth,
$nextvalidyear);
return $nexttime;
}
/**
* Get a descriptive name for this task (shown to admins).
*
* @return string
*/
public abstract function get_name();
}