34
votes

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 * * * * *"
   }
 ]
}
5
what have you tried? it uses cron syntax, perhaps start there (crontab-generator.org)Mark

5 Answers

59
votes

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)
32
votes

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 * * *"
    }
}
11
votes

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
    }
4
votes

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.

1
votes

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://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=javascript#configuration

function.json local.settings.json