OpenWhisk actions can be triggered periodically by an alarm trigger (similar to a cron job). Lets look at how to deploy an alarm trigger using wskdeploy:

Step 1: Create a manifest file (manifest.yaml)

To define an alarm trigger, you have to specify the source of the trigger as /whisk.system/alarms/alarm in the manifest.yaml file:


packages:
    helloworld:
        actions:
            helloworld:
                location: src/hello.js
                runtime: nodejs:6
                inputs:
                    name:
                        type: string
                        description: name of a person
                    place:
                        type: string
                        description: location of a person
                outputs:
                    payload:
                        type: string
                        description: a simple greeting message, Hello World!
        triggers:
            Every12Hours:
                feed: /whisk.system/alarms/alarm
        rules:
            hellowroldOnCron:
                action: helloworld
                trigger: Every12Hours

Step 2: Create a deployment file (deployment.yaml)

Set cron as an input to alarm trigger Every12Hours. You can specify both traditional and enhanced versions of cron expression in the cron field:


<Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week>

OR

<Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week> <Year>

deployment.yaml:


application:
    name: SampleHelloWorld
    namespace: _
    packages:
        helloworld:
            actions:
                helloworld:
                    inputs:
                        name: Amy
                        place: Paris
            triggers:
                Every12Hours:
                    inputs:
                        cron: "0 */12 * * *"

You can also set a trigger payload (action parameter values during each trigger) in the deployment file using trigger_payload:


application:
    name: SampleHelloWorld
    namespace: _
    packages:
        helloworld:
            actions:
                helloworld:
                    inputs:
                        name: Amy
                        place: Paris
            triggers:
                Every12Hours:
                    inputs:
                        cron: "0 */12 * * * *"
                        trigger_payload: "{\"name\":\"Mark\", \"place\":\"Barcelona\"}"

Step 3: Deploy a sample action


./wskdeploy -p ~/SampleHelloWorldApp/
         ____      ___                   _    _ _     _     _
        /\   \    / _ \ _ __   ___ _ __ | |  | | |__ (_)___| | __
   /\  /__\   \  | | | | '_ \ / _ \ '_ \| |  | | '_ \| / __| |/ /
  /  \____ \  /  | |_| | |_) |  __/ | | | |/\| | | | | \__ \   <
  \   \  /  \/    \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\
   \___\/              |_|

Packages:
Name: helloworld
    bindings:
  * action: helloworld
    bindings:
        - name: name value: Amy
        - name: place value: Paris


Triggers:
* trigger: Every12Hours
    bindings:
        - name: cron value: 0 */12 * * * *
    annotations:
        - name: feed value: /whisk.system/alarms/alarm

 Rules
* rule: helloworldEvery12Hours
    - trigger: Every12Hours
    - action: helloworld

Do you really want to deploy this? (y/N): y
Deploying package helloworld ... Done!
Deploying action helloworld/helloworld ... Done!
Deploying trigger feed Every12Hours ...
Done!
Deploying rule helloworldEvery12Hours ... Done!

Deployment completed successfully.

At this point, you have an action helloworld configured to run every 12 hours. You can verify your deployment by firing a trigger and checking if action is invoked by following instructions in Whisk Deploy: Action, trigger, and rule. Enjoy!

Join The Discussion

Your email address will not be published. Required fields are marked *