Azure Functions Timer Triggers
Last updated: September 28, 2023
Azure Functions provide a serverless compute experience that allows you to run small pieces of code, or "functions," in the cloud. Timer triggers are a common way to execute these functions on a schedule, enabling background tasks, batch processing, and recurring operations without manual intervention.
What are Timer Triggers?
A Timer trigger is a type of Azure Function trigger that executes your function on a specified schedule. This is achieved using a cron expression, which defines the recurrence pattern for the trigger.
Key Concepts
Cron Expressions
Timer triggers utilize a cron expression to define the schedule. A cron expression is a string consisting of six fields (or seven, if the seventh field, indicating seconds, is included):
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ │
│ │ │ │ │ └─ Day of the week (0 - 7) (Sunday=0 or 7)
│ │ │ │ └─ Month (1 - 12)
│ │ │ └─ Day of the month (1 - 31)
│ │ └─ Hour (0 - 23)
│ └─ Minute (0 - 59)
└─ Second (0 - 59)
Here are some common examples:
0 * * * * *
: Every hour, at the top of the hour.0 0 * * * *
: Every day, at midnight.0 0 12 * * *
: Every day, at 12:00 PM.0 0 12 1 * *
: On the first day of every month, at 12:00 PM.0 30 10 * * 1-5
: Every weekday (Monday to Friday) at 10:30 AM.
Function App Configuration
To use a timer trigger, you typically define it in your function's configuration file (e.g., function.json
for JavaScript or Python, or using attributes in C#). The trigger configuration specifies the schedule and other optional parameters.
Example: function.json
for JavaScript
{
"scriptFile": "index.js",
"bindings": [
{
"name": "myTimer",
"type": "timerTrigger",
"direction": "in",
"schedule": "0 */5 * * * *"
}
]
}
This configuration will run the function every 5 minutes.
Example: C# Attributes
using Microsoft.Azure.WebJobs;
public static class TimerTriggeredFunction
{
[FunctionName("MyTimerFunction")]
public static void Run(
[TimerTrigger("0 */10 * * * *")] TimerInfo myTimer)
{
// Your function logic here
System.Console.WriteLine($"C# Timer trigger function executed at: {System.DateTime.Now}");
}
}
This C# example runs the function every 10 minutes.
TimerInfo
Object
When the timer trigger fires, the function receives a TimerInfo
object (or its equivalent in your language) that provides information about the trigger instance, such as the schedule, the next due time, and whether it's a past due invocation.
Handling Past Due Invocations
By default, if your function app is down or experiences issues during a scheduled execution time, timer triggers are designed to be "at-least-once" reliable. This means that when the function app comes back online, it might execute missed invocations. You can control this behavior with the runOnStartup
and useMonitor
properties in your configuration.
Common Use Cases
- Scheduled Data Processing: Processing data from a database or storage at regular intervals.
- Batch Operations: Running batch jobs, like generating reports or sending out periodic email summaries.
- Cleanup Tasks: Deleting old logs, temporary files, or expired data.
- API Polling: Periodically checking an external API for updates.
- Scheduled Notifications: Sending reminders or alerts based on time.
Best Practices
- Idempotency: Design your functions to be idempotent. This means that running the function multiple times with the same input should have the same effect as running it once. This is important because triggers can sometimes fire more than once.
- Error Handling: Implement robust error handling and logging to diagnose and resolve issues quickly.
- Monitoring: Use Azure Monitor and Application Insights to track the execution of your timer-triggered functions and set up alerts for failures.
- Resource Management: Be mindful of the resources your function consumes, especially when running frequently. Optimize your code for performance and efficiency.
- Time Zones: Cron expressions are evaluated in UTC by default. Ensure you account for time zone differences if your schedule needs to align with a specific local time.