Timer Trigger

The timer trigger in Azure Functions allows you to run a function on a schedule. This is ideal for tasks like background processing, batch jobs, or simply executing code at regular intervals.

Cron Expressions

The timer trigger uses cron expressions to define the schedule. A cron expression consists of six required fields, separated by spaces, in the following order:

{second} {minute} {hour} {day} {month} {day-of-week}

Each field can contain numbers, wildcards (*), ranges (-), lists (,), and step values (/).

Cron Field Details

Field Description Allowed Values
second Seconds of the minute 0-59
minute Minute of the hour 0-59
hour Hour of the day 0-23
day Day of the month 1-31
month Month of the year 1-12 (or names like JAN, FEB)
day-of-week Day of the week 0-6 (0=Sunday, 6=Saturday, or names like SUN, MON)

Cron Expression Examples

Configuring the Timer Trigger

The timer trigger is configured in the function.json file (for JavaScript, Python, etc.) or via attributes/decorators (for C#, Java).

function.json Example

For JavaScript or other languages using function.json, the configuration looks like this:

{
  "scriptFile": "../run.js",
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *"
    }
  ]
}

In this example:

C# Attributes Configuration

For C#, you use attributes:

using Microsoft.Azure.WebJobs;

public static class TimerTriggerExample
{
    [FunctionName("TimerTriggerCSharp")]
    public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    }
}

The string passed to TimerTrigger is the cron expression.

TimerInfo Object

When the timer trigger fires, it passes a TimerInfo object to your function. This object provides useful information about the timer.

Common properties include:

Handling Past Due Executions

It's often a good practice to check IsPastDue and handle it appropriately. For example, you might want to skip execution if it's significantly late to avoid processing old data.

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

    if (myTimer.isPastDue) {
        context.log('JavaScript isPastDue!');
    }

    context.log('JavaScript timer trigger function executed at!', timeStamp);
    context.log('Next timer fire is at: ', myTimer.next);
};

Advanced Scheduling

Using the Lease Mechanism

Azure Functions employs a lease mechanism to ensure that only one instance of a timer-triggered function runs at a time, even in scaled-out environments. This prevents duplicate executions. You don't typically need to configure this explicitly, as it's handled by the Functions runtime.

Non-Cron Schedules (e.g., C#)

Some languages, like C#, offer alternative ways to specify schedules. For example, you can use the ScheduleStatus property to define schedules based on intervals:

using Microsoft.Azure.WebJobs;
using System;

public static class IntervalTimer
{
    [FunctionName("IntervalTimerCSharp")]
    public static void Run([TimerTrigger("00:01:00")]TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    }
}

This example runs every 1 minute. The format is typically HH:MM:SS.

Tip: Use the Azure Functions Timer expression calculator (available online) to help validate your cron expressions.

Common Use Cases