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
- 0 * * * * *- Every minute
- 0 0 * * * *- Every hour at the start of the hour
- 0 0 12 * * *- Every day at noon
- 0 0 12 1 * *- On the 1st of every month at noon
- 0 15 10 * * 1-5- Every weekday (Monday to Friday) at 10:15 AM
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:
- name: The name of the timer object in your function code.
- type: Set to- timerTrigger.
- direction: Set to- inas it's an input trigger.
- schedule: The cron expression defining when the function should run. In this case,- "0 */5 * * * *"means every 5 minutes.
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:
- IsPastDue: A boolean indicating if the timer job is running late (i.e., the scheduled time has passed).
- NextDeliveryUtc: The UTC date and time when the timer is scheduled to run next.
- LastDeliveryUtc: The UTC date and time when the timer last ran.
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.
            
Common Use Cases
- Scheduled data ingestion or ETL processes.
- Regularly cleaning up temporary files or database entries.
- Sending out scheduled reports or notifications.
- Heartbeat checks or monitoring tasks.
- Synchronizing data between services.