Azure Functions Timer Triggers

Timer triggers in Azure Functions allow you to run code on a schedule. This is incredibly useful for tasks like batch processing, sending out daily reports, or performing regular maintenance.

Key Concepts

CRON Expression Format

CRON expressions are strings that define the schedule. They consist of five or six fields (depending on whether you include seconds):

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

Special characters:

Example: Run every 5 minutes: 0 */5 * * * *

Example: Run daily at 10:30 AM: 0 30 10 * * *

Defining a Timer Trigger

You can define a timer trigger in your function's configuration (e.g., function.json for JavaScript/Python or attributes for C#).

Example: function.json (JavaScript)

{
  "schedule": "0 */5 * * * *",
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *"
    },
    {
      "name": "outputBlob",
      "type": "blob",
      "direction": "out",
      "path": "mytimerlog/{rand-guid}.txt",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

Example: C# Attribute

using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace MyCompany.MyFunctions
{
    public class TimerTriggerExample
    {
        private readonly ILogger _logger;

        public TimerTriggerExample(ILogger logger)
        {
            _logger = logger;
        }

        [Function("TimerTriggerCSharp")]
        public void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer)
        {
            _logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            // Your timer logic here
        }
    }
}

TimerInfo Object

When a timer trigger fires, it provides a TimerInfo object (or equivalent) to your function, containing information about the current and next scheduled execution.

Property Description
IsPastDue Indicates if the timer trigger is running late.
Now The current time.
NextOccurrence The next scheduled execution time.

Best Practices

For detailed API references and advanced configurations, please refer to the official Azure Functions documentation.

Azure Functions Timer Trigger Documentation CRON Expression Guide