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 * Scheduled task abstract class.
22 * @copyright 2013 Damyon Wiese
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 * Abstract class defining a scheduled task.
29 * @copyright 2013 Damyon Wiese
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 abstract class scheduled_task extends task_base {
34 /** Minimum minute value. */
36 /** Maximum minute value. */
38 /** Minimum hour value. */
40 /** Maximum hour value. */
43 /** @var string $hour - Pattern to work out the valid hours */
46 /** @var string $minute - Pattern to work out the valid minutes */
47 private $minute = '*';
49 /** @var string $day - Pattern to work out the valid days */
52 /** @var string $month - Pattern to work out the valid months */
55 /** @var string $dayofweek - Pattern to work out the valid dayofweek */
56 private $dayofweek = '*';
58 /** @var int $lastruntime - When this task was last run */
59 private $lastruntime = 0;
61 /** @var boolean $customised - Has this task been changed from it's default schedule? */
62 private $customised = false;
64 /** @var int $disabled - Is this task disabled in cron? */
65 private $disabled = false;
68 * Get the last run time for this scheduled task.
71 public function get_last_run_time() {
72 return $this->lastruntime;
76 * Set the last run time for this scheduled task.
77 * @param int $lastruntime
79 public function set_last_run_time($lastruntime) {
80 $this->lastruntime = $lastruntime;
84 * Has this task been changed from it's default config?
87 public function is_customised() {
88 return $this->customised;
92 * Has this task been changed from it's default config?
95 public function set_customised($customised) {
96 $this->customised = $customised;
100 * Setter for $minute. Accepts a special 'R' value
101 * which will be translated to a random minute.
102 * @param string $minute
104 public function set_minute($minute) {
105 if ($minute === 'R') {
106 $minute = mt_rand(self::HOURMIN, self::HOURMAX);
108 $this->minute = $minute;
112 * Getter for $minute.
115 public function get_minute() {
116 return $this->minute;
120 * Setter for $hour. Accepts a special 'R' value
121 * which will be translated to a random hour.
122 * @param string $hour
124 public function set_hour($hour) {
126 $hour = mt_rand(self::HOURMIN, self::HOURMAX);
135 public function get_hour() {
141 * @param string $month
143 public function set_month($month) {
144 $this->month = $month;
151 public function get_month() {
159 public function set_day($day) {
167 public function get_day() {
172 * Setter for $dayofweek.
173 * @param string $dayofweek
175 public function set_day_of_week($dayofweek) {
176 $this->dayofweek = $dayofweek;
180 * Getter for $dayofweek.
183 public function get_day_of_week() {
184 return $this->dayofweek;
188 * Setter for $disabled.
189 * @param bool $disabled
191 public function set_disabled($disabled) {
192 $this->disabled = (bool)$disabled;
196 * Getter for $disabled.
199 public function get_disabled() {
200 return $this->disabled;
204 * Override this function if you want this scheduled task to run, even if the component is disabled.
208 public function get_run_if_component_disabled() {
213 * Take a cron field definition and return an array of valid numbers with the range min-max.
215 * @param string $field - The field definition.
216 * @param int $min - The minimum allowable value.
217 * @param int $max - The maximum allowable value.
220 public function eval_cron_field($field, $min, $max) {
221 // Cleanse the input.
222 $field = trim($field);
224 // Format for a field is:
225 // <fieldlist> := <range>(/<step>)(,<fieldlist>)
227 // <range> := <any>|<int>|<min-max>
229 // <min-max> := int-int
230 // End of format BNF.
232 // This function is complicated but is covered by unit tests.
236 preg_match_all('@[0-9]+|\*|,|/|-@', $field, $matches);
242 foreach ($matches[0] as $match) {
244 array_push($range, range($min, $max));
245 } else if ($match == '/') {
247 } else if ($match == '-') {
249 } else if (is_numeric($match)) {
252 for ($i = 0; $i < count($range[count($range) - 1]); $i++) {
253 if (($i) % $match != 0) {
254 $range[count($range) - 1][$i] = -1;
258 } else if ($inrange) {
260 $range[count($range) - 1] = range($last, $match);
264 if ($match >= $min && $match <= $max) {
265 array_push($range, $match);
272 // Flatten the result.
274 foreach ($range as $r) {
276 foreach ($r as $rr) {
277 if ($rr >= $min && $rr <= $max) {
281 } else if (is_numeric($r)) {
282 if ($r >= $min && $r <= $max) {
287 $result = array_keys($result);
288 sort($result, SORT_NUMERIC);
293 * Assuming $list is an ordered list of items, this function returns the item
294 * in the list that is greater than or equal to the current value (or 0). If
295 * no value is greater than or equal, this will return the first valid item in the list.
296 * If list is empty, this function will return 0.
298 * @param int $current The current value
299 * @param int[] $list The list of valid items.
302 private function next_in_list($current, $list) {
303 foreach ($list as $l) {
304 if ($l >= $current) {
316 * Calculate when this task should next be run based on the schedule.
317 * @return int $nextruntime.
319 public function get_next_scheduled_time() {
322 $validminutes = $this->eval_cron_field($this->minute, self::MINUTEMIN, self::MINUTEMAX);
323 $validhours = $this->eval_cron_field($this->hour, self::HOURMIN, self::HOURMAX);
325 // We need to change to the server timezone before using php date() functions.
326 $origtz = date_default_timezone_get();
327 if (!empty($CFG->timezone) && $CFG->timezone != 99) {
328 date_default_timezone_set($CFG->timezone);
331 $daysinmonth = date("t");
332 $validdays = $this->eval_cron_field($this->day, 1, $daysinmonth);
333 $validdaysofweek = $this->eval_cron_field($this->dayofweek, 0, 7);
334 $validmonths = $this->eval_cron_field($this->month, 1, 12);
335 $nextvalidyear = date('Y');
337 $currentminute = date("i") + 1;
338 $currenthour = date("H");
339 $currentday = date("j");
340 $currentmonth = date("n");
341 $currentdayofweek = date("w");
343 $nextvalidminute = $this->next_in_list($currentminute, $validminutes);
344 if ($nextvalidminute < $currentminute) {
347 $nextvalidhour = $this->next_in_list($currenthour, $validhours);
348 if ($nextvalidhour < $currenthour) {
349 $currentdayofweek += 1;
352 $nextvaliddayofmonth = $this->next_in_list($currentday, $validdays);
353 $nextvaliddayofweek = $this->next_in_list($currentdayofweek, $validdaysofweek);
354 $daysincrementbymonth = $nextvaliddayofmonth - $currentday;
355 if ($nextvaliddayofmonth < $currentday) {
356 $daysincrementbymonth += $daysinmonth;
359 $daysincrementbyweek = $nextvaliddayofweek - $currentdayofweek;
360 if ($nextvaliddayofweek < $currentdayofweek) {
361 $daysincrementbyweek += 7;
364 // Special handling for dayofmonth vs dayofweek:
365 // if either field is * - use the other field
366 // otherwise - choose the soonest (see man 5 cron).
367 if ($this->dayofweek == '*') {
368 $daysincrement = $daysincrementbymonth;
369 } else if ($this->day == '*') {
370 $daysincrement = $daysincrementbyweek;
372 // Take the smaller increment of days by month or week.
373 $daysincrement = $daysincrementbymonth;
374 if ($daysincrementbyweek < $daysincrementbymonth) {
375 $daysincrement = $daysincrementbyweek;
379 $nextvaliddayofmonth = $currentday + $daysincrement;
380 if ($nextvaliddayofmonth > $daysinmonth) {
382 $nextvaliddayofmonth -= $daysinmonth;
385 $nextvalidmonth = $this->next_in_list($currentmonth, $validmonths);
386 if ($nextvalidmonth < $currentmonth) {
390 // Work out the next valid time.
391 $nexttime = mktime($nextvalidhour,
395 $nextvaliddayofmonth,
398 // We need to change the timezone back so other date functions in moodle do not get confused.
399 if (!empty($CFG->timezone) && $CFG->timezone != 99) {
400 date_default_timezone_set($origtz);
407 * Get a descriptive name for this task (shown to admins).
411 public abstract function get_name();