Timer Trigger
You can use Timer Trigger combined with cloud function to achieve the feature of scheduled tasks.
Configuring a Timer Trigger
- TCB
- Mini Program Cloud Function
- CloudBase CLI tool
Go to TCB/cloud function, select the function to be configured, click Edit, modify the Timer Trigger option in the form, and upload a configuration file or enter configuration content in the following format:
{
// The triggers field is a trigger array.
"triggers": [
{
// name: The name of the trigger. Refer to the description below for the rules.
"name": "myTrigger",
// type: Trigger type. Currently only supports timer (i.e., timer trigger)
"type": "timer",
// config: trigger configuration. Under the timer trigger, the format of config is a cron expression. Refer to the description below for the rules.
"config": "0 0 2 1 * * *"
}
]
}
Under the directory of the cloud function where triggers need to be added, create a new file named config.json with the following format:
{
// The triggers field is a trigger array. Currently, only one trigger is supported, meaning the array can contain only one entry and multiple entries cannot be added.
"triggers": [
{
// name: The name of the trigger. Refer to the description below for the rules.
"name": "myTrigger",
// type: Trigger type. Currently only supports timer (i.e., timer trigger)
"type": "timer",
// config: trigger configuration. Under the timer trigger, the format of config is a cron expression. Refer to the description below for the rules.
"config": "0 0 2 1 * * *"
}
]
}
After creation, you need to right-click the cloud function and click "Upload Trigger" to complete the trigger deployment.
Refer to the CloudBase CLI documentation.
Configuration Details
Field Rules
- Timer trigger name (name): Supports up to 60 characters, including
a-z,A-Z,0-9,-, and_. Must start with a letter. Multiple timer triggers with the same name are not supported under a single function. - Timer trigger schedule (config): Specifies when the function is triggered. Enter a custom standard Cron expression to determine when to trigger the function. For more information about Cron expressions, see the following.
Cron Expression
Cron expressions have seven required fields, separated by spaces. Each field has its corresponding allowed values:
| Order | Field | Value |
|---|---|---|
| First position | Second | Integer from 0-59 |
| 2nd | minute | Integer from 0 - 59 |
| 3rd | hour | Integer from 0 - 23 |
| 4th | Day of month | Integer from 1 - 31 (considering the number of days in the month) |
| 5th | Month | Integer from 1 - 12 or JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC |
| 6th | Day of week | Integer from 0 - 6 or MON, TUE, WED, THU, FRI, SAT, SUN, where 0 represents Sunday, 1 represents Monday, and so on |
| 7th | Year | Integer from 1970 - 2099 |
Wildcard
| Wildcard | Meaning |
|---|---|
, | Represents the union of values separated by commas. For example: In the "hour" field, 1,2,3 represents 1:00, 2:00, and 3:00. |
- | Includes all values in the specified range. For example: in the "day" field, 1 - 15 includes the 1st to the 15th of the specified month. |
* | Represents all values. In the "hour" field, it indicates every hour. |
/ | Specifies increments. In the "minute" field, entering 1/10 specifies repetition every 10 minutes starting from the first minute. For example, the 11th, 21st, 31st minutes, and so on. |
When both the "day" and "day of week" fields are specified in a Cron expression, they have an OR relationship, meaning both conditions will take effect.
cronjob Example
Here are some Cron expressions and their meanings:
*/5 * * * * * *means triggers every 5 seconds0 0 2 1 * * *means triggers at 2:00 AM on the 1st day of every month0 15 10 * * MON-FRI *means triggers at 10:15 AM every day from Monday to Friday0 0 10,14,16 * * * *means triggers at 10:00 AM, 2:00 PM, and 4:00 PM every day0 */30 9-17 * * * *means triggers every half hour from 9:00 AM to 5:00 PM every day0 0 12 * * WED *means triggers at 12:00 PM every Wednesday