Skip to content

Commit 242d07a

Browse files
committed
added new section
1 parent 0c06798 commit 242d07a

File tree

1 file changed

+100
-1
lines changed

1 file changed

+100
-1
lines changed

articles/automation/manage-runbooks.md

Lines changed: 100 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,105 @@ 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+
As part of the core logic, most runbooks make calls to the remote systems. For example, the calls are to:
222+
- Azure via ARM
223+
- Azure Resource Graph
224+
- Other web services
225+
- SQL Services
226+
227+
When the system that the runbooks are calling are busy, temporary unavailable or implementing throttling under load, the calls are vulnerable to have runtime errors. To build resiliency into the runbooks, you must implement the retry logic while making the calls so that the runbooks can handle a transient problem without failing.
228+
229+
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).
230+
231+
### Example 1: If your runbook makes only one or two calls
232+
233+
```powershell
234+
$searchServiceURL = "https://$searchServiceName.search.windows.net"
235+
$resource = Get-AzureRmResource -ResourceType "Microsoft.Search/searchServices" -ResourceGroupName $searchResourceGroupName -ResourceName $searchServiceName -ApiVersion 2015-08-19
236+
$searchAPIKey = (Invoke-AzureRmResourceAction -Action listAdminKeys -ResourceId $resource.ResourceId -ApiVersion 2015-08-19 -Force).PrimaryKey
237+
```
238+
When you call `Invoke-AzureRmResourceAction`, transient failures are observed. In such scenario, we recommend that you implement the following basic pattern around the call to the cmdlet.
239+
240+
```powershell
241+
$searchServiceURL = "https://$searchServiceName.search.windows.net"
242+
$resource = Get-AzureRmResource -ResourceType "Microsoft.Search/searchServices" -ResourceGroupName $searchResourceGroupName -ResourceName $searchServiceName -ApiVersion 2015-08-19
243+
244+
# Adding in a retry
245+
$Stoploop = $false
246+
$Retrycount = 0
247+
248+
do {
249+
try {
250+
$searchAPIKey = (Invoke-AzureRmResourceAction -Action listAdminKeys -ResourceId $resource.ResourceId -ApiVersion 2015-08-19 -Force).PrimaryKey
251+
write-verbose "Invoke-AzureRmResourceAction on $resource.ResourceId completed"
252+
$Stoploop = $true
253+
}
254+
catch {
255+
if ($Retrycount -gt 3)
256+
{
257+
Write-verbose "Could not Invoke-AzureRmResourceAction on $resource.ResourceId after 3 retrys."
258+
$Stoploop = $true
259+
}
260+
else
261+
{
262+
Write-verbose "Could not Invoke-AzureRmResourceAction on $resource.ResourceId retrying in 30 seconds..."
263+
Start-Sleep -Seconds 30
264+
$Retrycount = $Retrycount + 1
265+
}
266+
}
267+
}
268+
While ($Stoploop -eq $false)
269+
```
270+
>[!NOTE]
271+
>The attempt to retry the call is up to three times, sleeping for 30 seconds each time.
272+
273+
### Example 2 : If the runbook is making frequent remote calls
274+
275+
If the runbook is making frequent remote calls that it could experience transient runtime issues, then create a function that will implement this logic for each call that is made and pass the call to be made in as a script block to execute.
276+
277+
```powershell
278+
Function ResilientRemoteCall {
279+
280+
param(
281+
$scriptblock
282+
)
283+
284+
$Stoploop = $false
285+
$Retrycount = 0
286+
287+
do {
288+
try {
289+
Invoke-Command -scriptblock $scriptblock
290+
write-verbose "Invoked $scriptblock completed"
291+
$Stoploop = $true
292+
}
293+
catch {
294+
if ($Retrycount -gt 3)
295+
{
296+
Write-verbose "Invoked $scriptblock failed 3 times and we will not try again."
297+
$Stoploop = $true
298+
}
299+
else
300+
{
301+
Write-verbose "Invoked $scriptblock failed retrying in 30 seconds..."
302+
Start-Sleep -Seconds 30
303+
$Retrycount = $Retrycount + 1
304+
}
305+
}
306+
}
307+
While ($Stoploop -eq $false)
308+
}
309+
```
310+
311+
You can then pass each remote call into the function as </br>
312+
313+
`ResilientRemoteCall { Get-AzVm }` </br> or </br>
314+
315+
`ResilientRemoteCall { $searchAPIKey = (Invoke-AzureRmResourceAction -Action listAdminKeys -ResourceId $resource.ResourceId -ApiVersion 2015-08-19 -Force).PrimaryKey}`
316+
317+
219318
## Work with multiple subscriptions
220319

221320
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)