58

I am working on Azure functions timer Job , i need to get the cron expression from the appsettings. Please let me know, how can i get the value from the appsettings in the Azure functions. I want to run my azure function starting from 9:00 AM to 12:00 PM for every 30 minutes\

{
 "disabled": false,
 "bindings": [
   {
     "name": "timerInfo",
     "type": "timerTrigger",
     "direction": "in",
     "schedule": "0 * * * * *"
   }
 ]
}
Karthikeyan VK
  • 5,310
  • 3
  • 37
  • 50
Palanivelu Samudi
  • 1,125
  • 1
  • 10
  • 13
  • what have you tried? it uses cron syntax, perhaps start there (https://crontab-generator.org/) – Mark Aug 07 '17 at 03:26

6 Answers6

90

Set your schedule as "schedule": "%EmailScheduleTriggerTime%" and then in the appsetting.json or local.settings.json you can set EmailScheduleTriggerTime value as "0 30 9-12 * * *"

{
  "IsEncrypted": false,
  "Values": {
    "EmailScheduleTriggerTime": "0 30 9-12 * * *", //Run every  30 minutes from 9:00 to 12:00

  },
  "ConnectionStrings": {
    "DefaultConnection": ""
  }
}

[FunctionName("TimerfunctionApp")] 
public static void Run([TimerTrigger("%EmailScheduleTriggerTime%")] TimerInfo TInfo, TraceWriter log)
Karthikeyan VK
  • 5,310
  • 3
  • 37
  • 50
  • 1
    can you point me to the official docs? couldn't find this there – jayasurya_j Sep 24 '18 at 07:08
  • 1
    Is that what you are asking for https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer#cron-expressions – Karthikeyan VK Sep 24 '18 at 09:01
  • i meant where did you find in docs that says %variable% will be replaced with the environment variable 'variable' – jayasurya_j Sep 24 '18 at 09:50
  • 2
    Here it is: https://learn.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings#binding-expressions---app-settings – Benny Bauer Nov 19 '18 at 07:55
  • 13
    Did anyone make it work locally? When I specify a setting in local.settings.json, I get an error: `Microsoft.Azure.WebJobs.Host: Error indexing method 'MainTriggerEntry'. Microsoft.Azure.WebJobs.Host: '%CRON_EXPRESSION%' does not resolve to a value.` – Denis Molodtsov Mar 26 '19 at 20:26
  • In this case will EmailScheduleTriggerTime value be used only during installation of Azure function or it will be possible to edit schedule in runtime? – alex Nov 13 '19 at 12:00
  • @DenisMolodtsov Yes i am getting that error,so the way I resolved it was by adding the cron expression in the local.settings.json file and that did the trick and got me working locally – Bug Hunter Zoro Apr 06 '20 at 14:17
  • I had this issue because I didn't include the setting under the "Values" key in my local.settings.json. That resolved the issue @DenisMolodtsov mentioned. – José Mancharo Jul 01 '21 at 17:45
  • re `%FOO% does not resolve to a value`: when adding the value to appsettings.json I had to add it to the `root` of the file, **not** as a sub element of `Values` (azure functions 4.0). – BatteryBackupUnit Nov 17 '21 at 10:14
  • For this to work the names must match, and if it's under Values there must only be pairs of strings. You cannot have any arrays or objects in Values or it will fail for all of them – Red Dec 08 '21 at 10:51
  • Has anyone done this same thing with Java Azure Functions? I'm looking for whether my Java Azure function TimerTrigger can be configured from ApplicationSettings. Thanks! – bachman Aug 23 '22 at 14:46
  • Can somebody paste working code? – kudlatiger Dec 16 '22 at 05:25
47

If you are using the VS2017 Functions tooling and defining your function in a .NET project (rather than directly in the Azure portal) you can pick up the interval from AppSettings using the % syntax:

[FunctionName("MyTimerFunction")] 
public static void Run([TimerTrigger("%TimerInterval%")] TimerInfo myTimer, TraceWriter log, ..

Then in your app settings specify the required CRON format interval eg. in local.settings.json

{
  "Values" : { 
      "TimerInterval" : "0 30 9-12 * * *"
    }
}
Karthikeyan VK
  • 5,310
  • 3
  • 37
  • 50
Garth Mason
  • 7,611
  • 3
  • 30
  • 39
  • 1
    Why the downvotes? Happy to improve this answer if there is some feedback. – Garth Mason Aug 08 '17 at 00:53
  • Yours is only part of the answer use 0 30 9-12 * * *, i have edited and send a review. – Karthikeyan VK Aug 08 '17 at 05:19
  • Is it possible to use other settings like this inside the function? I mean just putting the parametere name in %'s? I'm not able to get it working. Can only TimerTrigger use the settings this way? – Kuczi Jun 19 '18 at 08:23
  • Unfortunately this does not work when you deploy the function to Azure. `Error indexing method 'SampleFunction.Run' '%TimerInterval%' does not resolve to a value.` local.settings.json is deployed along with function. – Viktors Telle Sep 04 '18 at 11:34
  • 1
    The workaround is to set "TimerInterval" value in Application settings of Azure Function in Azure Portal, but I would like this value to be configurable during the deployment. – Viktors Telle Sep 04 '18 at 11:40
  • 1
    Found the solution here: https://octopus.com/blog/azure-functions#step-2-update-appsettings – Viktors Telle Sep 04 '18 at 13:34
  • `Values` contains List>, so you can only provide strings, not arrays or objects. `TimerInterval` is not a special word, and works purely because it matches. A more realistic scenario is `"Job1": "0 */2 * * * *", Job2": "0 */5 * * * *"` – Red Dec 08 '21 at 10:49
31

To add to the previous answers, you can get any value form any field in a config file (appsettings.json) file using % syntax - not only from Values configuration object.

For example:

appsettings.json:

{      
  "ScheduleConfiguration": {
    "FunctionOne": {
      "CronExpression": "0 40 2 * * *"
    }
  }
}

Functions.cs:

    /// <summary>
    /// %ScheduleConfiguration:FunctionOne:CronExpression%
    ///  - e.g. "0 40 2 * * *" - executes at 02:40:00am every day
    /// </summary>
    [FunctionName("FunctionOne")]
    public async Task FunctionOne(
        [TimerTrigger("%ScheduleConfiguration:FunctionOne:CronExpression%")]
        TimerInfo timerInfo)
    {
        // Azure function code
    }
Dmitry Karpenko
  • 544
  • 5
  • 6
  • 2
    According to https://stackoverflow.com/a/45680677/10245 `appsettings.json` has been renamed to `local.settings.json` to make it clearer that it **only applies to local development** – Tim Abell Jan 08 '21 at 15:17
  • 1
    Use double underscore '__' instead of ':' incase you want to deploy to Linux. Windows will understand both – Pieter Jan 13 '23 at 09:06
  • Sadly, it will not work with appsettings.json even when I explicitly add it to .NET config builder .AddJsonFile("appsettings.json"). I often have lots of other logic settings that I want to deliver to prod also, so I include this file with default values, the same way as I do for Web APIs. But it seems, functions do not load their parameter values from .NET config, so only environment variables will work. Is there any other way to get this time trigger value to prod in a config file and not explicit environment setting? – JustAMartin Jan 31 '23 at 14:43
6

Since this post already has good answers, I want to share my experience here. I was trying to run the app locally by adding the cron expression in the appsettings.json file but then when I ran the function app I always received an error as follows

The 'EmployeeTimerTrigger' function is in error: Microsoft.Azure.WebJobs.Host: Error indexing method 'EmployeeTimerTrigger'. Microsoft.Azure.WebJobs.Host: '%EmployeeTimerTrigger%' does not resolve to a value.

So in order to resolve that what we need to do is shift the cron expression from appsettings.json to local.settings.json and it worked just fine and I was able to test it locally.

Edit: Adding notes for deployment

The function app reads the timer trigger settings from the application settings of the function app itself on azure.

When you plan to deploy your function app in azure there's a small change that you need to do in the Application Settings of the function app in the Azure portal.

You can navigate to those settings by selecting the function app in azure portal -> Configuration -> Application setting

The very first setting that you need to add is the time zone in which you want the timer trigger to run here's an example of the same

Note: Please note these values are case sensitive

Key = WEBSITE_TIME_ZONE

Now the second setting we will add here is for our timer trigger function, in this image you will see that the timer will run at 10:00 AM and 2:00 PM eastern standard time.

Why eastern standard time you ask? well, remember our first settings were for the website time zone so both settings work hand in hand.

Note: The name of the timer trigger should match the one you have in your function app, please note these values are case sensitive and should match exactly with what you have in your timer trigger function.

enter image description here

Once you have added these settings, don't forget to save your changes and now you are all set to deploy your function app.

I hope this answer helps in the deployment and running the timer function locally.

Bug Hunter Zoro
  • 1,743
  • 18
  • 21
  • The change you suggest means the value isn't used in production (if my limited understanding is correct) – Tim Abell Jan 08 '21 at 14:53
  • Hello @TimAbell I have added the deployment steps to my answer, please review the same and let me know if it helps. Please do upvote if this was helpful :) – Bug Hunter Zoro Jan 08 '21 at 17:56
  • 1
    Yeah I'd say that clarifies things, thanks – Tim Abell Jan 11 '21 at 09:28
  • Yeah, it will not work with appsettings.json even when I explicitly add it to .NET config builder `.AddJsonFile("appsettings.json")`. I often have lots of other logic settings that I want to deliver to prod also, so I include this file with default values, the same way as I do for Web APIs. But it seems, functions do not load their parameter values from .NET config, so only environment variables will work. Is there any other way to get this time trigger value to prod in a config file and not explicit environment setting? – JustAMartin Jan 31 '23 at 14:25
  • One more important thing: if the name of your cron expression contains __ (as we often have to support both Linux and Windows), then don't forget to replace __ with : in your trigger function definition. For example, `My__Cron__Timer` in web app config (and in local.settings.json Values) should be referred to as `My:Cron:Timer` in your function. – JustAMartin Feb 06 '23 at 10:22
1

As mentioned earlier (for Node js) we can use %scheduleValue% in function.json and use scheduleValue as parameter in local.settings.json and here its mentioned in Microsoft docs - https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=javascript#configuration

function.json local.settings.json

Arjit Sharma
  • 416
  • 5
  • 15
-1

Refer the below Microsoft doc Timer trigger for Azure Functions

Sample Json Config :

{
    "schedule": "0 */5 * * * *",
    "name": "myTimer",
    "type": "timerTrigger",
    "direction": "in"
}

C# code :

public static void Run(TimerInfo myTimer, ILogger log)
{
    if (myTimer.IsPastDue)
    {
        log.LogInformation("Timer is running late!");
    }
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}" );  
}

Source of code : Microsoft website