Scheduled trigger
You can use Timer Trigger combined with Cloud Function to implement the scheduled task feature.
Setting Up Scheduled Trigger
- Cloud Development Platform
- Mini Program Cloud Function
- CloudBase CLI Tool
Go to Cloud Development Platform/Cloud Functions, choose the function to configure, click Edit, modify the Scheduled Trigger option in the form, and you can upload a configuration file or configure the content. The format is as follows:
{
// The triggers field is an array of triggers. Currently, only one trigger is supported, meaning the array can contain only one element and multiple entries are not allowed.
"triggers": [
{
// name: trigger name, rules are described below
"name": "myTrigger",
// type: trigger type. Currently, only `timer` (namely scheduled trigger) is supported.
"type": "timer",
// config: trigger configuration. For scheduled triggers, the config format is a cron expression. Rules are described below.
"config": "0 0 2 1 * * *"
}
]
}
Under the directory of the cloud function that requires a trigger, create a new file named config.json. The format is as follows:
{
// The triggers field is an array of triggers. Currently, only one trigger is supported, meaning the array can contain only one element and multiple entries are not allowed.
"triggers": [
{
// name: trigger name, rules are described below
"name": "myTrigger",
// type: trigger type. Currently, only `timer` (namely scheduled trigger) is supported.
"type": "timer",
// config: trigger configuration. For scheduled triggers, the config format is a cron expression. Rules are described below.
"config": "0 0 2 1 * * *"
}
]
}
After creating, right-click the cloud function and click "Upload Trigger" to complete the trigger deployment.
Refer to CloudBase CLI Documentation.
Detailed Configuration Explanation
Field Rules
- Scheduled trigger name (name): Supports up to 60 characters, including
a-z,A-Z,0-9,-, and_. Must start with a letter. Multiple scheduled triggers with the same name are not supported under a single function. - Trigger cycle (config): The scheduled trigger time for the function. Enter a custom standard Cron expression to determine when to trigger the function. For more information about Cron expressions, refer to 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 | Seconds | Integer from 0 - 59 |
| Second Position | Minutes | Integer from 0 - 59 |
| Third Position | Hours | Integer from 0 - 23 |
| Fourth Position | Day of Month | Integer from 1 - 31 (must consider the number of days in the month) |
| Fifth Position | Month | Integer from 1 - 12 or JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC |
| Sixth Position | Day of Week | Integer from 0 - 6 or MON, TUE, WED, THU, FRI, SAT, SUN, where 0 is Sunday, 1 is Monday, and so on |
| Seventh Position | Year | Integer from 1970 - 2099 |
Wildcard
| Wildcard | Meaning |
|---|---|
, | Represents the union of comma-separated values. For example, in the "hour" field, '1,2,3' represents hours 1, 2, and 3. |
- | Includes all values within 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 represents every hour. |
/ | Specifies increments. In the "minute" field, entering '1/10' specifies repetition every ten minutes starting from the first minute. For example, the 11th minute, 21st minute, 31st minute, and so on. |
In a Cron expression, when both the "day" and "day of week" fields are specified, they have an OR relationship, meaning both conditions are effective.
cronjob example
The following lists some Cron expressions and their corresponding meanings:
*/5 * * * * * *triggers every 5 seconds0 0 2 1 * * *triggers at 2:00 AM on the 1st day of every month0 15 10 * * MON-FRI *triggers at 10:15 AM every Monday to Friday0 0 10,14,16 * * * *triggers at 10 AM, 2 PM, and 4 PM every day0 */30 9-17 * * * *triggers every half hour between 9 AM and 5 PM daily0 0 12 * * WED *triggers at 12 PM every Wednesday