You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/automation/manage-runbooks.md
+95-1Lines changed: 95 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ title: Manage runbooks in Azure Automation
3
3
description: This article tells how to manage runbooks in Azure Automation.
4
4
services: automation
5
5
ms.subservice: process-automation
6
-
ms.date: 11/02/2021
6
+
ms.date: 01/16/2022
7
7
ms.topic: conceptual
8
8
ms.custom: devx-track-azurepowershell
9
9
---
@@ -216,6 +216,100 @@ If your runbook normally runs within a time constraint, have the script implemen
216
216
> [!NOTE]
217
217
> 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.
218
218
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
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.
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>
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