Timer Trigger Bindings
The Timer trigger in Azure Functions allows you to run a function on a schedule. This is useful for scenarios like running background tasks, processing data periodically, or sending out scheduled reports.
How it Works
The Timer trigger uses a CRON expression to define the schedule. When the time specified by the CRON expression is reached, the trigger fires and executes your function. It also provides a TimerInfo object to your function, which contains information about the timer elapsed and whether it's the first run.
Configuration
The Timer trigger is configured using a function.json file. Here's a basic example:
{
"schedule": "0 */5 * * * *",
"type": "timerTrigger",
"direction": "in",
"name": "myTimer"
}
The schedule property accepts a CRON expression. The example above, "0 */5 * * * *", means the function will run every 5 minutes.
CRON Expression Format
Azure Functions uses the following CRON expression format (seconds, minutes, hours, day of month, month, day of week):
* * * * * *
* * * * * *: Every second0 0 12 * * *: Every day at noon0 */5 * * * *: Every 5 minutes0 0 0 1 * *: On the first day of every month at midnight
Important:
The Timer trigger is based on UTC time. Ensure your CRON expressions are set relative to UTC.
TimerInfo Object
When your Timer triggered function executes, it receives a TimerInfo object as a parameter. This object provides two important properties:
isPastDue(boolean): Indicates if the timer has missed one or more scheduled runs. This can happen if your function takes longer to execute than the schedule interval, or if the host is unavailable.next(DateTime): The scheduled time of the next timer run.nextAvailableStartTime(DateTime): The time that the next run can start, considering potential retries and backoffs.
Code Examples
C# Example
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
public static class TimerTriggerCSharp
{
[FunctionName("TimerTriggerCSharp")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.UtcNow}");
if (myTimer.IsPastDue)
{
log.LogInformation("Timer is past due!");
}
log.LogInformation($"Next timer is scheduled at: {myTimer.Next}");
}
}
JavaScript Example
module.exports = async function (context, myTimer) {
const timeStamp = new Date().toISOString();
if (myTimer.isPastDue) {
context.log('JavaScript is running late!');
}
context.log('JavaScript timer trigger function ran!', timeStamp);
context.log('Next timer is scheduled at: ' + myTimer.next);
};
function.json as shown above, and myTimer is passed as an argument to your script.
Python Example
import datetime
import logging
import azure.functions as func
def main(myTimer: func.TimerRequest) -> None:
utc_timestamp = datetime.datetime.utcnow().isoformat()
if myTimer.past_due:
logging.info('The timer is past due!')
logging.info('Python timer trigger function ran at %s', utc_timestamp)
logging.info('Next timer is scheduled at: %s', myTimer.next_run)
function.json, and the timer information is accessed via the func.TimerRequest object.
Tip:
For development and testing, you can use specific CRON expressions to run your timer frequently (e.g., every minute). Remember to change it back to your desired production schedule.
Considerations
- Concurrency: By default, Timer triggers are designed to run once at a time. If a timer job is already running, a new one won't start until the previous one finishes. This helps prevent race conditions and ensures that scheduled tasks are processed reliably.
- Host Availability: If your Azure Functions host is unavailable when a timer event is scheduled, the
isPastDueproperty will be true on the next execution. - Scalability: For very high-frequency or long-running scheduled tasks, consider using other Azure services like Azure Logic Apps or Azure Batch, which are designed for more robust orchestration and parallel processing.
Next Steps
Explore how to integrate Timer triggers with other Azure services, such as Azure Storage for data processing or Azure Cosmos DB for scheduled updates.