'How to restart a Azure FunctionApp programmatically when an Exception occur

I am very new in Azure FunctionApp. Using Java, I am writing the the function app. My Azure function is generating some xml file on 2 hours interval. The process takes almost 1 hour to generate the xml files to finish the operation.

I observed that, the process got IO Exception in the middle of its process.

In this kind of situation, How to restart the FunctionApp if exception occures.

Please share some sample code.



Solution 1:[1]

Here are few workarounds that you can try:-

WAY - 1

Using watchDirectories setting in the host.json

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

WAY - 2

Using Powershell script by saving inside your catch block. So, whenever the exception occurs the powershell script (sh) file runs and restarts the function app.

Here is the powershell script to restart the function app

Restart-AzFunctionApp
       -Name <String>
       -ResourceGroupName <String>
       [-SubscriptionId <String>]
       [-Force]
       [-DefaultProfile <PSObject>]
       [-PassThru]
       [-Confirm]
       [-WhatIf]
       [<CommonParameters>]

WAY - 3

Using RestAPI

POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/start?api-version=2021-02-01

REFERENCES:

  1. Is there a way to programmatically restart an azure function
  2. Restart using Powershell
  3. Restart using RestAPI

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SwethaKandikonda-MT