Jordan1993
Jordan1993

Reputation: 822

Azure Functions RunOnStartUp set in configuration rather than at compile time?

I have an Azure timer triggered function scheduled to run every 3 months in production. However in test environment I'd like it to run on start up, every time it is triggered.

At the moment I have:

[TimerTrigger("%TimerInterval%", RunOnStartup = false)]

I don't really want to change the RunonStartup to true but wondered if there's a way of setting this in the configuration?

Is it possible to do something like:

RunOnStartup = "%RunOnStartUpBool%" and set that in appsettings?

Upvotes: 3

Views: 4271

Answers (2)

Troy Witthoeft
Troy Witthoeft

Reputation: 2666

Update 2022-03-30: My previous answer was updating your code to use an #IF Debug pre-processor directive as a way to switch into the RunOnStartup=true method parameter. As of 2022, you can bypass that ungainly workaround and just select an option in the VS Code Azure Functions extension! That seems less complex. There is more information here.

enter image description here

Another alternative would be logging into the Azure portal, navigating to your function app and using the function's Test/Run tab.

OLD ANSWER: There is a good SO question with multiple answers to this same question here.
My test environment is normally my local environment. So if we want to write code that ONLY runs on your local environment and not in production we could use a a preprocessor directive in the middle of the method signature that only sets RunOnStartup=true when you are in the debug mode.

    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)

    {

Explanation: During local development (debug) the #if DEBUG block is activated. It enables the RunOnStartup=true parameter. In production (not debug) the #if DEBUG block is hidden.

Clearly not the prettiest code. But much better than the alternatives ... such as having to wait on the timer trigger interval during dev.

Upvotes: 3

krishg
krishg

Reputation: 6508

You can not set RunOnStartup dynamically from configuration/environment variable at runtime. But you can solve your problem in some other way since your purpose is to trigger it manually at startup (or anytime). You can manually trigger the function by some specialized http call as described below. You can do that let's say from your deployment pipeline of test environment as a post deployment step (or any other means you prefer).

To run a non HTTP-triggered function (like in this case it's timer triggered), you need a way to send a request to Azure to run the function. The URL used to make this request takes a specific form. enter image description here

  • Host name: The function app's public location that is made up from the function app's name plus azurewebsites.net or your custom domain.
  • Folder path: To access non HTTP-triggered functions via an HTTP request, you have to send the request through the folders admin/functions.
  • Function name: The name of the function you want to run.

You use this request location along with the function's "master key" as x-functions-key header in the POST request to Azure to run the function. Note Content-Type header should be set as application/json

For details, refer Manually run a non HTTP-triggered function

Upvotes: 0

Related Questions