Understanding Azure Function NCRONTAB Expressions

Advertisements
Advertisements

Azure Functions provide a serverless compute service that enables you to run code on-demand without having to explicitly provision or manage infrastructure.

One of the key features of Azure Functions is the ability to schedule function execution using NCRONTAB expressions. NCRONTAB expressions are used to define the schedule for when a function should be executed.

What are NCRONTAB Expressions?

NCRONTAB is a format for specifying a schedule, similar to the familiar cron format. It allows you to define the schedule for when a function should be triggered by specifying the minutes, hours, days, months, and days of the week. NCRONTAB expressions follow the pattern:

{second} {minute} {hour} {day} {month} {day-of-week}

Where each field can contain either a specific value, a range of values, a wildcard (*), or a combination of these.

Code Examples

Let’s take a look at some code examples to understand how NCRONTAB expressions work in Azure Functions.

Schedule a Function to Run Every Hour

[FunctionName("HourlyFunction")]
public static void Run([TimerTrigger("0 * * * * *")]TimerInfo myTimer, ILogger log)
{
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}

In this example, the TimerTrigger attribute is used with the NCRONTAB expression "0 * * * * *" to specify that the function should be triggered at the start of every hour.

Schedule a Function to Run Every Monday at 8 AM

[FunctionName("WeeklyFunction")]
public static void Run([TimerTrigger("0 0 8 * * 1")]TimerInfo myTimer, ILogger log)
{
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}

In this example, the TimerTrigger attribute is used with the NCRONTAB expression "0 0 8 * * 1" to specify that the function should be triggered at 8 AM every Monday.

Schedule a Function to Run Every Day at 2 PM

[FunctionName("DailyFunction")]
public static void Run([TimerTrigger("0 0 14 * * *")]TimerInfo myTimer, ILogger log)
{
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}

In this example, the TimerTrigger attribute is used with the NCRONTAB expression "0 0 14 * * *" to specify that the function should be triggered at 2 PM every day.

Schedule a Function to Run on the 15th of Every Month at 10 PM

[FunctionName("MonthlyFunction")]
public static void Run([TimerTrigger("0 0 22 15 * *")]TimerInfo myTimer, ILogger log)
{
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}

In this example, the TimerTrigger attribute is used with the NCRONTAB expression "0 0 22 15 * *" to specify that the function should be triggered at 10 PM on the 15th of every month.

Conclusion

NCRONTAB expressions provide a flexible and powerful way to schedule the execution of Azure Functions. By understanding and using NCRONTAB expressions effectively, you can automate the execution of your functions according to your specific business needs.

Stay tuned for more Azure Functions tips and tricks!

Advertisements
Advertisements
Advertisements

Leave a comment

Create a website or blog at WordPress.com