Introduction to Timer Triggers

Azure Functions Timer Trigger allows you to run a Function App on a schedule. This is incredibly useful for a wide variety of scenarios, such as:

  • Performing cleanup tasks periodically.
  • Sending out scheduled reports.
  • Synchronizing data at regular intervals.
  • Running background jobs.

The Timer Trigger uses the NCRONTAB expression format to define the schedule, providing a flexible way to specify when your function should execute.

How it Works

When you configure a Timer Trigger, Azure Functions manages a timer that periodically checks the schedule. When the schedule matches the current time, the function is invoked.

Configuration

The core of the Timer Trigger configuration is the schedule expression. This is a string that defines the time pattern for execution. The format follows NCRONTAB, which is similar to cron expressions but with a few key differences, most notably the inclusion of a sixth field for seconds.

NCRONTAB Expression Format

The format is:

{second} {minute} {hour} {day} {month} {day of the week}
  • Second: 0-59
  • Minute: 0-59
  • Hour: 0-23
  • Day: 1-31
  • Month: 1-12 or JAN-DEC
  • Day of the week: 0-6 or SUN-SAT (where 0 is Sunday)

Common Schedule Examples

  • Every minute: * * * * * *
  • Every hour at the 15-minute mark: 0 15 * * * *
  • Once a day at 3:30 AM: 0 30 3 * * *
  • Every Monday at 9 AM: 0 0 9 * * 1
  • Every 5 minutes: 0 */5 * * * *

Note: By default, Timer Triggers are configured to run only once. To enable repeated runs, set the isPastDue: true property in your function's binding configuration.

Creating a Timer Trigger Function

You can create a Timer Trigger function using the Azure portal, Azure CLI, or by defining it in your project's function.json file (for JavaScript, Python, etc.) or attributes (for C#).

Example (function.json for JavaScript)

{
  "schedule": "0 */5 * * * *",
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *"
    },
    {
      "name": "myTimer",
      "type": "blob",
      "path": "logs/timer-log-{datetime}.txt",
      "connection": "AzureWebJobsStorage",
      "direction": "out"
    }
  ],
  "disabled": false
}

Example (C# attribute)

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

public static class TimerTriggerFunction
{
    [FunctionName("DailyReport")]
    public static void Run([TimerTrigger("0 0 1 * * *")]TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.UtcNow}");
        // Your scheduled task logic here
    }
}

Best Practices

  • Idempotency: Design your timer-triggered functions to be idempotent. This means that running the function multiple times with the same input should produce the same result.
  • Error Handling: Implement robust error handling and logging to diagnose and resolve issues quickly.
  • Resource Management: Be mindful of the resources your function consumes, especially if it runs frequently.
  • Monitoring: Utilize Azure Monitor to track the execution of your timer-triggered functions.

Advanced Scenarios

For more complex scheduling needs, consider combinations of NCRONTAB expressions or explore other Azure services like Azure Logic Apps or Azure Data Factory which offer more sophisticated workflow orchestration capabilities.