Search code examples
c#azure-webjobsazure-webjobssdktimer-trigger

WebJobs - How to configure TimeTrigger RunOnStartup in appsettings?


The documentation is very clear - set RunOnStartup = false in production. (https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=in-process&pivots=programming-language-csharp#example)

However, how do we do that without 'remembering' to change true to false in the code?

I can't see how to pass a config setting from appsettings.json like you can for the Cron String?

e.g.

public async Task DataRetentionAllTenantsAndAccounts([TimerTrigger("%App:DataRetentionAllTenantsAndAccountsCronTrigger%", RunOnStartup = true)] TimerInfo myTimer, ILogger log)
    {
        Console.WriteLine("Started DataRetentionAllTenantsAndAccounts");
        log.LogInformation("Started DataRetentionAllTenantsAndAccounts");
        await _mailMiloManager.DataRetentionAllTenantsAndAccountsAsync();
    }

Solution

  • Here is the code to set RunOnStartup = True in production environment.

        public static void Run([TimerTrigger("%TimerInterval%" 
    
    #if DEBUG 
    ,RunOnStartup=true // When debugging... run the job as soon as we press debug, no need to wait for the timer. 
    #endif
    
        )]TimerInfo myTimer)
    
        {
    

    This you can do it using Visual Code. check this SO for complete information.