我有一个在免费消费应用服务计划上运行的 Azure Function。它使用每 5 分钟触发一次的 CRON 计时器 (0 */5 * * * *)。
确认这是否在消费计划中(F1:免费)。
这是一个非常简单的函数,可以使用我的动态 IP 地址更新我的 NSG。
代码方面,它每 5 分钟完美执行一次。 (我确实计划更新脚本,因此只有在检测到 IP 已更改时才会更新)
我面临的问题是;如果我让它继续做它该做的事情,它最终会在一两个小时后停止。我已在 host.json 中将该函数的空闲时间增加到 10 分钟:
{
"version": "2.0",
"managedDependency": {
"Enabled": true
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[2.*, 3.0.0)"
},
"functionTimeout": "00:10:00"
}
以下是该函数的代码:
# Input bindings are passed in via param block.
param($Timer)
# Get the current universal time in the default string format.
$currentUTCtime = (Get-Date).ToUniversalTime()
# The 'IsPastDue' property is 'true' when the current function invocation is later than scheduled.
if ($Timer.IsPastDue) {
Write-Host "PowerShell timer is running late!"
}
# Write an information log with the current time.
Write-Host "PowerShell timer trigger function started! TIME: $currentUTCtime"
Try{
# Write to the Azure Functions log stream.
Write-Output "PowerShell HTTP trigger function processed a request."
# Interact with query parameters or the body of the request.
Write-Output ($request | ConvertTo-Json -depth 99)
$rawIP = [System.Net.Dns]::GetHostAddresses("MY DYNAMIC IP HOSTNAME")
$ips = $rawIP.IPAddressToString
$nsgName = "NSGNAME"
$resourceGroupName = "resourcegroupname"
$rule_names = @("NSGRULE")
foreach($rule_name in $rule_names)
{
$nsg = Get-AzNetworkSecurityGroup -Name $nsgName -ResourceGroupName $resourceGroupName
$rule= $nsg | Get-AzNetworkSecurityRuleConfig -Name $rule_name
Set-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $nsg `
-Name $rule_name `
-Access $rule.Access `
-Protocol $rule.Protocol `
-Direction $rule.Direction `
-Priority $rule.Priority `
-SourceAddressPrefix $ips `
-SourcePortRange $rule.SourcePortRange `
-DestinationAddressPrefix $rule.DestinationAddressPrefix `
-DestinationPortRange $rule.DestinationPortRange
$nsg | Set-AzNetworkSecurityGroup | out-null
}
}
Catch{
[string]$message += $_
}
我目前的解决方法是在空闲虚拟机上打开一个网页,该虚拟机使用扩展每 2.5 分钟自动刷新一次......这使该功能可以无限期地运行;我在尝试解决问题时发现它有效。
我尝试寻找这不起作用的可能原因,但我发现的最好的原因是超时的增加;这没有起作用。
这里是一个链接,解释动态函数的服务是应用程序外部的。我明白了,但即使应用程序空闲,当 CRON 作业计时器被命中时,它也不会再次触发吗?
该函数根本不会达到任何免费配额...每天大约使用 3/60 分钟的计算,并且仅以 400/1000MB 的速度运行。
我目前没有启用任何日志分析,因为该功能是为了帮我省钱;但如果有人建议我应该打开它来排除故障,我可以。
这个How come my Azure timer function app stops firing文章似乎表明,如果我重新部署所有资源,它可能会起作用;我想避免,但如果社区没有其他想法是众包的,我会尝试一下。
最佳答案
当您在 Azure Functions 中使用免费定价层消费计划时,即使它位于共享应用服务计划中>,不提供始终在线支持。
如果您在基本/标准/高级应用服务计划中拥有 Azure Function 应用,则默认情况下“始终开启”处于开启状态,如果将其关闭,您将收到警告在 Azure 门户的函数用户界面中。
如果您采用即用即付消费计划,系统会在指定运行时唤醒您的函数,即 <为大多数用户推荐的方法。
提到搜索引擎的另一种小技巧是您可以
ping
服务(Function URL)
每个自定义时间
(以秒/分钟为单位)间隔
,防止其空闲 状态,并且 ping 不会计入 CPU 运行时间,除非您 ping 实际函数 URL(这不会影响时间限制)。
关于Azure 函数在几个小时后停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71825678/