All Products
Search
Document Center

Timer trigger

Last Updated: Oct 10, 2019

Function Compute timer triggers are used to call functions at a scheduled time. This article describes the parameters involved in timer triggers, and illustrates how to configure a timer trigger by using the Function Compute console, the command-line tool fcli, and the SDK.

Configure a timer trigger

  1. {
  2. payload:"payload"
  3. cronExpression: "cronExpression"
  4. enable: true|false
  5. }
  • payload: The content of the event that is triggered. You can customize the content.
  • cronExpression: The time when the timer trigger calls the function. You can set the time in the following ways:
    • @every: Specifies the interval of invocation.
    • Cron expression: Use the crontab expression to schedule the invocation.

Cron expression settings

Note: The minimum trigger interval is 1 minute. Do not set a trigger interval of less than 1 minute in the following cron expressions:

@every

@every Value Unit: In this expression, Value is a positive integer, and Unit can be "ns", "us" (or "µs"), "ms", "s", "m", and "h". This expression indicates that the timer trigger calls the function at intervals as specified in Value Unit.

Example Expression
Calls the function every 5 minutes. @every 5m
Calls the function every 1.5 hours. @every 1.5h
Calls the function every 2.75 hours. @every 2h45m

Cron expression

The standard cron expression format is as follows: cron(Seconds Minutes Hours Day-of-month Month Day-of-week).

Note: The cron expression uses Coordinated Universal Time (UTC), and is set to UTC + 08:00 to indicate China Standard Time.

Field name Value Special character
Seconds 0-59
Minutes 0-59 , - * /
Hours 0-23 , - * /
Day-of-month 1-31 , - * ? /
Month 1-12 or JAN-DEC , - * /
Day-of-week 1-7 or SUN-SAT , - * ?

Special character descriptions

Character Description Example
* Every You can use this character in the Minutes field to call a function at the beginning of every minute.
, Field delimiter You can use this character in the Day-of-week field to separate days of a week, for example, MON,WED,FRI.
- Range You can use this character in the Hours field to specify a time range. For example, 10-12 means 10:00 UTC to 12:00 UTC.
? Unspecified value This character is used with other specified values. For example, you can use this character in the Day-of-week field to call a function at the specified time on any day of a week.
/ The increment added to a value. For example, n/m means to add an increment of m to n. You can set the Minutes field to 3/5 to call the function at the third minute with intervals of 5 minutes.

The following table shows examples of using the cron expressions.

The first column shows the China Standard Time (UTC + 08:00), and the second column shows UTC. For example, 12:00 China Standard Time equals 04:00 UTC. The corresponding cron expression is 0 0 4 * * *.

Example (China Standard Time) Example (UTC) Expression
Calls a function at 12:00 every day. Calls a funciton at 04:00 every day. 0 0 4 * * *
Calls a function at 12:30 every day. Calls a function at 04:30 every day. 0 30 4 * * *
Calls a function at the 26th, 29th, and 33rd minute of each hour. Calls a function at the 26th, 29th, and 33rd minute of each hour. 0 26,29,33 * * * *
Calls a function at 12:30 from Monday to Friday. Calls a function at 04:30 from Monday to Friday. 0 30 4 ? * MON-FRI
Calls a function at intervals of 5 minutes from 12:00 to 14:00, Monday to Friday. Calls a function at intervals of 5 minutes from 04:00 to 06:00, Monday to Friday. 0 0/5 4-6 ? * MON-FRI
Calls a function at 12:00 each day from January to April. Calls a function at 04:00 each day from January to April. 0 0 4 ? JAN,FEB,MAR,APR *

Event format

A timer trigger calls a function according to the following event: The function is processed based on the event.

  1. {
  2. "triggerTime":"2018-02-09T05:49:00Z",
  3. "triggerName":"my_trigger",
  4. "payload":"awesome-fc"
  5. }
  • triggerTime: The time when the timer trigger calls a function. The function may be triggered several times at the specified time. You can use this value to deduplicate trigger data.
  • triggerName: The trigger name.
  • payload: The custom content of the event that is triggered.

Create a timer trigger

You can create a timer trigger in the Function Compute console, by using the fcli tool, or by using the SDK, as follows:

You do not need to specify SourceArn and InvocationRole when creating a timer trigger.

By using the console

You can create a timer trigger when creating a function.

Create a function

You can also create a timer trigger for an existing function. Go to the Function Properties page, click the Triggers tab, and then click Create Trigger.

Create a timer trigger for an existing function

Use the fcli tool

  1. Create the yaml file that contains the TimeTrigger Config parameter. The following example shows a timeTriggerConfig.yaml sample file that is used to call a function at intervals of 5 hours. The payload parameter of this function is awesome-fc.

    1. triggerConfig:
    2. payload: "awesome-fc"
    3. cronExpression: "0 0 0/5 * * ?"
    4. enable: true
  2. Create a timer trigger in the corresponding function directory:mkt serviceName/functionName -t timer -c timeTriggerConfig.yaml.

Use the SDK to create a timer trigger

Function Compute provides multiple SDKs, such as fc-nodejs-sdk, fc-python-sdk, and fc-java-sdk. The following example uses Python to create a timer trigger.

  1. import fc2
  2. client = fc2. Client(
  3. endpoint = '<Your Endpoint>',
  4. accessKeyID = '<Your AccessKeyID>',
  5. accessKeySecret = '<Your AccessKeySecret>')
  6. service_name = '<service_name>'
  7. function_name = '<function_name>'
  8. trigger_name = '<trigger_name>'
  9. trigger_type = 'timer'
  10. trigger_config = {
  11. 'payload': 'awesome-fc',
  12. 'cronExpression': '0 0 0/5 * * ?',
  13. 'enable': True
  14. }
  15. client.create_trigger(service_name, function_name,
  16. trigger_name, trigger_type, trigger_config, None, None)

Function code

  1. import json
  2. import logging
  3. LOG = logging.getLogger()
  4. def handler(event, context):
  5. LOG.info('event: %s', event)
  6. evt = json.loads(event)
  7. triggerName = evt["triggerName"]
  8. triggerTime = evt["triggerTime"]
  9. message = evt["payload"]
  10. creds = context.credentials
  11. LOG.info('access_key_id: %s', creds.access_key_id)
  12. LOG.info("message = %s", message)