MSDN Documentation

Implementing Scheduled Tasks with Azure Functions

Azure Functions provide a powerful, serverless way to run code on a schedule. This allows you to automate recurring tasks, batch processing, data synchronization, and much more without managing infrastructure. This article explores how to create and manage scheduled tasks using the Timer Trigger in Azure Functions.

Understanding the Timer Trigger

The Timer Trigger in Azure Functions allows you to execute a function at a specified time. It's based on the CRON expression format, which is a standard way to define schedules.

CRON Expressions

A CRON expression consists of six fields (though some CRON implementations support seven), separated by spaces:

  1. Minute (0 - 59)
  2. Hour (0 - 23)
  3. Day of Month (1 - 31)
  4. Month (1 - 12)
  5. Day of Week (0 - 6, where 0 is Sunday, or Sun, Mon, Tue, etc.)
  6. Year (optional, can be specified as a 4-digit year or range)

Here are some common examples:

Note: Azure Functions uses a specific CRON syntax. For detailed information, refer to the official Azure Functions documentation on CRON expressions.

Creating a Timer Triggered Function

You can create a Timer Triggered function using the Azure portal, Azure CLI, or your preferred development tools.

Example: Using C#

When creating a new function, select the Timer Trigger template. The core of the function will look something like this:


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

namespace AzureFunctions.ScheduledTasks
{
    public static class TimerTriggerFunction
    {
        [FunctionName("MyTimerTrigger")]
        public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            
            // Your scheduled task logic goes here.
            // For example, fetch data, perform maintenance, send notifications, etc.

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

In this example:

Example: Using JavaScript (Node.js)


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);

    // Your scheduled task logic goes here.
};
            

The function.json file for a JavaScript Timer Trigger would typically look like this:


{
  "schedule": "0 */5 * * * *",
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *"
    }
  ]
}
            
Tip: For complex schedules or to avoid unintended runs, consider using a CRON expression generator tool.

Managing Scheduled Tasks

Once deployed, your Timer Triggered functions will run automatically according to their schedule. You can monitor their execution through the Azure portal's monitoring features, which include logs, metrics, and execution history.

Key Considerations:

Warning: Misconfigured CRON expressions can lead to unexpected execution times or missed runs. Always test your schedules thoroughly.

Advanced Scenarios

Conclusion

Azure Functions Timer Triggers offer a robust and flexible solution for automating recurring tasks. By understanding CRON expressions and following best practices, you can effectively leverage this feature to streamline your cloud operations and build more efficient applications.