In this article, I will explain all about time-triggered functions using Azure Functions App. Before getting into implementation, let’s have an overview of Azure Functions. Azure Functions is a solution for running small lines of code in the Cloud, and we can select the programming language as per our desire. We can build the code that we need at a small level so that we need not worry about the whole application for running it. Azure Functions applications let us develop serverless applications. Now, let us come to the Azure time-triggered functions.
A time-triggered function allows the users to schedule a time for executing the particular function. It means that it will trigger a particular function on the correct scheduled time. As we are speaking of Azure time-triggered functions, it will work based on CRON expressions. When we create a time-triggered function, the user is needed to give time in CRON format which will determine when should the trigger be executed. So, coming to point, CRON expression consists of six parts - second, minute, hour, day, month, and it should be specified in the following expression.
- Asterisk (*) is used to express that the expression can have any value
- Hyphen (-) is used to indicate a range of values
- comma (,) is used in the specification of listing of values
- Forward slash (/) is used to reoccur the execution time.
I have provided the expressions and description on CRON examples listed below,
Expression
|
Description
|
0 * * * * *
|
Every minute
|
0 */2 * * * *
|
Every 2 minute
|
*/1 * * * * *
|
Every second
|
0 0 * * * *
|
Every hour
|
0 0 0 * * *
|
Every day
|
0 30 11 * * *
|
Every day at 11:30:00
|
0 0 5-10 * * *
|
Every hour between 5 to 10
|
0 0 0 * * SAT
|
Every Saturday
|
0 0 0 * * 6
|
Every Saturday
|
0 0 0 * * 1-5
|
Every workday
|
0 0 0 * * SAT, SUN
|
Every Saturday & Sunday
|
0 0 0 1 1 *
|
Every year 1st Jan
|
0 0 0 1-7 * SAT
|
Every first Saturday of month
|
Now, we will be creating a time-triggered function app.
Step 1
Log in to Azure portal using the following
link. After logging into the Azure portal, we can see the dashboard.
For creating an Azure Function app, select "create a resource" >> compute and then select Function App.
Step 2
After that, we need to provide the basic properties, such as a name for the app, and after that, we need to select the subscription. For resource group selection, we can either select the existing resource group or create a new resource group. After that, we need to select the hosting plan and the location for faster access. Select the location where the app must be deployed, after that click "Create".
After successful deployment, now select the created function app in the dashboard and we can see that the function app is running.
Step 3
Now, here comes the next stage of creating a new function. Select New Functions to create a new function app.
Now, after expanding the function app, click the ‘+’ button to select ‘In-portal’ and Click-> Continue.
Next step is now select ->Timer then select->create’.
Step 4
Coming to the next step, it is showing that we need to configure the created new timer function. So, we need to provide a name to the function & we need to provide a scheduled time. Now the scheduled time is in a six fields CRON expression and its default set time is set for 5 minutes for triggering the function then select->Create.
Step 5
After creating the time-triggered function by providing the scheduled time we can see that in logs, the Date-Time value is printed in the console window, but here, I have provided the time and function to be executed in 5 mins.
Step 6
For testing purposes, I have rescheduled a timer for every minute using CRON expression. In this testing, I have printed Date Time value in console window log and you can see the output here.
I hope you have learned something new. Thanks for reading. Please comment below so that I can improve my next article.