This topic describes how to configure a scheduled trigger in the Function Compute console, including creating a trigger, writing a function, and testing a function.

Procedure

  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, click Services and Functions.
  3. In the top navigation bar, select the region where your Kubernetes cluster is deployed.
  4. On the Services page, find the service that you want to manage and click Functions in the Actions column.
  5. On the Functions page, click the name of the function that you want to manage.
    hanshuguanli
  6. On the function details page, click the Triggers tab, select the version or alias from the Version or Alias drop-down list, and then click Create Trigger.
    Create a webhook
  7. In the Create Trigger panel, enter the relevant information. Click OK.
    Create Trigger Panel-timer
    Parameter Configuration method Example
    Trigger Type Select Time Trigger. Time Trigger
    Parameter Enter a custom trigger name. my_trigger
    Version or alias The default value is LATEST. If you want to create a trigger of another version or alias, you must switch to this version or alias in the upper-right corner of the function details page. For more information about the version, see Introduction to versions. LATEST
    Trigger Select the trigger method as needed:
    • Triggered by Time Interval: Enter a positive integer n in the Time Interval field, which indicates that the function is executed every n minutes.
    • Triggered by CRON Expression: Enter a Cron expression in the CRON Expression field to trigger a function execution at the specified time point.
    Interval: 5 Minutes
    Event Payload Enter custom parameters. The trigger message will be used as the payload value in the event.
    {awesome-fc}
    Time expressions
    • CRON expression

      The CRON expression uses Coordinated Universal Time (UTC). For example, if the function is scheduled at 12:00 every day in Beijing time, then the function is scheduled at 4:00 every day in UTC time, you can use the 0 0 4 * * *.

    • Time zone expression

      If your task needs to run in a specific time zone, you can specify it by CRON_TZ. For example, if the function is triggered at 04:00 on the first day of each month in Beijing time, you can use the CRON_TZ=Asia/Shanghai 0 0 4 1 * *. The time zone expression varies with the region.

      Note

      If your time zone uses summer time and winter time, the function may be scheduled more or fewer times than expected during the changes between summer time and winter time. We recommend that you set the scheduling time outside of the change periods.

    The following table describes common examples of time expressions. For example, if you want a function in Function Compute to be triggered at 12:00 (UTC+8), the CRON expression is 0 0 4 * * *, and the time zone expression is CRON_TZ=Asia/Shanghai 0 0 12 * * *.
    Examples Cron expression Time zone expression
    Schedule the function at 12:00 every day 0 0 4 * * * CRON_TZ=Asia/Shanghai 0 0 12 * * *
    Schedule the function at 12:30 every day 0 30 4 * * * CRON_TZ=Asia/Shanghai 0 30 12 * * *
    Schedule the function at the 26th, 29th, and 33rd minutes in each hour 0 26,29,33 * * * * CRON_TZ=Asia/Shanghai 0 26,29,33 * * * *
    Schedule the function at 12:30 every day from Monday to Friday 0 30 4 ? * MON-FRI CRON_TZ=Asia/Shanghai 0 30 12 ? * MON-FRI
    Schedule the function every 5 minutes from 12:00 to 14:00 every day from Monday to Friday 0 0/5 4-6 ? * MON-FRI CRON_TZ=Asia/Shanghai 0 0/5 12-14 ? * MON-FRI
    Schedule the function at 12:00 every day from January to April 0 0 4 ? JAN,FEB,MAR,APR * CRON_TZ=Asia/Shanghai 0 0 12 ? JAN,FEB,MAR,APR *
    The following table describes the fields of the CRON expression.
    • Field description
      Field Value range Allowed special character
      Seconds 0 to 59 None.
      Minutes 0 to 59 , - * /
      Hours 0 to 23 , - * /
      Day-of-month 1 to 31 , - * ? /
      Month 1 to 12 or JAN to DEC , - * /
      Day-of-week 0 to 6 or MON to SUN , - * ?
    • Special characters
      Character Definitions Examples
      * Indicates any or each. In the Minutes field, 0 indicates that the function is triggered at the 0th second of every minute.
      , Indicates the list value. In the Day-of-week field, MON,WED,FRI indicates Monday, Wednesday, and Friday.
      - Indicates a range. In the Hours field, 10-12 indicates that the time range is from 10:00 to 12:00 in UTC.
      ? Indicates an uncertain value. This special character is used with other specified values. For example, if you specify a specific date, but you do not care what day of the week it is, you can use this special character in the Day-of-week field.
      / Indicates the increment of a value. For example, n/m means to add an increment m to n each time. In the minute field, 3/5 indicates that the function is triggered every 5 minutes starting from the 3rd minute.

    After the trigger is created, the created trigger is displayed in the Trigger Name list.

    timer-trigger

Write code for a function

After you create a scheduled trigger, you can write function code to trigger function execution at a scheduled time.

On the function details page, click the Function Code tab and write code in the code editor.
In the following example, Python is used to define the function.
import json
import logging

logger = logging.getLogger()

def handler(event, context):
    logger.info('event: %s', event)

    evt = json.loads(event)
    triggerName = evt["triggerName"]
    triggerTime = evt["triggerTime"]
    message = evt["payload"]
    creds = context.credentials
    logger.info('access_key_id: %s', creds.access_key_id)
    logger.info("message = %s", message)                        

Test a function

After you define a function, you must debug it to verify the correctness of its code. When the specified time arrives, a time trigger automatically triggers the function.

  1. On the Function Details page, click the Function Code tab, and then click the xialatubiao icon. From the drop-down list, select Configure Test Parameters.
  2. In the Configure Test Parameters dialog box, click the Create New Test Event or Edit Existing Test Event tab, and specify the Event Name and event content. Click OK.

    The scheduled trigger triggers the function in the following event format.

    {
        "triggerTime":"2018-02-09T05:49:00Z",
        "triggerName":"timer-trigger",
        "payload":"awesome-fc"
    }            
    Filed Type Example Description
    triggerTime String 2018-02-09T05:49:00Z The triggering time of the function.
    triggerName String timer-trigger The name of the time trigger.
    payload String awesome-fc The value of the Trigger parameter that you enter in the trigger configuration.
  3. On the function details page, click the Function Code tab, and then click Test Function.

Check the result

On the Function Code tab, you can view a successful command.

result

Additional information

In addition to the Function Compute console, you can configure triggers in the following ways:
  • Use the Serverless Devs tool to configure triggers. For more information, see Serverless Devs.
  • Configure triggers by using the SDK. For more information, see Supported SDKs.

To modify or delete a created trigger, see Manage triggers.