DEV Community

Paul Michaels
Paul Michaels

Posted on

2 2

Creating a Scheduled Azure Function

I've previously written about creating Azure functions. I've also written about how to react to service bus queues. In this post, I wanted to cover creating a scheduled function. Basically, these allow you to create a scheduled task that executes at a given interval, or at a given time.

Timer Trigger

The first thing to do is create a function with a type of Timer Trigger:

Schedule / CRON format

The next thing is to understand the schedule, or CRON, format. The format is:

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

Scheduled Intervals

The example you'll see when you create this looks like this:

0 */5 * * * *
Enter fullscreen mode Exit fullscreen mode

The notation */[number] means once every number; so */5 would mean once every 5... and then look at the placeholder to work out 5 what; in this case it means once every 5 minutes. So, for example:

*/10 * * * * *
Enter fullscreen mode Exit fullscreen mode

Would be once every 10 seconds.

Scheduled Times

Specifying numbers means the schedule will execute at that time; so:

0 0 0 * * *
Enter fullscreen mode Exit fullscreen mode

Would execute every time the hour, minute and second all hit zero - so once per day at midnight; and:

0 * * * * *
Enter fullscreen mode Exit fullscreen mode

Would execute every time the second hits zero - so once per minute; and:

0 0 * * * 1
Enter fullscreen mode Exit fullscreen mode

Would execute once per hour on a Monday (as the last placeholder is the day of the week).

Time constraints

These can be specified in any column in the format [lower bound]-[upper bound], and they restrict the timer to a range of values; for example:

0 */20 5-10 * * *
Enter fullscreen mode Exit fullscreen mode

Means every 20 minutes between 5 and 10am (as you can see, the different types can be used in conjunction).

Asterisks (*)

You'll notice above that there are asterisks in every placeholder that a value has not been specified. The askerisk signifies that the schedule will execute at every interval within that placeholder; for example:

* * * * * *
Enter fullscreen mode Exit fullscreen mode

Means every second; and:

0 * * * * *
Enter fullscreen mode Exit fullscreen mode

Means every minute.

Back to the function

Upon starting, the function will detail when the next several executions will take place:

But what if you don't know what the schedule will be at compile time. As with many of the variables in an Azure Function, you can simply substitute the value for a placeholder:

[FunctionName("MyFunc")]
public static void Run([TimerTrigger("%schedule%")]TimerInfo myTimer, TraceWriter log)
{
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
}
Enter fullscreen mode Exit fullscreen mode

This value can then be provided inside the local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProto . . .",
    "AzureWebJobsDashboard": "DefaultEndpointsProto . . .",
    "schedule": "0 * * * * *"
  }
}
Enter fullscreen mode Exit fullscreen mode

References

https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer

http://cronexpressiondescriptor.azurewebsites.net/?expression=1+*+*+*+*+*&locale=en

Originally posted here.

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay