Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure functions configure timer trigger from Azure App Configuration

I'm doing an Azure Function that is timer triggered. I want to be able to change the timer, i.e the cron expression, without having to do a re-deploy. I'm getting my other settings from an App Configuration in Azure but this doesn't work for the TimerTrigger and I get : An object reference is required for the non-static field, method or property when I write TimerTrigger(config["CronExpression"]) where config is an IConfiguration. Does anyone know how to do this with Azure App Configuration? I wish to not use a settings.json file other than for local development

like image 333
Heden Den Qvisten Avatar asked Nov 21 '19 15:11

Heden Den Qvisten


People also ask

How do I create a timer trigger in Azure function?

Create a timer triggered functionIn your function app, select Functions, and then select + Create. Select the Timer trigger template. Configure the new trigger with the settings as specified in the table below the image, and then select Create. Defines the name of your timer triggered function.

How do I manually trigger a timer in Azure?

Navigate to your function app in the Azure portal, select App Keys, and then the _master key. In the Edit key section, copy the key value to your clipboard, and then select OK. After copying the _master key, select Code + Test, and then select Logs.

How do you trigger a function on Azure app?

On the new function, select the HTTP trigger Azure function template. Now in the next window, Provide a name for the HTTP triggered Azure Function and then choose the Authorization level as function and then click on the Create Function to create the HTTP triggered Azure function.


2 Answers

You can specify the timer expression in your configuration by referencing the name surrounded by %...%. For example, in your configuration create a new value with a name of MyTimerExpression and value of, for example, 0 */10 * * * * to run every 10 minutes. In your local development environment, that means adding an entry into the local.settings.json file like this:

    {
        ...
        "MyTimerExpression": "0 */10 * * * *"
        ...
    }

Now in your timer trigger, do this:

    [TimerTrigger("%MyTimerExpression%")]
like image 92
DavidG Avatar answered Oct 19 '22 00:10

DavidG


Quick Answer

You can specify the timer expression in your configuration by referencing the name surrounded by %...%.

schedule / ScheduleExpression:

A CRON expression or a TimeSpan value. A TimeSpan can be used only for a function app that runs on an App Service Plan. You can put the schedule expression in an app setting and set this property to the app setting name wrapped in % signs, as in this example: "%ScheduleAppSetting%".

~ https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=csharp#configuration

Research

It's not mentioned in the xml doc for the TimerTriggerAttribute https://github.com/Azure/azure-webjobs-sdk-extensions/blob/a34f4909bad85ebdb7777b2fe8a823d879f3c48d/src/WebJobs.Extensions/Extensions/Timers/TimerTriggerAttribute.cs#L21-L24

However, TimerSchedule Create uses a name resolver https://github.com/Azure/azure-webjobs-sdk-extensions/blob/9feb4f2ad6f70e443a036a135b58121c70dbdaf3/src/WebJobs.Extensions/Extensions/Timers/Scheduling/TimerSchedule.cs#L65

... which has the logic in to parse out %...% values https://github.com/Azure/azure-webjobs-sdk/blob/b798412ad74ba97cf2d85487ae8479f277bdd85c/src/Microsoft.Azure.WebJobs.Host/NameResolverExtensions.cs#L56-L72

... which is then used by the DefaultNameResolver to get a value from configuration https://github.com/Azure/azure-webjobs-sdk/blob/aeabc5f43f7c50ca67267cbfa429a08fc68623a9/src/Microsoft.Azure.WebJobs.Host/DefaultNameResolver.cs#L34


Thanks to https://stackoverflow.com/a/58979319

like image 37
Tim Abell Avatar answered Oct 18 '22 23:10

Tim Abell