1

I have developed .Net Core 2.2 Azure Web Job project which is having multiple timer trigger functions. One function is run on every 15 minutes and other one is run on every 10 minutes.

  public async Task GetRecordsFromCosmosDBCollection_0([TimerTrigger("0 0/15 * * * *")]TimerInfo timerInfo){
   //Custom Business Logic
  }
  public async Task GetRecordsFromCosmosDBCollection_1([TimerTrigger("0 0/10 * * * *")]TimerInfo timerInfo){
   //Custom Business Logic
  }

If I used the CRON expression directly in the function parameters then it works as expected. But I want to read the CRON expression information from appsettings.json file and then pass it to the above two functions.

So, can anyone suggest the right approach of reading the CRON expression information from appsettings.json in Functions.cs file in Azure WebJob project.

Pradeep
  • 5,101
  • 14
  • 68
  • 140

1 Answers1

0

I assume the motivation is to get the schedule out of the compiled code where it can be changed without having to re-compile and re-deploy.

This might not be the most elegant solution - but you could put your two functions into two different scheduled WebJobs. Then you could set a separate settings.job for each one.

https://learn.microsoft.com/en-us/azure/app-service/webjobs-create#CreateScheduledCRON

The external settings.job file is deployed to the folder where the WebJob exe is - typically D:\home\site\wwwroot\App_Data\jobs\triggered\WebJobName - and you could change the schedule there.

To have different schedules - you'd have to split into different WebJobs because the settings.job schedule is kicking off Main, as opposed to a specific function like [TimerTrigger].

In a scheduled job, the code in Main would look like this:

    await host.StartAsync();
    await jobHost.CallAsync("ManualTrigger", inputs);
    await host.StopAsync();

where "ManualTrigger" is the function in Functions.cs. The schedule in settings.jobs kicks off Main, runs the function, and then shuts down.

jefftrotman
  • 1,059
  • 7
  • 16
  • I know this way. But I want to pass different CRON expression from appsettings.json file using NameResolver. Because Single WebJob contains the mutilple functions. – Pradeep May 11 '20 at 04:44
  • @pradeep - did you implement? I need functionality as yours. so, can you please let me know the solution if you resolved? I have a webjob which needs to be run on 9pm-6am for Monday-Friday and every hour on Saturday and Sunday. – Surya Mar 16 '22 at 07:26