Azure Serverless Timer Task Sample

Demonstrating scheduled execution with Azure Functions

This sample illustrates how to create a serverless timer task using Azure Functions. A timer trigger allows your function to execute on a recurring schedule, making it ideal for background jobs, data synchronization, or periodic cleanup.

What is a Timer Trigger?

A timer trigger in Azure Functions allows you to run a function at a specified interval. This is configured using a cron expression, similar to how you would schedule tasks on a Linux system.

Key features:

Example: Timer Task Function (C#)

Here's a typical C# implementation for a timer-triggered Azure Function.

Function.cs

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

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

            // Your scheduled task logic goes here.
            // For example:
            // - Fetch data from a database
            // - Perform background processing
            // - Send out notifications
            // - Clean up temporary files

            if (myTimer.IsPastDue)
            {
                log.LogWarning($"Timer was triggered late. Next due time: {myTimer.Next}");
            }

            log.LogInformation($"Timer finished. Next execution at: {myTimer.Next}");
        }
    }
}

function.json (Configuration)

{
  "scriptFile": "bin/AzureTimerTaskSample.dll",
  "entryPoint": "AzureTimerTaskSample.TimerTriggerFunction.Run",
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *"
    }
  ]
}

The schedule property uses a cron expression. 0 */5 * * * * means "at minute 0 of every 5th minute, every hour, every day of the month, every month, every day of the week".

Example: Timer Task Function (Node.js)

And here's a JavaScript example using Node.js.

index.js

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

    if (myTimer.isPastDue) {
        context.log('JavaScript isPastDue: ' + timeStamp);
    }
    context.log('JavaScript timer trigger function executed at: ', timeStamp);

    // Your scheduled task logic goes here.
    // For example:
    // - Fetch data from an API
    // - Process a queue
    // - Update a dashboard

    context.log('Next timer will be: ' + myTimer.next);
};

function.json (Configuration)

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

Key Considerations

Learn More

For more in-depth information and advanced configurations, please refer to the official Azure Functions documentation:

Azure Functions Timer Trigger Documentation