layout | title | nav_order |
---|---|---|
default |
Task 3 |
3 |
Another use case for Azure Functions is when you have a job that needs to run on a schedule. Once a week, once a month og even once every five minutes. In this task, you are to create a function that prints a summary of the ratings on the pizza site every 5 minute.
- Using the same project as previous steps: navigate to the Azure functions extension and click the "Create new function.." button
-
When prompted for configurations enter:
Template for function: TimerTrigger
Name: ScheduledJob
Namespace: LearningFunctions.TimerTrigger
Cron Expression (every 5 minute): 0 */5 * * * *
-
If prompted to create a storage account when debugging, click "use local emulator"
The timer trigger is now generated in your solution folder in "ScheduledJob.cs":
[Function("ScheduledJob")]
public static void Run([TimerTrigger("0 */5 * * * *")] MyInfo myTimer, FunctionContext context)
{
var logger = context.GetLogger("ScheduledJob");
logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
logger.LogInformation($"Next timer schedule at: {myTimer.ScheduleStatus.Next}");
}
To test the function, run func start
from cmd / terminal in the solution folder. The output should
[FunctionName("ScheduledJob")]
public static async Task Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
{
var endpoint = "https://apipizza.azurewebsites.net/ratings/ab2a204f-c5f1-424b-85e2-d8d7ac4730cc";
var httpClient = new HttpClient();
var result = await httpClient.GetStringAsync(endpoint + "?min=5");
log.LogInformation($"Result: {result}");
}
(Replace {sessionCode} in the endpoint variable with your session code from https://pizzaapp.z1.web.core.windows.net/)
5 minutes is a long time to wait for your function to trigger. Are you abel to modify the cron job to trigger every 5 second?