The Timer trigger allows you to run a Function at regular intervals. This is useful for background tasks, scheduled jobs, or periodic data processing.
Timer Trigger Configuration
The Timer trigger is configured using a JSON object in your function.json file. The primary property is schedule, which accepts a CRON expression.
CRON Expressions
CRON expressions are strings that define a schedule. They consist of six fields:
| Field | Description | Allowed Values | Required |
|---|---|---|---|
| Minute | The minute of the hour. | 0-59 | Yes |
| Hour | The hour of the day. | 0-23 | Yes |
| Day of Month | The day of the month. | 1-31 | Yes |
| Month | The month of the year. | 1-12 | Yes |
| Day of Week | The day of the week. | 0-6 (0=Sunday, 1=Monday, ..., 6=Saturday) | Yes |
| Year | The year. | 1970-2099 | Yes |
Special characters include:
*: Wildcard, matches any value.,: Separator, allows multiple values.-: Range, specifies a range of values./: Step, specifies a step value.
Example CRON Expressions
* * * * * *: Runs every minute.0 * * * * *: Runs at the beginning of every hour.0 0 * * * *: Runs at midnight every day.0 0 1 * * *: Runs at midnight on the first day of every month.0 0 0 1 1 *: Runs at midnight on January 1st of every year.
Function.json Configuration
Here's an example of how to configure a Timer trigger in your function.json:
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "mytimer",
"type": "timerTrigger",
"direction": "in",
"schedule": "0 */5 * * * *"
}
]
}
This configuration will trigger the function every 5 minutes.
Timer Information
When the Timer trigger fires, it passes a TimerInfo object to your function. This object contains information about the trigger, such as:
isPastDue: A boolean indicating if the trigger missed a schedule due to system downtime.next: The scheduled time of the next execution.prev: The scheduled time of the previous execution.nextFromEnd: The time of the next execution, measured from the end of the current invocation.
Programming Language Examples
Python
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 executed at: %s', utc_timestamp)
logging.info('Next timer schedule is: %s', mytimer.next)
C#
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
namespace Company.Function
{
public static class TimerTriggerCSharp
{
[FunctionName("TimerTriggerCSharp")]
public static void Run([TimerTrigger("*/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 schedule: {myTimer.ScheduleStatus.Next}");
}
}
}
JavaScript
module.exports = async function (context, myTimer) {
const timeStamp = new Date().toISOString();
if (myTimer.isPastDue) {
context.log('JavaScript timer trigger function is past due!');
}
context.log('JavaScript timer trigger function executed at!', timeStamp);
context.log('Next timer schedule is: ', myTimer.next);
};
Timeouts and Delays
The Timer trigger is designed for running tasks at intervals, not for long-running operations within a single invocation. If your task takes longer than the function app's timeout, it may be terminated. For long-running tasks, consider using other Azure services like Azure Logic Apps or Azure Batch.
Troubleshooting
- Missed Schedules: If you see
isPastDueis true, it means your function app may have been unavailable or the function itself was taking too long to complete. - Incorrect Timing: Double-check your CRON expression and ensure it's configured for UTC.
- Function App Uptime: Ensure your Function App is running and scaled appropriately to handle the scheduled triggers.