8

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

  • Possible duplicate of [Dynamically set schedule in Azure Function](https://stackoverflow.com/questions/45564848/dynamically-set-schedule-in-azure-function) – Martin Frøhlich Nov 21 '19 at 16:00

2 Answers2

18

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%")]
Stef Heyenrath
  • 9,335
  • 12
  • 66
  • 121
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • Sorry I was unclear with my question. I want to be able to change the value in Azure and only use the local.settings.json for local development. – Heden Den Qvisten Nov 22 '19 at 08:35
  • The local settings file isn't used in Azure, instead those values come from the configuration section of the Azure Portal. – DavidG Nov 22 '19 at 08:39
  • Ok I didn't know that. That's a good solution. You don't happen to know if instead of using the functions configuration settings if I can can get the value from an App Configuration which I use for several other apps in Azure including this function to get other values? – Heden Den Qvisten Nov 22 '19 at 08:56
  • Not in this scenario no. I believe there is an Azure feature in preview that will solve this, but it's not something I've tried yet. – DavidG Nov 22 '19 at 08:58
  • fyi for future visitors, currently putting the schedule in App Configuration doesn't work with consumption plans, presumably because the trigger sync doesn't know to or can't retrieve the schedule value from App Configuration. – BenV Jul 24 '20 at 13:41
  • @BenV Where did you see/read this? I'm literally running this code in a consumption plan and it works fine. – DavidG Jul 24 '20 at 13:58
  • @DavidG Your setting is in Azure App Configuration and not in your function's app settings? What's the frequency of your schedule? We had it that way and it would only run if the timer interval was less than the consumption plan timeout. With a schedule of every hour, the consumption plan would "go to sleep" after 30 minutes and not wake up on the next hour. Moving the schedule to an app setting fixed it. – BenV Jul 24 '20 at 15:40
  • 1
    Note that you need to add this inside the "Values" section. – Stef Heyenrath Oct 20 '20 at 13:14
8

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

Tim Abell
  • 11,186
  • 8
  • 79
  • 110
  • 2
    Documentation for the schedule attribute where it mentions wrapping it in %% is in https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=in-process&pivots=programming-language-csharp#:~:text=Description-,Schedule,-A%20CRON%20expression – Tony_Henrich Feb 10 '23 at 18:27