# Timed Trigger

This feature requires Developer Tools 1.02.1811270 or above.

When a cloud function must be executed at set times or on a regular schedule, we can use a cloud function timed trigger. A cloud function configured with a timed trigger will be automatically triggered at the set time point, but the results returned by the function are not returned to the caller.

Create a new file config.json in the directory of the cloud function you want to add the trigger to in the following format:

{
  // The triggers field is the trigger array, and only one trigger is supported.
  "triggers": [
    {
      // name: The name of the trigger. See the rules below.
      "name": "myTrigger",
      // type: The type of the trigger. Only "timer" (i.e. timed trigger) is supported.
      "type": "timer",
      // config: The trigger configuration. For a timed trigger, the config format is a Cron expression. See the rules below.
      "config": "0 0 2 1 * * *"
    }
  ]
}

Field rules:

  • Timed trigger name (name): The name can include up to 60 characters and supports a-z, A-Z, 0-9, -, and _. The name must start with a letter and be unique within a function.
  • Timed trigger cycle (config): Specifies times at which the function is triggered. Enter a custom Cron expression to determine when the function is triggered. See below for further information.

# Cron Expression

A Cron expression contains seven required fields separated by spaces.

1st 2nd 3rd 4th 5th 6th 7th
Seconds Minutes Hours Days of Month Months Days of Week Years

Each field has a corresponding value range:

Field Value Range Wildcards
Seconds Integers between 0 and 59 , - * /
Minutes Integers between 0 and 59 , - * /
Hours Integers between 0 and 23 , - * /
Days of Month Integers between 1 and 31 (consider the number of days in the relevant month) , - * /
Months Integers between 1 and 12 or JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, and DEC , - * /
Days of Week Integers between 0 and 6 or MON, TUE, WED, THU, FRI, SAT, and SUN; 0 indicates Monday, 1 indicates Tuesday, and so on , - * /
Years Integers between 1970 and 2099 , - * /

Wildcards

Wildcard Description
, (comma) Separates the characters in a set. For example, in the "Hours" field, "1,2,3" represents 01:00, 02:00, and 03:00.
- (hyphen) Indicates a range of values. For example, in the "Days of Month" field, "1-15" indicates the 1st to the 15th of the month (inclusive).
* (asterisk) Indicates all possible values. For example, in the "Hours" field, * indicates each hour.
/ (forward slash) Specifies an increment. In the "Minutes" field, "1/10" specifies that the function is triggered on minute 1 and then every 10 minutes afterward, e.g. minute 11, minute 21, minute 31, and so on.

Notes

  • When the "Days of Month" and "Days of Week" fields are both specified in a Corn expression, they have an "or" relationship. This means that only one of the two conditions must be satisfied.

Examples

The examples below can help you better understand different types of Cron expressions:

  • */5 * * * * * * indicates the function is triggered once every 5 seconds.
  • 0 0 2 1 * * * indicates the function is triggered at 2:00 am on the 1st of each month.
  • 0 15 10 * * MON-FRI * indicates that the function is triggered at 10:15 am every Monday to Friday.
  • 0 0 10,14,16 * * * * indicates that the function is triggered at 10:00 am, 2:00 pm, and 4:00 pm every day.
  • 0 */30 9-17 * * * * indicates that the function is triggered every half hour between 9:00 am and 5:00 pm each day.
  • 0 0 12 * * WED * indicates that the function is triggered at 12:00 pm every Wednesday.

In the next chapter, we will take a look at the operating mechanism of cloud functions.