Trigger
A trigger is an abstraction of a module that triggers functions based on specific rules. CloudBase cloud function currently only supports timer triggers.
If a cloud function needs to be executed at scheduled/regular times, that is, triggered on a schedule, you can use the cloud function timer trigger. A cloud function configured with a timer trigger will be automatically triggered at the corresponding time points, and the function's return result will not be returned to the caller.
An example:
{
"version": "2.0",
"envId": "xxx",
"functions": [
{
// The triggers field is an array of triggers. Currently, only one trigger is supported, meaning the array can only contain one element; multiple entries are not allowed.
"triggers": [
{
// name: Trigger name, rules are described below
"name": "myTrigger",
// type: Trigger type. Currently only supports timer (which is a scheduled trigger)
"type": "timer",
// config: Trigger configuration. For timer triggers, the config should be a cron expression, and the rules are described below.
"config": "0 0 2 1 * * *"
}
]
}
]
}
Create a Function Trigger
You can use the following command to create a trigger:
# Creating a Trigger for app Function Configuration
tcb fn trigger create app
Cloudbase CLI automatically reads the timer trigger specified in the function configuration of the cloudbaserc.json
file and creates a cloud function trigger. If the configuration file does not contain trigger configuration, the creation will fail.
A function can contain multiple triggers, and each trigger includes the following three key pieces of information: name, type, config
.
{
// name: Trigger name, rules are described below
"name": "myTrigger",
// type: Trigger type. Currently only supports timer (which is a scheduled trigger)
"type": "timer",
// config: Trigger configuration. For timer triggers, the config should be a cron expression.
"config": "0 0 2 1 * * *"
}
When no function name is specified, Cloudbase CLI creates all triggers for all functions included in the cloudbaserc.json
file.
Delete a Function Trigger
You can use the following command to delete the function's trigger:
# Delete the trigger named 'trigger' for the app function
tcb fn trigger delete app trigger
Similarly, when no function name is specified, Cloudbase CLI deletes all triggers for all functions included in the cloudbaserc.json
file. When only the function name is specified, Cloudbase CLI deletes all triggers for the specified function. When both the function name and trigger name are specified, Cloudbase CLI only deletes the specified trigger.
# Delete all triggers for all functions in the `cloudbaserc.json` file
tcb fn trigger delete
# Delete all triggers for the function app
tcb fn trigger delete app
# Delete the trigger 'trigger' for the function app
tcb fn trigger delete app trigger
Trigger Rules
- Timer trigger name (name): supports up to 60 characters, supports
a-z
,A-Z
,0-9
,-
, and_
. It must start with a letter, and multiple timer triggers with the same name are not supported under a single function. - Timer trigger schedule (config): The scheduled function trigger time. Enter a custom standard Cron expression to determine when to trigger the function. For more information about Cron expressions, refer to the following content.
A Cron expression has seven required fields, separated by spaces. Each field has a corresponding range of values:
Order | Field | Value | Wildcard |
---|---|---|---|
First | Seconds | 0 - 59 integer | , - * / |
Second | Minutes | 0 - 59 integer | , - * / |
Third | Hours | 0 - 23 integer | , - * / |
Fourth | Day | 1 - 31 integer (considering the number of days in the month) | , - * / |
Fifth | Month | 1 - 12 integer or JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, and DEC | , - * / |
Sixth | Day of week | 0 - 6 integer or MON, TUE, WED, THU, FRI, SAT, and SUN, where 0 represents Sunday, 1 represents Monday, and so on | , - * / |
Seventh | Year | 1970 - 2099 integer | , - * / |
Wildcard
Wildcard | Meaning |
---|---|
, (comma) | 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. |
- (hyphen) | 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. |
* (asterisk) | Represents all values. For example, in the "hour" field, * represents every hour. |
/ (forward slash) | Specifies increments. In the "minute" field, entering 1/10 specifies repetition every ten minutes starting from the first minute. For example, the 111th minute, the 21st minute, and the 31st minute, and so on. |
! When both the "day" and "day of week" fields are specified in a Cron expression, they are in an "OR" relationship, meaning both conditions take effect.
Example
Below are some Cron expressions and their meanings:
*/5 * * * * * *
means trigger every 5 seconds0 0 2 1 * * *
means trigger at 2:00 AM on the 1st day of every month.0 15 10 * * MON-FRI *
means trigger at 10:15 AM every day from Monday to Friday.0 0 10,14,16 * * * *
means trigger at 10 AM, 2 PM, and 4 PM every day.0 */30 9-17 * * * *
means trigger every 30 minutes between 9:00 AM and 5:00 PM every day.0 0 12 * * WED *
means trigger at 12:00 PM (noon) every Wednesday.