The original title of this post was suppose to be “Strategy on Running Runbooks in Azure Automation” and wanted to write it for more than a month. Why the title changed during this period? Simple – there was some limitation on running runbooks in Azure Automation and that limitation was significantly increased which pushed me to change the title. Let me explain in detail what was the limitation, what has happened and what is that strategy I had that you might still need and it is always good to implement.
So Azure Automation had this limitation that if one runbook is running for more than 30 mins it will restart the job and it will start either from the beginning of the runbook or from the last checkpoint you’ve made in it. So those 30 mins were increased to 3 hours. I’ve found that just before writing this article. I always verify sources before writing blog post. A month ago this limitation was 30 mins and I know because I’ve hit it. I was a little bit busy so couldn’t write blog post on it right away and after a month things has changed. This limitation is described over here under Fair Share section.
But still with 3 hours I guess in some extreme cases you can still hit that limit. My proposal on this is to have one root runbook that will start other child runbooks one after another. In the child runbook you will have tasks that you will make sure they run under 3 hours or the tasks you will execute in them can be asynchronous. Usually asynchronous tasks return a job ID that you can query every 5 seconds for example to check the status. Before doing the check you can create a checkpoint (Checkpoint-Workflow ) in your runbook so in case the runbook takes longer than 3 hours and it is restarted it will continue right from your last safe point. In fact your root runbook you can use the same approach of starting child runbooks, getting their job ID, making a checkpoint and waiting for the status of the job. There is a good example on starting a runbook and checking its job status here.
Here is sample code from me in order to get the logic:
workflow ExampleRoot
{
param (
[Parameter(Mandatory=$true)][string]$Param1
)
$Creds = Get-AutomationPSCredential -Name ‘Creds’
########################### RUNBOOK 1 #############################################################
Connect to Azure Subscription
$AzureAccount = Add-AzureAccount -Credential $Creds
$job1 = Start-AzureAutomationRunbook -AutomationAccountName CloudAdmin -Name "ExampleChild1"
-Parameters $param1
$Creds = $null
#Create Checkpoint
Checkpoint-Workflow
$Creds = Get-AutomationPSCredential -Name ‘Creds’
Connect to Azure Subscription
$AzureAccount = Add-AzureAccount -Credential $Creds
$doLoop1 = $true
While ($doLoop1)
{
$job1 = Get-AzureAutomationJob -AutomationAccountName CloudAdmin `
-Id $job1.Id
$status1 = $job1.Status
$doLoop1 = (($status1 -ne “Completed”) -and ($status1 -ne “Failed”) -and ($status1 -ne “Suspended”) -and ($status1 -ne “Stopped”))
}
$Output1 = Get-AzureAutomationJobOutput -AutomationAccountName CloudAdmin-Id $job1.Id
-Stream Output
$RemovedAzureAccount = Remove-AzureAccount -Name $AzureAccount.ID -Force
-WarningAction SilentlyContinue
########################### RUNBOOK 2 #############################################################
Connect to Azure Subscription
$AzureAccount = Add-AzureAccount -Credential $Creds
$job2 = Start-AzureAutomationRunbook -AutomationAccountName CloudAdmin -Name "ExampleChild1"
-Parameters $param1
$Creds = $null
#Create Checkpoint
Checkpoint-Workflow
$Creds = Get-AutomationPSCredential -Name ‘Creds’
# Connect to Azure Subscription
$AzureAccount = Add-AzureAccount -Credential $Creds
$doLoop2 = $true
While ($doLoop2)
{
$job2 = Get-AzureAutomationJob -AutomationAccountName CloudAdmin`
-Id $job2.Id
$status2 = $job2.Status
$doLoop2 = (($status2 -ne “Completed”) -and ($status2 -ne “Failed”) -and ($status2 -ne “Suspended”) -and ($status2 -ne “Stopped”))
}
$Output2 = Get-AzureAutomationJobOutput -AutomationAccountName CloudAdmin -Id $job2.Id
-Stream Output
$RemovedAzureAccount = Remove-AzureAccount -Name $AzureAccount.ID -Force
-WarningAction SilentlyContinue
}
Logically this would look something like this:
Now there is one tip I want to share here. You will notice that before making checkpoint I am clearing the credentials variable I am using by this: $Creds= $null. Currently the checkpoint cannot handle credentials and your runbook you will probably fail if you do not clear all your credential variables you are using before making a checkpoint. This issue is described here. After clearing the credentials, making the checkpoint you can get your credentials again from Assets. Example:
$Creds = $null
#Create Checkpoint
Checkpoint-Workflow
$Creds = Get-AutomationPSCredential -Name ‘Creds’
If you have more than one credentials you are using obviously you will need to clear every one of them and get them again after the checkpoint.
Whether Azure Automation has that time limit or not it is always good to make checkpoints after certain actions. This will make your runbook better.
I hope this was helpful.