Azure automation child runbook timeout error

Raj D 616 Reputation points
2022-12-06T18:34:54.903+00:00

Hi, I'm using the below powershell script to execute child runbook from another runbook and run into timeout errors. How do I work around this issue.

workflow Start-AutomationChildRunbook  
{  
    [OutputType([object])]  
  
    param (  
        [Parameter(Mandatory=$true)]  
        [string]   
        $ChildRunbookName,  
  
        [Parameter(Mandatory=$false)]  
        [hashtable]   
        $ChildRunbookInputParams,  
  
        [Parameter(Mandatory=$true)]  
        [PSCredential]   
        $AzureOrgIdCredential,  
  
 [Parameter(Mandatory=$true)]  
 [string]  
 $AzureSubscriptionName  
  
        [Parameter(Mandatory=$true)]  
        [string]   
        $AutomationAccountName,  
  
        [Parameter(Mandatory=$false)]  
        [boolean]   
        $WaitForJobCompletion = $false,  
  
        [Parameter(Mandatory=$false)]  
        [boolean]   
        $ReturnJobOutput = $false,  
  
        [Parameter(Mandatory=$false)]  
        [int]   
        $JobPollingIntervalInSeconds = 10,  
  
        [Parameter(Mandatory=$false)]  
        [int]   
        $JobPollingTimeoutInSeconds = 600  
    )  
  
   # Determine if parameter values are incompatible  
   if(!$WaitForJobCompletion -and $ReturnJobOutput) {  
       $msg = "The parameters WaitForJobCompletion and ReturnJobOutput must both "  
       $msg += "be true if you want job output returned."  
       throw ($msg)  
   }  
  
    # Connect to Azure so that this runbook can call the Azure cmdlets  
    Add-AzureAccount -Credential $AzureOrgIdCredential | Write-Verbose  
  
    # Select the Azure subscription we will be working against  
    Select-AzureSubscription -SubscriptionName $AzureSubscriptionName | Write-Verbose  
  
    # Assure not null for this param  
    if ($ChildRunbookInputParams -eq $null) { $ChildRunbookInputParams = @{} }  
  
    # Start the child runbook and get the job returned  
    $job = Start-AzureAutomationRunbook `  
                -Name $ChildRunbookName `  
                -Parameters $ChildRunbookInputParams `  
                -AutomationAccountName $AutomationAccountName `  
                -ErrorAction "Stop"  
      
    # Determine if there is a job and if the job output is wanted or not  
    if ($job -eq $null) {  
        # No job was created, so throw an exception  
        throw ("No job was created for runbook: $ChildRunbookName.")  
    }  
    else {  
        # There is a job  
          
        # Log the started runbook’s job id for tracking  
        Write-Verbose "Started runbook: $ChildRunbookName. Job Id: $job.Id"  
          
        if (-not $WaitForJobCompletion) {  
            # Don't wait for the job to finish, just return the job id  
            Write-Output $job.Id  
        }  
        else {  
            # Monitor the job until finish or timeout limit has been reached  
            $maxDateTimeout = InlineScript{(Get-Date).AddSeconds($using:JobPollingTimeoutInSeconds)}  
              
            $doLoop = $true  
              
            while($doLoop) {  
                Start-Sleep -s $JobPollingIntervalInSeconds  
                  
                $job = Get-AzureAutomationJob `  
                    -Id $job.Id `  
                    -AutomationAccountName $AutomationAccountName  
                  
                if ($maxDateTimeout -lt (Get-Date)) {  
                    # timeout limit reached so exception  
                    $msg = "The job for runbook $ChildRunbookName did not "  
                    $msg += "complete within the timeout limit of "  
                    $msg += "$JobPollingTimeoutInSeconds seconds, so polling "  
                    $msg += "for job completion was halted. The job will "  
                    $msg += "continue running, but no job output will be returned."  
                    throw ($msg)  
                }  
                  
                $doLoop = (($job.Status -notmatch "Completed") `  
                          -and ($job.Status -notmatch "Failed") `  
                          -and ($job.Status -notmatch "Suspended") `  
                          -and ($job.Status -notmatch "Stopped"))  
            }  
              
            if ($job.Status -match "Completed") {  
                if ($ReturnJobOutput) {  
                    # Output  
                    $jobout = Get-AzureAutomationJobOutput `  
                                    -Id $job.Id `  
                                    -AutomationAccountName $AutomationAccountName `  
                                    -Stream Output  
                    if ($jobout) {Write-Output $jobout.Text}  
                      
                    # Error  
                    $jobout = Get-AzureAutomationJobOutput `  
                                    -Id $job.Id `  
                                    -AutomationAccountName $AutomationAccountName `  
                                    -Stream Error  
                    if ($jobout) {Write-Error $jobout.Text}  
                      
                    # Warning  
                    $jobout = Get-AzureAutomationJobOutput `  
                                    -Id $job.Id `  
                                    -AutomationAccountName $AutomationAccountName `  
                                    -Stream Warning  
                    if ($jobout) {Write-Warning $jobout.Text}  
                      
                    # Verbose  
                    $jobout = Get-AzureAutomationJobOutput `  
                                    -Id $job.Id `  
                                    -AutomationAccountName $AutomationAccountName `  
                                    -Stream Verbose  
                    if ($jobout) {Write-Verbose $jobout.Text}  
                }  
                else {  
                    # Return the job id  
                    Write-Output $job.Id  
                }  
            }  
            else {  
                # The job did not complete successfully, so throw an exception  
                $msg = "The child runbook job did not complete successfully."  
                $msg += "  Job Status: " + $job.Status + "."  
                $msg += "  Runbook: " + $ChildRunbookName + "."  
                $msg += "  Job Id: " + $job.Id + "."  
                $msg += "  Job Exception: " + $job.Exception  
                throw ($msg)  
            }  
        }  
    }  
}  

Error: The job for runbook did not complete within the timeout limit of 600 seconds, so polling for job completion was halted. The job will continue running, but no job output will be returned.

Code block for error:

if ($maxDateTimeout -lt (Get-Date)) {  
                        # timeout limit reached so exception  
                        $msg = "The job for runbook $ChildRunbookName did not "  
                        $msg += "complete within the timeout limit of "  
                        $msg += "$JobPollingTimeoutInSeconds seconds, so polling "  
                        $msg += "for job completion was halted. The job will "  
                        $msg += "continue running, but no job output will be returned."  
                        throw ($msg)  
                    }  

I'm using hybrid worker group for azure automation runbook execution. I was planning to imply batch execution but not sure how to implement it. Any inputs would be helpful. The parent runbook reads data from a text file and in some scenarios the record count run into millions. How would I pass data in chunks of 25,000 records or so let's say.

Thank you

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,364 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,628 questions
{count} votes

1 answer

Sort by: Most helpful
  1. tbgangav-MSFT 10,426 Reputation points Moderator
    2022-12-13T14:19:25.683+00:00

    Hi @Raj D ,

    I believe the script which you have shared is taken from here. If yes, its very old and is from 2015 and also looks like it doesn't use recommended Az PowerShell cmdlets. So, if your requirement is to execute child runbook from a parent runbook then I would recommend you to try it using Az PowerShell cmdlets as explained here. Also as mentioned here, Hybrid Runbook Workers aren't limited by fair share and don't have a limitation on how long a runbook can execute. These are the other job limits apply to both Azure sandboxes and Hybrid Runbook Workers. While Hybrid Runbook Workers aren't limited by the three-hour fair share limit, you should develop runbooks to run on the workers that support restarts from unexpected local infrastructure issues. Let me know if you have any further queries with regards to it.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.