简体   繁体   中英

Azure Function Stops Working After a Few Hours

I have an Azure Function running on a free consumption App Service Plan. It uses a CRON timer that triggers every 5 minutes (0 */5 * * * *).

To confirm this is on a consumption plan (F1: Free).

It is a very simple function that updates my NSG with my Dynamic IP address.

Code wise it executes perfectly every 5 minutes. (I do plan to update the script so it will only update if it detects the IP has changed)

The issue I'm facing is; If I leave it to do it's thing, it eventually stops after an hour or two. I've increased the idle time for the function to 10 minutes in the host.json:

{
  "version": "2.0",
  "managedDependency": {
    "Enabled": true
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[2.*, 3.0.0)"
  },
  "functionTimeout": "00:10:00"
}

Here is the code for the function:

# 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 += $_
}

The workaround I have at the moment is to have a webpage open on an idle VM that auto-refreshes every 2.5 minutes using an extension... This keeps the function working indefinitely; which I noticed worked when trying to troubleshoot it.

I've tried looking at possible reasons this isn't working but the best I have found was the increase in timeout; which hasn't worked.

Here is a link explaining that the service for dynamic functions is external to the app. I get that but even if the app idles out should it not trigger again when the CRON job timer is hit?

The function does NOT hit any free quotas by a long shot... uses about 3/60 minutes compute a day and only runs at 400/1000MB.

I do not have any log analytics enabled at the moment as the function is meant to save me money; but if anyone suggests I should turn it on to troubleshoot I can.

This How come my Azure timer function app stops firing article seems to suggest it might work if I redeploy all the resources; would like to avoid but if no other ideas from the community is crowdsourced I will give it a go.

As you're using the Free Pricing Tier Consumption Plan in Azure Functions and even if it is in Shared App Service Plan , Always On support is not available.

  • If you have an Azure Function App in a Basic/Standard/Premium App Service Plan , Always On is on by default and if it is turn off, you'll get a warning in the Functions User Interface of the Azure Portal.

  • If you are in Pay-As-You-Go Consumption Plan , the system takes Care of waking up your functions whenever they are specified to run, which is the recommended approach for most of the users.

  • Alternative small hack mentioned the search engine is that you can ping the service ( Function URL) every custom time (in seconds/minutes) interval which prevents it from being idle state and that pinging doesn't count towards the CPU run time unless you ping the actual function URL which doesn't affect the time limit .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM