Search code examples
powershelltimerazure-functionstimeout

Can I run an Azure Function which needs to run more as 1 hour?


Can I run an scheduled ps Azure Function which needs to run more as 1 hour? I already try consumption and premium plan, but the scheduled Azure function stuck because of a timeout.

Update

I try to get all the group members in AAD. I run this ms graph url:

https://graph.microsoft.com/v1.0/groups?$top=999&$expand=members

But because there are more as 999 groupmembers I need to use the paging functionality. See code snippet below:

$allPages = @()
    $items = (Invoke-RestMethod -Method 'Get' -Uri 'https://graph.microsoft.com/v1.0/groups?$top=999&$expand=members' -Headers $authHeader  -ContentType 'Application/Json') 
    $allPages += $items.value

    $index = 0
    if ($items.'@odata.nextLink') {
            do {
                  $index++
                  "Index counter: $index"

                  $token = (Get-AzAccessToken -ResourceUrl "https://graph.microsoft.com/").Token
                  $authHeader = @{Authorization = "Bearer $token"}

                  $items = (Invoke-RestMethod -Method 'Get' -Uri $items.'@odata.nextLink' -Headers $authHeader  -ContentType 'Application/Json')
                  $allPages += $items.value
             } until (
                 !$items.'@odata.nextLink'
             )
    }

    $items = $allPages
    "Count items: " + $items.Count

    $filePath = "D:\home\data\$($fileName)"

    Write-Host "Convert to csv to path '$($filePath)'" -ForegroundColor green
    $items | Export-Csv -NoTypeInformation -Path $filePath

Solution

  • Can I run an Azure Function which needs to run more as 1 hour?

    yes, you can run the Azure functions more than one hour.

    • In Consumption Plan, default duration is 5minutes and Maximum duration is 10 minutes.
    • In Premium Plan, default duration is 30 minutes. Though maximum duration is technically unbounded, function execution is guaranteed only for 60minutes.
    • So, you can go with Dedicated(App Service) plan which allows to increase the timeout for more than an hour.

    Set the functionTimeout attribute in host.json:

    {
      "functionTimeout":"02:00:00"
    }
    
    • created a PowerShell Azure function and using functionTimeout attribute to set the timeout of the function.
    {
      "version": "2.0",
      "logging": {
        "applicationInsights": {
          "samplingSettings": {
            "isEnabled": true,
            "excludedTypes": "Request"
          }
        }
      },
      "managedDependency": {
        "enabled": true
      },
      "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[4.*, 5.0.0)"
      },
      "functionTimeout":"02:00:00"
    }
    

    Response:

    enter image description here