有没有办法以编程方式重新启动 azure 函数

Aur*_*ius 7 azure azure-api-management azureportal azure-functions

我有一个 Azure 函数每隔几分钟在计时器上运行一次,在不同的运行时间后,每次运行都会因为外部 API 而开始失败,并且在 azure 门户中手动点击重启按钮可以解决问题和工作再次工作。

有没有办法让 azure 函数重新启动自身,或者通过 web 钩子或 API 请求或在计时器上运行从外部重新启动 azure 函数

我曾尝试使用 Azures API 管理服务,该服务可用于在 azure 中重新启动其他类型的应用程序服务,但结果发现 API 中没有请求重新启动 azure 函数的功能,还查看了 power shell,它似乎是同样的问题,您可以重新启动不同的应用程序服务,但不能重新启动 azure 功能

我曾尝试使用 API https://docs.microsoft.com/en-us/rest/api/azure/ 示例 API 请求,您可以在其中列出 azure 函数中的函数 GET https://management.azure.com/订阅/ {subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/functions?api-version=2016-08-01

但是没有任何功能可以从我研究的内容中重新启动 azure 功能

基本上我想重新启动 Azure 功能,就像我要点击此按钮 Azure 功能手动停止/启动和重新启动 azure 门户中的按钮

因为有一种情况,由于外部 API,作业每次运行时都会进入错误状态,我无法控制并且手动点击重新启动让作业再次运行

DSp*_*rit 5

重新启动函数的另一种方法是使用 host.json 文件中的“watchDirectories”设置。如果你的 host.json 看起来像这样:

{
    "version": "2.0",
    "watchDirectories": [ "Toggle" ]
}
Run Code Online (Sandbox Code Playgroud)

您可以通过在函数中使用以下语句来切换重新启动:

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

查看日志,该函数会重新加载,因为它检测到目录中的文件更改:

Watched directory change of type 'Changed' detected for 'D:\home\site\wwwroot\Toggle\restart.conf'  
Host configuration has changed. Signaling restart
Run Code Online (Sandbox Code Playgroud)


Pet*_*ons 1

您可以使用Azure CLI来执行此操作:

az functionapp restart --name MyFunctionApp --resource-group MyResourceGroup
Run Code Online (Sandbox Code Playgroud)

您可以通过检查该功能的活动日志来确认它与门户上的手动重启按钮的作用相同:

在此输入图像描述

还查看了 power shell,似乎是同样的问题,您可以重新启动不同的应用程序服务,但不能重新启动 azure 功能

现在可以使用 PowerShell 实现这一点:

Get-AzFunctionApp -Name MyAppName -ResourceGroupName MyResourceGroupName | Restart-AzFunctionApp -Force
Run Code Online (Sandbox Code Playgroud)

(如果你想自动化操作,你可以创建一个 PowerShell Azure Function)