onefootswill
onefootswill

Reputation: 4097

Unable to Configure Azure Function with Crom Expression from Settings

I've read about how you can use a cron expression from local.settings.json (and presumably from settings on the portal) by using a %% notation in the TimerTrigger parameter.

I'm not having much luck. I added the following to my local.settings.json:

  "Payments": {
    "StripePaymentThirdRetry": {
      "Cron": "0 */2 * * * *"
    }
  }

And then added this to my TimerTrigger parameter:

public async Task RunThird([TimerTrigger("%Payments:StripePaymentThirdRetry:Cron%")] TimerInfo myTimer, MsLogging.ILogger log)

The error I get is:

[2022-07-19T05:59:55.917Z] Microsoft.Azure.WebJobs.Host: Error indexing method 'StripePaymentThirdRetry'. Microsoft.Azure.WebJobs.Host: '%Payments:StripePaymentThirdRetry:Cron%' does not resolve to a value. [2022-07-19T05:59:55.948Z] Error indexing method 'StripePaymentThirdRetry' [2022-07-19T05:59:55.949Z] Microsoft.Azure.WebJobs.Host: Error indexing method 'StripePaymentThirdRetry'. Microsoft.Azure.WebJobs.Host: '%Payments:StripePaymentThirdRetry:Cron%' does not resolve to a value. [2022-07-19T05:59:55.949Z] Function 'StripePaymentThirdRetry' failed indexing and will be disabled. [2022-07-19T05:59:55.982Z] The 'StripePaymentThirdRetry' function is in error: Microsoft.Azure.WebJobs.Host: Error indexing method 'StripePaymentThirdRetry'. Microsoft.Azure.WebJobs.Host: '%Payments:StripePaymentThirdRetry:Cron%' does not resolve to a value. [2022-07-19T05:59:55.982Z] The 'StripePaymentThirdRetry' function is in error: Microsoft.Azure.WebJobs.Host: Error indexing method 'StripePaymentThirdRetry'. Microsoft.Azure.WebJobs.Host: '%Payments:StripePaymentThirdRetry:Cron%' does not resolve to a value.

Any idea what I'm doing wrong. I've seen some suggestion that a function.json file is required, but the doco says that is only for non-compiled languages.

Upvotes: 2

Views: 1127

Answers (2)

anon
anon

Reputation:

According to MS Doc of Azure functions Timer Trigger Cron Schedule, you have to define every attribute in values section like:

local.settings.json:

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "Payments_StripePaymentThirdRetry_cron": "0 */2 * * * *"
      }
}

Code

public static void Run([TimerTrigger("%Payments_StripePaymentThirdRetry_cron%")]TimerInfo myTimer, ILogger log)

Result:

enter image description here

and as per this local.settings.json azure functions official document,

Values must be strings and not JSON objects or arrays. Setting names can't include a double underline (__) and shouldn't include a colon (:). Double underline characters are reserved by the runtime, and the colon is reserved to support dependency injection.

Upvotes: 0

Thomas
Thomas

Reputation: 29726

local.settings.json doesn't really work like a normal appsettings.json. Settings needs to be inside the Values property and it doesn't really like Section. If you use __ it will be able to resolve to the desired setting:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "Payments__StripePaymentThirdRetry__Cron": "0 */2 * * * *"
  }
}

Upvotes: 4

Related Questions