Skip to content

Commit 6c3b3c6

Browse files
Merge pull request #224049 from SnehaSudhirG/16Jan-managerunbooks-docupdate
added new section
2 parents b0d2c1f + 17cc4b2 commit 6c3b3c6

File tree

1 file changed

+95
-1
lines changed

1 file changed

+95
-1
lines changed

articles/automation/manage-runbooks.md

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Manage runbooks in Azure Automation
33
description: This article tells how to manage runbooks in Azure Automation.
44
services: automation
55
ms.subservice: process-automation
6-
ms.date: 11/02/2021
6+
ms.date: 01/16/2022
77
ms.topic: conceptual
88
ms.custom: devx-track-azurepowershell
99
---
@@ -216,6 +216,100 @@ If your runbook normally runs within a time constraint, have the script implemen
216216
> [!NOTE]
217217
> The local time on the Azure sandbox process is set to UTC. Calculations for date and time in your runbooks must take this fact into consideration.
218218
219+
## Retry logic in runbook to avoid transient failures
220+
221+
Runbooks often make calls to remote systems such as Azure via ARM, Azure Resource Graph, SQL services and other web services.
222+
When the system that the runbooks are calling is busy, temporary unavailable or implementing throttling under load, the calls are vulnerable to have runtime errors. To build resiliency in the runbooks, you must implement retry logic when making the calls so that the runbooks can handle a transient problem without failing.
223+
224+
For more information, refer [Retry pattern](https://learn.microsoft.com/azure/architecture/patterns/retry) and [General REST and retry guidelines](https://learn.microsoft.com/azure/architecture/best-practices/retry-service-specific#general-rest-and-retry-guidelines).
225+
226+
### Example 1: If your runbook makes only one or two calls
227+
228+
```powershell
229+
$searchServiceURL = "https://$searchServiceName.search.windows.net"
230+
$resource = Get-AzureRmResource -ResourceType "Microsoft.Search/searchServices" -ResourceGroupName $searchResourceGroupName -ResourceName $searchServiceName -ApiVersion 2015-08-19
231+
$searchAPIKey = (Invoke-AzureRmResourceAction -Action listAdminKeys -ResourceId $resource.ResourceId -ApiVersion 2015-08-19 -Force).PrimaryKey
232+
```
233+
When you call `Invoke-AzureRmResourceAction`, you may observe transient failures. In such scenario, we recommend that you implement the following basic pattern around the call to the cmdlet.
234+
235+
```powershell
236+
$searchServiceURL = "https://$searchServiceName.search.windows.net"
237+
$resource = Get-AzureRmResource -ResourceType "Microsoft.Search/searchServices" -ResourceGroupName $searchResourceGroupName -ResourceName $searchServiceName -ApiVersion 2015-08-19
238+
239+
# Adding in a retry
240+
$Stoploop = $false
241+
$Retrycount = 0
242+
243+
do {
244+
try {
245+
$searchAPIKey = (Invoke-AzureRmResourceAction -Action listAdminKeys -ResourceId $resource.ResourceId -ApiVersion 2015-08-19 -Force).PrimaryKey
246+
write-verbose "Invoke-AzureRmResourceAction on $resource.ResourceId completed"
247+
$Stoploop = $true
248+
}
249+
catch {
250+
if ($Retrycount -gt 3)
251+
{
252+
Write-verbose "Could not Invoke-AzureRmResourceAction on $resource.ResourceId after 3 retrys."
253+
$Stoploop = $true
254+
}
255+
else
256+
{
257+
Write-verbose "Could not Invoke-AzureRmResourceAction on $resource.ResourceId retrying in 30 seconds..."
258+
Start-Sleep -Seconds 30
259+
$Retrycount = $Retrycount + 1
260+
}
261+
}
262+
}
263+
While ($Stoploop -eq $false)
264+
```
265+
>[!NOTE]
266+
>The attempt to retry the call is up to three times, sleeping for 30 seconds each time.
267+
268+
### Example 2 : If the runbook is making frequent remote calls
269+
270+
If the runbook is making frequent remote calls then it could experience transient runtime issues. Create a function that implements the retry logic for each call that is made and pass the call to be made in as a script block to execute.
271+
272+
```powershell
273+
Function ResilientRemoteCall {
274+
275+
param(
276+
$scriptblock
277+
)
278+
279+
$Stoploop = $false
280+
$Retrycount = 0
281+
282+
do {
283+
try {
284+
Invoke-Command -scriptblock $scriptblock
285+
write-verbose "Invoked $scriptblock completed"
286+
$Stoploop = $true
287+
}
288+
catch {
289+
if ($Retrycount -gt 3)
290+
{
291+
Write-verbose "Invoked $scriptblock failed 3 times and we will not try again."
292+
$Stoploop = $true
293+
}
294+
else
295+
{
296+
Write-verbose "Invoked $scriptblock failed retrying in 30 seconds..."
297+
Start-Sleep -Seconds 30
298+
$Retrycount = $Retrycount + 1
299+
}
300+
}
301+
}
302+
While ($Stoploop -eq $false)
303+
}
304+
```
305+
306+
You can then pass each remote call into the function as </br>
307+
308+
`ResilientRemoteCall { Get-AzVm }` </br> or </br>
309+
310+
`ResilientRemoteCall { $searchAPIKey = (Invoke-AzureRmResourceAction -Action listAdminKeys -ResourceId $resource.ResourceId -ApiVersion 2015-08-19 -Force).PrimaryKey}`
311+
312+
219313
## Work with multiple subscriptions
220314

221315
Your runbook must be able to work with [subscriptions](automation-runbook-execution.md#subscriptions). For example, to handle multiple subscriptions, the runbook uses the [Disable-AzContextAutosave](/powershell/module/Az.Accounts/Disable-AzContextAutosave) cmdlet. This cmdlet ensures that the authentication context isn't retrieved from another runbook running in the same sandbox.

0 commit comments

Comments
 (0)