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
- Scheduled Execution: Timer triggers execute your function based on a predefined schedule.
- CRON Expressions: Schedules are typically defined using CRON expressions, providing a flexible way to specify exact times and intervals.
- System-Managed: The timer trigger runs within the Azure Functions host, ensuring reliability and availability.
- Scalability: As your application scales, timer triggers continue to operate without manual intervention.
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}
- {second}: 0-59
- {minute}: 0-59
- {hour}: 0-23
- {day of month}: 1-31
- {month}: 1-12 or JAN-DEC
- {day of week}: 0-6 or SUN-SAT (0 or 7 is Sunday)
Special characters:
*: Wildcard, matches any value.,: List separator.-: Range of values./: Step values.
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
- Idempotency: Ensure your function can be run multiple times without unintended side effects.
- Error Handling: Implement robust error handling and logging.
- Monitoring: Use Azure Monitor to track your timer function's executions and performance.
- Avoid Long-Running Tasks: For very long operations, consider alternative patterns like Durable Functions or sending messages to a queue.
For detailed API references and advanced configurations, please refer to the official Azure Functions documentation.
Azure Functions Timer Trigger Documentation CRON Expression Guide