Timer Trigger Bindings

The Timer trigger in Azure Functions allows you to run a function on a schedule. This is useful for scenarios like running background tasks, processing data periodically, or sending out scheduled reports.

How it Works

The Timer trigger uses a CRON expression to define the schedule. When the time specified by the CRON expression is reached, the trigger fires and executes your function. It also provides a TimerInfo object to your function, which contains information about the timer elapsed and whether it's the first run.

Configuration

The Timer trigger is configured using a function.json file. Here's a basic example:

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

The schedule property accepts a CRON expression. The example above, "0 */5 * * * *", means the function will run every 5 minutes.

CRON Expression Format

Azure Functions uses the following CRON expression format (seconds, minutes, hours, day of month, month, day of week):

* * * * * *

Important:

The Timer trigger is based on UTC time. Ensure your CRON expressions are set relative to UTC.

TimerInfo Object

When your Timer triggered function executes, it receives a TimerInfo object as a parameter. This object provides two important properties:

Code Examples

C# Example

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

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

        if (myTimer.IsPastDue)
        {
            log.LogInformation("Timer is past due!");
        }

        log.LogInformation($"Next timer is scheduled at: {myTimer.Next}");
    }
}

JavaScript Example

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

    if (myTimer.isPastDue) {
        context.log('JavaScript is running late!');
    }
    context.log('JavaScript timer trigger function ran!', timeStamp);
    context.log('Next timer is scheduled at: ' + myTimer.next);
};
For JavaScript, the schedule is typically defined in function.json as shown above, and myTimer is passed as an argument to your script.

Python Example

import datetime
import logging

import azure.functions as func


def main(myTimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().isoformat()

    if myTimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function ran at %s', utc_timestamp)
    logging.info('Next timer is scheduled at: %s', myTimer.next_run)
In Python, the schedule is defined in function.json, and the timer information is accessed via the func.TimerRequest object.

Tip:

For development and testing, you can use specific CRON expressions to run your timer frequently (e.g., every minute). Remember to change it back to your desired production schedule.

Considerations

Next Steps

Explore how to integrate Timer triggers with other Azure services, such as Azure Storage for data processing or Azure Cosmos DB for scheduled updates.