Reputation: 112
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
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:
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
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
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
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:
Upvotes: 1