|
| 1 | +--- |
| 2 | +title: db/tasks.php |
| 3 | +tags: |
| 4 | + - Plugins |
| 5 | + - Common files |
| 6 | + - Scheduled tasks |
| 7 | +description: A description of the plugin scheduled task configuration file |
| 8 | +--- |
| 9 | + |
| 10 | +import { LanguageProperty, Since } from '@site/src/components'; |
| 11 | + |
| 12 | +If a plugin wants to configure scheduled task, two items are required: |
| 13 | + |
| 14 | +- a class extending the `\core\task\scheduled_task` class; and |
| 15 | +- the `db/tasks.php` file containing its initial configuration. |
| 16 | + |
| 17 | +The general format of the file is as follows: |
| 18 | + |
| 19 | +```php |
| 20 | +$tasks = [ |
| 21 | + // First task configuration. |
| 22 | + [ ... ], |
| 23 | + |
| 24 | + // Second task configuration. |
| 25 | + [ ... ], |
| 26 | +]; |
| 27 | +``` |
| 28 | + |
| 29 | +Each task configuration entry has a number of possible properties, described below. |
| 30 | + |
| 31 | +## Task configuration entries |
| 32 | + |
| 33 | +### Classname |
| 34 | + |
| 35 | +<LanguageProperty |
| 36 | + required |
| 37 | + types={["string"]} |
| 38 | +/> |
| 39 | + |
| 40 | +The `classname` contains the fully-qualified class name where the scheduled task is located. |
| 41 | + |
| 42 | +```php |
| 43 | +$tasks = [ |
| 44 | + [ |
| 45 | + 'classname' => 'mod_example\task\do_something', |
| 46 | + // ... |
| 47 | + ] |
| 48 | +] |
| 49 | +``` |
| 50 | + |
| 51 | +### Blocking |
| 52 | + |
| 53 | +<LanguageProperty |
| 54 | + types={["integer"]} |
| 55 | +/> |
| 56 | + |
| 57 | +Tasks can be configured to block the execution of all other tasks by setting the `blocking` property to a truthy value. |
| 58 | + |
| 59 | +:::caution |
| 60 | + |
| 61 | +Whilst this feature is available its use is _strongly_ discouraged and *will not* be accepted in Moodle core. |
| 62 | + |
| 63 | +::: |
| 64 | + |
| 65 | +```php |
| 66 | +$tasks = [ |
| 67 | + [ |
| 68 | + 'classname' => 'mod_example\task\do_something', |
| 69 | + 'blocking' => 1, |
| 70 | + // ... |
| 71 | + ], |
| 72 | +]; |
| 73 | +``` |
| 74 | + |
| 75 | +### Date and time fields |
| 76 | + |
| 77 | +<LanguageProperty |
| 78 | + types={["string"]} |
| 79 | +/> |
| 80 | + |
| 81 | +The following date and time fields are available: |
| 82 | + |
| 83 | +- month |
| 84 | +- day |
| 85 | +- dayofweek |
| 86 | +- hour |
| 87 | +- month |
| 88 | + |
| 89 | +Each of these fields accepts one, or more values, and the format for each field is described as: |
| 90 | + |
| 91 | +``` |
| 92 | +<fieldlist> := <range>(/<step>)(,<fieldlist>) |
| 93 | +<step> := int |
| 94 | +<range> := <any>|<int>|<min-max>|<random> |
| 95 | +<any> := * |
| 96 | +<min-max> := int-int |
| 97 | +<random> := R |
| 98 | +``` |
| 99 | + |
| 100 | +:::info Random values |
| 101 | + |
| 102 | +A fixed random value can be selected by using a value of `R`. By specifying this option, a random day or time is chosen when the task is installed or updated. The same value will be used each time the task is scheduled. |
| 103 | + |
| 104 | +::: |
| 105 | + |
| 106 | +If no value is specified then the following defaults are used: |
| 107 | + |
| 108 | +- Month: `*` (Every month) |
| 109 | +- Day: `*` (Every day) |
| 110 | +- Day of the week: `*` (Every day of the week) |
| 111 | +- Hour: `*` (Every hour) |
| 112 | +- Minute: `*` (Every minute) |
| 113 | + |
| 114 | +:::info Day and Day of the week |
| 115 | + |
| 116 | +If either field is set to `*` then use the other field, otherwise the soonest value is used. |
| 117 | + |
| 118 | +::: |
| 119 | + |
| 120 | +#### Examples |
| 121 | + |
| 122 | +```php title="Run at a fixed time each day, randomised during installation of the task" |
| 123 | +$tasks = [ |
| 124 | + [ |
| 125 | + 'classname' => 'mod_example\task\do_something', |
| 126 | + |
| 127 | + // Every month. |
| 128 | + 'month' => '*', |
| 129 | + // Every day. |
| 130 | + 'day' => '*', |
| 131 | + |
| 132 | + // A fixed random hour and minute. |
| 133 | + 'hour' => 'R', |
| 134 | + 'month' => 'R', |
| 135 | + ], |
| 136 | +]; |
| 137 | +``` |
| 138 | + |
| 139 | +```php title="Specifying multiple times in an hour" |
| 140 | +$tasks = [ |
| 141 | + [ |
| 142 | + 'classname' => 'mod_example\task\do_something', |
| 143 | + |
| 144 | + // At two intervals in the hour. |
| 145 | + 'minute' => '5, 35', |
| 146 | + ], |
| 147 | +]; |
| 148 | +``` |
0 commit comments