Aurelius
Aurelius

Reputation: 112

Is there a way to programmatically restart an azure function

I have an Azure function running on a timer every few minutes that after a varied amount of time of running will begin to fail every time it runs because of an external API and hitting the restart button manually in the azure portal fixes the problem and the job works again.

Is there a way to either get an azure function to restart itself or have something externally restart an azure function via a web hook or API request or running on a timer

I have tried using Azures API Management service which can be used to restart other kinds of app services in azure but it turns out there is no functionality in the API to request a restart of an azure function, Also looked into power shell and it seems to be the same problem you can restart different app services but not azure functions

i have tried working with the API https://learn.microsoft.com/en-us/rest/api/azure/ Example API request where you can list functions within an azure function GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/functions?api-version=2016-08-01

but there is no functionality to restart an azure function from what i have researched

Basically i want to Restart the Azure function as if i was to hit this button Azure functions manual stop/start and restart buttons in azure portal

because there is a case where the job gets into a bad state every time it runs because of an external API i have no control over and hitting restart manually gets the job going again

Upvotes: 11

Views: 13829

Answers (4)

Peter Bons
Peter Bons

Reputation: 29840

You can use the Azure CLI for that:

az functionapp restart --name MyFunctionApp --resource-group MyResourceGroup

You can confirm it does the same as the manual restart button on the portal by inspecting the Activity Log of the function:

enter image description here

Also looked into power shell and it seems to be the same problem you can restart different app services but not azure functions

Nowaday this is possible using PowerShell:

Get-AzFunctionApp -Name MyAppName -ResourceGroupName MyResourceGroupName | Restart-AzFunctionApp -Force

(You could create a PowerShell Azure Function if you want to automate things)

Upvotes: 2

DSpirit
DSpirit

Reputation: 2290

Another way to restart your function is by using the "watchDirectories" setting in the host.json file. If your host.json looks like this:

{
    "version": "2.0",
    "watchDirectories": [ "Toggle" ]
}

You could toggle a restart by using following statement in a function:

System.IO.File.WriteAllText("D:/home/site/wwwroot/Toggle/restart.conf", DateTime.Now.ToString());     

Looking at the logs, the function reloads as it has detected the file change in the directory:

Watched directory change of type 'Changed' detected for 'D:\home\site\wwwroot\Toggle\restart.conf'  
Host configuration has changed. Signaling restart

Upvotes: 5

Inzi
Inzi

Reputation: 346

Why don't you try below ARM API. Since Azure function also fall under App service category, sometimes this may be helpful, https://learn.microsoft.com/en-us/rest/api/appservice/webapps/restart

Upvotes: 0

Murray Foxcroft
Murray Foxcroft

Reputation: 13775

Azure functions by their nature are called upon an event. That may be a timer, a trigger or invocation like a HTTP event. They cannot be restarted per se, i.e. if you a function throws and exception, you cannot find the specific instance and re-run it using the out of the box functionality.

However, you can engineer your way to a more reliable solution:

  1. Replay the event that invoked the function (i.e. kick it off again)
  2. For non-sensitive data, log the payload of the function and create a another function that can be called on demand to re-run it. I.e. you create a proxy to "re-invoke" the function.
  3. Harden your code by implementing a retry policy. See Polly.
  4. Add a service bus in to your architecture. Have a simple function to write the call payload to a message bus payload. Have another function to pick up the payload and process it more extensively where there may be unreliable integrations etc). That way if the call fails you can abandon and dead letter failures for later reprocessing.
  5. Consider using Durable Function Extensions and leveraging the durable patterns, these can help make your functions code more robust and manage state.

Upvotes: 1

Related Questions