All Products
Search
Document Center

Timer trigger sample

Last Updated: Sep 06, 2018

A trigger is used to call a function. If a function is triggered automatically by an event, this event is called the event source. You can configure the event source in the same way as you configure a timer trigger to call a function.

Contents

This tutorial describes how a timer trigger calls a function in the Function Compute console, by using the command-line tool fcli, or by using the SDK. The trigger Calls the testHelloWorld function in Sample 1 every 5 minutes. Select one of the following methods to set a timer trigger.

Create a timer trigger in the Function Compute console

We recommend that you read How to use the console and Timer trigger before you start creating a timer trigger. You can set the timer trigger during or after creating the function. See Basic operations. This example describes how to set the timer trigger after you have created the function.

  1. On the function execution page, click Triggers -> Create Trigger -> Time Trigger. One can set the name and the interval or cron expression of the timer trigger on this page. The event payload content is the data the timer function gets when it’s invoked.console screenshot

  2. After completing the configuration, you can check the function log to verify that the function is called at the specified time. You can check the function log on the Log Service console or by using the fcli tool. For more information, see Function logs.

Create the timer trigger by using the fcli tool

Create a timer trigger after you have completed Sample 1.The service name is demo. The function name is testTHelloWorld. The timer trigger name is time-trigger.

  1. Enter the interactive mode of fcli.

    1. # get into service
    2. >>> cd demo
    3. # get into function
    4. >>> cd testHelloWorld
  2. Create trigger

    1. >>> mkt time-trigger -t timer -c timeTriggerConfig.yaml
    • -t specifies the type of the trigger.
    • -c specifies the configuration file.

You can use mkt --help to obtain more information about the trigger. You can use logs testHelloWorld to check the function log and verify that the function has been called as specified.

Create the timer trigger by using an SDK

For more information about SDKs for multiple languages, see SDKs.

Python SDK example

  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)

Nodejs SDK example

  1. 'use strict';
  2. var fs =require('fs')
  3. var FCClient = require('@alicloud/fc2');
  4. var client = new FCClient('<accountId>', {
  5. accessKeyID: '<accessKeyID>',
  6. accessKeySecret: '<accessKeySecret>',
  7. region: 'cn-shanghai',
  8. timeout: 10000 // Request timeout in milliseconds, default is 10s
  9. });
  10. var serviceName = '<serviceName>';
  11. var funcName = '<fuctionName>';
  12. var triggerName = '<triggerName>'
  13. var timeTriggerConfig = {
  14. payload: 'awesome-fc',
  15. cronExpression: '0 0 0/5 * * ?',
  16. enable: true
  17. }
  18. client.createTrigger(serviceName, funcName,{
  19. triggerName: triggerName,
  20. triggerType: 'timer',
  21. triggerConfig: timeTriggerConfig
  22. } ).then(function(res) {
  23. console.log('create trigger: %j', res)
  24. }).catch(function(err){
  25. console.error(err)
  26. })

Java SDK example

  1. import com.aliyuncs.fc.client.FunctionComputeClient;
  2. import com.aliyuncs.fc.model.TimeTriggerConfig;
  3. import com.aliyuncs.fc.request.*;
  4. import com.aliyuncs.fc.response.*;
  5. import java.io.IOException;
  6. public class testTimeTrigger {
  7. private static final String REGION = "cn-shanghai";
  8. private static final String SERVICE_NAME = "<service_name>";
  9. private static final String FUNCTION_NAME = "<function_name>";
  10. public static void main(final String[] args) throws IOException {
  11. String accountId = "<accountId>";
  12. String accessKey = "<accessKey";
  13. String accessSecretKey = "accessSecretKey";
  14. // Initialize FC client
  15. FunctionComputeClient fcClient = new FunctionComputeClient(REGION, accountId, accessKey, accessSecretKey);
  16. String cronEvery = "@every 5m";
  17. String payload = "awesome-fc";
  18. String triggerName = "time-trigger";
  19. CreateTriggerRequest createTReq = new CreateTriggerRequest(SERVICE_NAME, FUNCTION_NAME);
  20. TimeTriggerConfig timeTriggerConfig = new TimeTriggerConfig(cronEvery, payload, true);
  21. createTReq.setTriggerName(triggerName);
  22. createTReq.setTriggerType("timer");
  23. createTReq.setTriggerConfig(timeTriggerConfig);
  24. CreateTriggerResponse createTriggerResponse = fcClient.createTrigger(createTReq);
  25. }
  26. }

References