Learn how to create an Azure Function that is triggered based on a timer.
- [Instructor] Once your function is created, you will immediately be brought to the dash board for the function app. Notice in the upper left hand corner that it says, JLmyAzurefunction, that's the name we gave it. And then there's a list of functions proxies and slots, all of which are empty under the name of our function. We're going to start by creating a new azure function that will be set off or run in response to a timer. To create a new function within our MyAzure function, we're going to click, create new, next to functions.
And notice that it provides us a set of choices for scenarios. We want a timer function and now we can choose our language, in this case I'm going to choose C sharp and then create the function. It creates a simple example and you can see in the code, that what's going to be run each time this is run, is a information line to the log, indicating the time that the timer triggered this function.
The entry point for an azure function is the run method and trace writer is used to write to log files. If we come down and click on integrate, we see that there is a schedule for our timer. This uses cron, and you can get a great idea of how to use cron by going to cron tab guru.
cron tab guru will show you the synthax for creating different intervals. So for example, if we change this five to a seven, it's going to change it to 407. If we change this star to a two, it's going to be on any day of the month with a two in it, that's a little silly. Let's change the month to one, now it's going to be any day in January.
Let's change that two back to a star, now it's in January and we can put here for day a three that'll be Wednesday, any Wednesday in January. So you can see how this works, it's worth studying the values crontab.guru to get a sense of how all of this works. The one difference will be that instead of five values, we're going to be working with six, where that extra value is seconds.
So let's come back to our function, and right now it says zero seconds and every five minutes. We want this to run much more frequently than that. Let's make it run every second, so we'll come back to this first entry and say star over one and remember to put a space between each of the entries. This will now run every one second, let's click save, we'll return to the function.
Notice by the way if this function is named run.csx. Note that extension, that is an extension specifically for azure functions. It will be rebuilt each time you click save and then we'll click run. And in the log we can see that it was triggered every second and that it's completing successfully each time that it is triggered.
If the log does not show and the log files can be finicky, there's another way to get at this information. You can access these logs from hudu and we'll take a look at that next.
Released
10/12/2017- Examining and personalizing the Azure portal
- Creating an app service
- Creating an ASP.NET application with Visual Studio
- Viewing your published app
- Creating a virtual machine
- Triggering Azure functions
Share this video
Embed this video
Video: Azure function example: Timer