Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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://docs.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

like image 294
Aurelius Avatar asked Jan 10 '19 11:01

Aurelius


People also ask

How do I restart Azure function automatically?

Create a logic app and add recurrence trigger. Add time interval,frequency and Time zone according to the time you required to restart your function app.It will trigger the logic app automatically at the time provided in the recurrence. So that Function App will Restart Automatically.

How do I start and stop the function app in Azure?

You have the ability to simply restart the Function App site, which will kill any functions (Function App Settings > Go To App Service Settings > Restart).

How do I manually trigger Azure function?

Navigate to your function app in the Azure portal, select App Keys, and then the _master key. In the Edit key section, copy the key value to your clipboard, and then select OK. After copying the _master key, select Code + Test, and then select Logs.

How do I trigger an Azure function?

Determine which trigger works best for your business needs. Create a timer trigger to invoke a function on a consistent schedule. Create an HTTP trigger to invoke a function when an HTTP request is received. Create a blob trigger to invoke a function when a blob is created or updated in Azure Storage.


1 Answers

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
like image 179
DSpirit Avatar answered Nov 04 '22 18:11

DSpirit