Lab 4: Create a function that triggers on a schedule
Task 1: Create a schedule-triggered function
- On the taskbar, select the Windows Terminal icon.
Run the following command to change the current directory to the $HOME\training-az204\Labs\02\Starter\func directory:
cd $HOME\training-az204\Labs\02\Starter\func
From the command prompt, run the following command to use the Azure Functions Core Tools to create a new function named Recurring, using the Timer trigger template:
func new --template "Timer trigger" --name "Recurring"
Note: You can review the documentation to [create a new function][azure-functions-core-tools-new-function] using the Azure Functions Core Tools.
Close the currently running Windows Terminal application.
Task 2: Observe function code
- On the Start screen, select the Visual Studio Code tile.
- On the File menu, select Open Folder.
- In the File Explorer window that opens, browse to $HOME\training-az204\Labs\02\Starter\func, and then select Select Folder.
- On the Explorer pane of the Visual Studio Code window, open the Recurring.cs file.
In the code editor, observe the implementation:
using System; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Host; using Microsoft.Extensions.Logging; namespace func { public static class Recurring { [FunctionName("Recurring")] public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log) { log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}"); } } }
Task 3: Observe function runs
- On the taskbar, select the Windows Terminal icon.
Run the following command to change the current directory to the $HOME\training-az204\Labs\02\Starter\func directory:
cd $HOME\training-az204\Labs\02\Starter\func
From the command prompt, run the following command to run the function app project:
func start --build
Note: You can review the documentation to [start the function app project locally][azure-functions-core-tools-start-function] using the Azure Functions Core Tools.
- Observe the function run that occurs about every five minutes. Each function run should render a simple message to the log.
- Close the currently running Windows Terminal application.
Task 4: Update the function integration configuration
- On the Start screen, select the Visual Studio Code tile.
- On the File menu, select Open Folder.
- In the File Explorer window that opens, browse to $HOME\training-az204\Labs\02\Starter\func, and then select Select Folder.
- On the Explorer pane of the Visual Studio Code window, open the Recurring.cs file.
In the code editor, observe the existing Run method signature:
[FunctionName("Recurring")] public void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
Update the Run method signature code block to change the schedule to run once every 30 seconds:
[FunctionName("Recurring")] public void Run([TimerTrigger("*/30 * * * * *")]TimerInfo myTimer, ILogger log)
Select Save to save your changes to the Recurring.cs file.
Task 5: Observe function runs
On the taskbar, select the Windows Terminal icon.
Run the following command to change the current directory to the $HOME\training-az204\Labs\02\Starter\func directory:
cd $HOME\training-az204\Labs\02\Starter\func
From the command prompt, run the following command to run the function app project:
func start --build
Note: You can review the documentation to [start the function app project locally][azure-functions-core-tools-start-function] using the Azure Functions Core Tools.
Observe the function run that occurs about every 30 seconds. Each function run should render a simple message to the log.
Close the currently running Windows Terminal application.
Close the Visual Studio Code window.
Review
In this exercise, you created a function that runs automatically based on a fixed schedule.