Timer official documentation
0. Description of timing triggers
- Function: let the cloud function trigger at the specified time/specified period according to certain rules
- Effect: Equivalent to calling the cloud function on the applet side using the callFunction method
- Note: parameters cannot be passed in the trigger configuration; they must be written in the cloud function to be triggered
1. Create a trigger
- Create a new config.json file in the cloud function that will use the trigger.
- Configure triggers (Remove all comments) as follows
// Triggers field is an array of triggers. Currently, only one trigger is supported
"triggers": [{// name: indicates the name of the trigger
"name": "myTrigger".// type: indicates the type of the trigger. Currently, only timer is supported.
"type": "timer".// config: indicates the trigger configuration. For timed triggers, the config format is cron
"config": "0 0 2 1 * * *"}]Copy the code
-
Fields that
- Name: indicates the user-defined trigger name. The same cloud function can have multiple triggers, but not one with the same name
- Type: Only timer is supported
- Config: indicates the trigger time. See the following for details
-
Config field format (Cron expression) :
- Seven fields: second minute hour day month week year
- Each field is separated by a space
-
Values of the Cron expression field:
field | value | The wildcard |
---|---|---|
seconds | 0-59 integer | , – * / |
minutes | 0-59 integer | , – * / |
hours | An integer 0-23 | , – * / |
day | Integers from 1 to 31 (days of the month to be considered) | , – * / |
month | 1-12 of integer or JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP and OCT, NOV, DEC | , – * / |
week | 0 to 6 integers or SUN, MON, TUE, WED and THU, FRI, SAT; 0 refers to Sunday, 1 refers to Monday, and so on, and 6 refers to Saturday | , – * / |
years | An integer of 1970 ~ 2099 |
- Cron expression wildcard
The wildcard | meaning |
---|---|
, (comma) | Represents the union of characters separated by commas. For example, in the hour field, 1,2, and 3 indicate 1,2, and 3 |
– (dash) | Contains all values for the specified range. For example, in the Day field, 1-15 contains the 1st to 15th of the specified month |
* (asterisk) | Represents all values. In the Hour field, * indicates each hour |
/ (forward slash) | Specify the increment. In the Minute field, enter 1/10 to specify repeats every ten minutes from the first minute. For example, 11 minutes, 21 minutes, 31 minutes, and so on |
- Cron expression example
*/5 * * * * * * 0 0 2 1 * * * 0 15 10 * * Mon-fri * 0 0 10 14 16 * * * * 0 0 10 14 16 * * * * 0 */30 9-17 * * * * 0 0 12 * * WED * every half hour between 9 am and 5 PM every WednesdayCopy the code
2. Use triggers
- Note that triggers is the “root” in config.json. If there is a permission field in the original cloud function config.json, triggers should be the same as Permission
- Upload the entire cloud function: right-click the cloud function, upload and deploy
- Upload trigger: right-click config.json and upload trigger
3. Possible errors
- If an error message is displayed, the file is not in JSON format
- Check whether it is in json format. Find an online test tool and copy your field to check
- Check whether any comment is not deleted. No comment can be left in the JSON
- The field in Triggers is invalid if an error is reported
- Check whether triggers is written in the outermost layer and not in other fields
- Check internal Crons to see if they split each field by space. If not, copy the examples in the official documentation and adjust them to your own rules