Often as part of an overall solution, you will need scheduled tasks to run on fixed intervals. These tasks are usually administrative, maintenance or integration. There are a lot of ways to achieve this, usually involve deploying windows services that have a scheduler built in like Quartz or using built-in windows task scheduler. One of the major advantage we have in azure functions is server-less architecture, which means no deployment required and no need to pay for a virtual machine to host our service or tasks.
Overview
In this post, I will explain how Azure Functions time trigger works and what are CRON expressions.
This post assumes you have Azure account and you are familiar with Azure Portal
Create Azure Time Trigger Function
Lets start by creating a new time triggered function in azure portal:
- If you don’t have an existing function app, create a new one. please read my earlier post about creating azure function app
- Open your Function App and click “New Function”
- Find and select “Timer Trigger – C#”
- Give it a name and accept the default schedule expression.
Congratulation, you have just created your first time triggered function. However, we skipped over the most important part, “Schedule” Expression. Let us spend sometime explaining what that is in details.
Schedule Expression (Cron Expression)
The origin of the name cron is from the Greek word for time (chronos) from Wikipidia. It expresses time scheduled jobs as a string format often segmented to 6 or 7 fields separated by a space. Let us examine the format with examples to help explain it.
These fields represent the following:
Seconds |
Minutes |
Hours |
Day of month |
Month |
Day of week |
Year (Optional) |
0-59 |
0-59 |
0-23 |
1-31 |
1-12 or JAN-DEC |
0-6 or MON-SUN |
1970-2099 |
Lets look at some basic examples expressions:
Expression |
Behaviour |
0 * * * * * |
Trigger at Zero second on any minute, any hour, any day. Effectively triggering once every 60 seconds |
0 0 * * * * |
Trigger at Zero second and Zero minute on any hour, any day. Effectively triggering once every 60 minutes. Because the occurrence of 0 second and 0 minute happens once an hour |
0 15 * * * * |
Trigger at Zero second and 15th minute, any hour, any day. Effectively triggering once every 60 minutes at the 15th minute mark |
30 30 1 * * * |
Trigger at 30th second and 30th minute and 1st hour, any day. Effectively triggering once a day at 01:30:30 AM |
0 0 0 * MON, TUE, WED,FRI |
Trigger at Zero second and Zero minute and Zero hour, any day of the Month and on MON,TUE, WED, FRI. Effectively triggering at midnight on the days of Monday, Tuesday, Wednesday and Friday |
Let us explore a more useful feature of the expression:
Expression |
Behaviour |
*/1 * * * * * |
Trigger at any second. Effectively triggering every second |
*/30 * * * * * |
Trigger at any 30 seconds Effectively triggering every 30 seconds |
0 */5 * * * * |
Trigger at Zero second and any 5th minutes Effectively triggering at zero second every 5 minutes |
0 30 */6 * * * |
Trigger at Zero second and 30th minute at any six hours Effectively triggering every 6 hours at 30min and zero second |
Let us try some of these expression on our newly created function
Click on “View Files” then Select file named “function.json”, change the schedule property to the expression “0/30 * * * * *”
This should trigger the function every 30 seconds starting from zero seconds.
Conclusion
Azure functions are very flexible and versatile. There are a lot of use-cases were you need to run code on time bases to perform a business function. Some of the common use-cases for time based execution like triggering a backup based on conditions, generate financial statements at the end of the month, send out weekly audit emails, etc
No comments yet.