Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
191 lines (143 sloc) 5.13 KB
title description services documentationcenter author manager editor tags keywords ms.assetid ms.service ms.devlang ms.topic ms.tgt_pltfrm ms.workload ms.date ms.author
Azure Functions timer trigger | Microsoft Docs
Understand how to use timer triggers in Azure Functions.
functions
na
christopheranderson
erikre
azure functions, functions, event processing, dynamic compute, serverless architecture
d2f013d1-f458-42ae-baf8-1810138118ac
functions
multiple
reference
multiple
na
10/31/2016
chrande; glenga

Azure Functions timer trigger

[!INCLUDE functions-selector-bindings]

This article explains how to configure and code timer triggers in Azure Functions. Azure Functions supports the trigger for timers. Timer triggers call functions based on a schedule, one time or recurring.

The timer trigger supports multi-instance scale-out. One single instance of a particular timer function is run across all instances.

[!INCLUDE intro]

Timer trigger

The timer trigger to a function uses the following JSON object in the bindings array of function.json:

{
    "schedule": "<CRON expression - see below>",
    "name": "<Name of trigger parameter in function signature>",
    "type": "timerTrigger",
    "direction": "in"
}

The value of schedule is a CRON expression that includes 6 fields: {second} {minute} {hour} {day} {month} {day of the week}. Many of the cron expressions you find online omit the {second} field. If you copy from one of them, you need to adjust for the extra {second} field. For specific examples, see Schedule examples below.

The default time zone used with the CRON expressions is Coordinated Universal Time (UTC). If you want your CRON expression to be based on another time zone, create a new app setting for your function app named WEBSITE_TIME_ZONE. Set the value to the the name of the desired time zone as shown in the Microsoft Time Zone Index.

For example, Eastern Standard Time is UTC-05:00. If you want your timer trigger to fire at 10:00 AM EST every day, your could use the following CRON expression which accounts for UTC time zone:

"schedule": "0 0 15 * * *",

Alternatively, you could add a new app setting for your function app named WEBSITE_TIME_ZONE and set the value to Eastern Standard Time. Then the following CRON expression could be used for 10:00 AM EST:

"schedule": "0 0 10 * * *",

Schedule examples

Here are some samples of CRON expressions you can use for the schedule property.

To trigger once every 5 minutes:

"schedule": "0 */5 * * * *"

To trigger once at the top of every hour:

"schedule": "0 0 * * * *",

To trigger once every two hours:

"schedule": "0 0 */2 * * *",

To trigger once every hour from 9 AM to 5 PM:

"schedule": "0 0 9-17 * * *",

To trigger At 9:30 AM every day:

"schedule": "0 30 9 * * *",

To trigger At 9:30 AM every weekday:

"schedule": "0 30 9 * * 1-5",

Trigger usage

When a timer trigger function is invoked, the timer object is passed into the function. The following JSON is an example representation of the timer object.

{
    "Schedule":{
    },
    "ScheduleStatus": {
        "Last":"2016-10-04T10:15:00.012699+00:00",
        "Next":"2016-10-04T10:20:00+00:00"
    },
    "IsPastDue":false
}

Trigger sample

Suppose you have the following timer trigger in the bindings array of function.json:

{
    "schedule": "0 */5 * * * *",
    "name": "myTimer",
    "type": "timerTrigger",
    "direction": "in"
}

See the language-specific sample that reads the timer object to see whether it's running late.

Trigger sample in C#

public static void Run(TimerInfo myTimer, TraceWriter log)
{
    if(myTimer.IsPastDue)
    {
        log.Info("Timer is running late!");
    }
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}" );  
}

Trigger sample in F#

let Run(myTimer: TimerInfo, log: TraceWriter ) =
    if (myTimer.IsPastDue) then
        log.Info("F# function is running late.")
    let now = DateTime.Now.ToLongTimeString()
    log.Info(sprintf "F# function executed at %s!" now)

Trigger sample in Node.js

module.exports = function (context, myTimer) {
    var timeStamp = new Date().toISOString();

    if(myTimer.isPastDue)
    {
        context.log('Node.js is running late!');
    }
    context.log('Node.js timer trigger function ran!', timeStamp);   

    context.done();
};

Next steps

[!INCLUDE next steps]