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
+100-1Lines changed: 100 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,105 @@ 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
+
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
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.
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>
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