Skip to content

Commit 558caa5

Browse files
authored
feat: script for resubmitting failed Logic App instances (#385)
Co-authored-by: Pim Simons <pim.simons@codit.eu>
1 parent 2baf459 commit 558caa5

7 files changed

Lines changed: 401 additions & 4 deletions

File tree

docs/preview/03-Features/powershell/azure-logic-apps.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,39 @@ PS> Cancel-AzLogicAppRuns `
3333
# Successfully cancelled all running instances for the Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev'
3434
```
3535

36+
## Resubmitting failed instances for an Azure Logic App
37+
38+
Use this script to re-run a failed Azure Logic App run.
39+
40+
| Parameter | Mandatory | Description |
41+
| ------------------- | --------- | ---------------------------------------------------------------------------------------------------------- |
42+
| `ResourceGroupName` | yes | The resource group containing the Azure Logic App. |
43+
| `LogicAppName` | yes | The name of the Azure Logic App to be disabled. |
44+
| `StartTime` | yes | The start time in UTC for retrieving the failed instances. |
45+
| `EndTime` | no | The end time in UTC for retrieving the failed instances, if not supplied it will use the current datetime. |
46+
47+
**Example**
48+
49+
Taking an example in which a specific Azure Logic App (`"rcv-shopping-order-sftp"`) needs to have all its failed runs resubmitted from 2023-05-01 00:00:00.
50+
51+
```powershell
52+
PS> Resubmit-FailedAzLogicAppRuns `
53+
-ResourceGroupName "rg-common-dev" `
54+
-LogicAppName "rcv-shopping-order-sftp" `
55+
-StartTime "2023-05-01 00:00:00"
56+
# Successfully resubmitted all failed instances for the Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev' from '2023-05-01 00:00:00'
57+
```
58+
59+
Taking an example in which a specific Azure Logic App (`"rcv-shopping-order-sftp"`) needs to have all its failed runs resubmitted from 2023-05-01 00:00:00 until 2023-05-01 10:00:00.
60+
61+
```powershell
62+
PS> Resubmit-FailedAzLogicAppRuns `
63+
-ResourceGroupName "rg-common-dev" `
64+
-LogicAppName "rcv-shopping-order-sftp" `
65+
-StartTime "2023-05-01 00:00:00" `
66+
-EndTime "2023-05-01 10:00:00"
67+
# Successfully resubmitted all failed instances for the Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev' from '2023-05-01 00:00:00' and until '2023-05-01 10:00:00'
68+
```
3669

3770
## Disable an Azure Logic App
3871

76 Bytes
Binary file not shown.

src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.psm1

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<#
22
.Synopsis
3-
Cancel all running instances of a specific Logic App.
3+
Cancel all running instances of a specific Azure Logic App.
44
55
.Description
6-
Cancel all running instances of a specific Logic App.
6+
Cancel all running instances of a specific Azure Logic App.
77
88
.Parameter ResourceGroupName
99
The resource group containing the Azure Logic App.
@@ -23,6 +23,43 @@ function Cancel-AzLogicAppRuns {
2323

2424
Export-ModuleMember -Function Cancel-AzLogicAppRuns
2525

26+
<#
27+
.Synopsis
28+
Resubmit all failed instances of a specific Azure Logic App.
29+
30+
.Description
31+
Resubmit all failed instances of a specific Azure Logic App within a specified start and end time.
32+
33+
.Parameter ResourceGroupName
34+
The resource group containing the Azure Logic App.
35+
36+
.Parameter LogicAppName
37+
The name of the Azure Logic App.
38+
39+
.Parameter StartTime
40+
The start time of the failed instances of the Azure Logic App.
41+
42+
.Parameter EndTime
43+
The end time of the failed instances of the Azure Logic App.
44+
45+
#>
46+
function Resubmit-FailedAzLogicAppRuns {
47+
param(
48+
[Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Name of the resource group is required"),
49+
[Parameter(Mandatory = $true)][string] $LogicAppName = $(throw "Name of the logic app is required"),
50+
[Parameter(Mandatory = $true)][datetime] $StartTime = $(throw "Start time is required"),
51+
[Parameter(Mandatory = $false)][datetime] $EndTime
52+
)
53+
54+
if ($EndTime) {
55+
. $PSScriptRoot\Scripts\Resubmit-FailedAzLogicAppRuns.ps1 -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -StartTime $StartTime -EndTime $EndTime
56+
} else {
57+
. $PSScriptRoot\Scripts\Resubmit-FailedAzLogicAppRuns.ps1 -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -StartTime $StartTime
58+
}
59+
}
60+
61+
Export-ModuleMember -Function Resubmit-FailedAzLogicAppRuns
62+
2663
<#
2764
.Synopsis
2865
Disable a specific Logic App.

src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.pssproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<ItemGroup>
3232
<Compile Include="Arcus.Scripting.LogicApps.psd1" />
3333
<Compile Include="Arcus.Scripting.LogicApps.psm1" />
34+
<Compile Include="Scripts\Resubmit-FailedAzLogicAppRuns.ps1" />
3435
<Compile Include="Scripts\Cancel-AzLogicAppRuns.ps1" />
3536
<Compile Include="Scripts\Disable-AzLogicApp.ps1" />
3637
<Compile Include="Scripts\Disable-AzLogicAppsFromConfig.ps1" />
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
param(
2+
[Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Name of the resource group is required"),
3+
[Parameter(Mandatory = $true)][string] $LogicAppName = $(throw "Name of the logic app is required"),
4+
[Parameter(Mandatory = $true)][datetime] $StartTime = $(throw "Start time is required"),
5+
[Parameter(Mandatory = $false)][datetime] $EndTime
6+
)
7+
8+
try{
9+
if ($EndTime) {
10+
$runs = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $LogicAppName |
11+
Where-Object {$_.Status -eq 'Failed' -and $_.StartTime -ge $StartTime -and $_.EndTime -le $EndTime}
12+
} else {
13+
$runs = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $LogicAppName |
14+
Where-Object {$_.Status -eq 'Failed' -and $_.StartTime -ge $StartTime}
15+
}
16+
17+
$token = Get-AzCachedAccessToken
18+
$accessToken = $token.AccessToken
19+
$subscriptionId = $token.SubscriptionId
20+
21+
foreach ($run in $runs) {
22+
$triggerName = $run.Trigger.Name
23+
$runId = $run.Name
24+
$resubmitUrl = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Logic/workflows/$LogicAppName/triggers/$triggerName/histories/$runId/resubmit?api-version=2016-06-01"
25+
26+
$params = @{
27+
Method = 'Post'
28+
Headers = @{
29+
'authorization'="Bearer $accessToken"
30+
}
31+
URI = $resubmitUrl
32+
}
33+
34+
$web = Invoke-WebRequest @params -ErrorAction Stop
35+
36+
Write-Verbose "Resubmitted run $runId for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'"
37+
}
38+
39+
if ($EndTime) {
40+
Write-Host "Successfully resubmitted all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime' and until '$EndTime'" -ForegroundColor Green
41+
} else {
42+
Write-Host "Successfully resubmitted all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime'" -ForegroundColor Green
43+
}
44+
} catch {
45+
if ($EndTime) {
46+
throw "Failed to resubmit all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime' and until '$EndTime'. Details: $($_.Exception.Message)"
47+
} else {
48+
throw "Failed to resubmit all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime'. Details: $($_.Exception.Message)"
49+
}
50+
}

src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.LogicApps.tests.ps1

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,5 +227,116 @@ InModuleScope Arcus.Scripting.LogicApps {
227227
}
228228
}
229229
}
230+
Context "Resubmit Failed Logic Apps runs" {
231+
It "Resubmit all failed instances for a Logic App"{
232+
# Arrange
233+
$resourceGroupName = $config.Arcus.ResourceGroupName
234+
$logicAppName = Create-AzLogicAppName
235+
$workflowDefinition = '{
236+
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
237+
"actions": {
238+
"Terminate": {
239+
"inputs": {
240+
"runStatus": "Failed"
241+
},
242+
"runAfter": {},
243+
"type": "Terminate"
244+
}
245+
},
246+
"outputs": {},
247+
"parameters": {},
248+
"triggers": {
249+
"Recurrence": {
250+
"recurrence": {
251+
"frequency": "Minute",
252+
"interval": 1
253+
},
254+
"type": "recurrence"
255+
}
256+
},
257+
"contentVersion": "1.0.0.0"
258+
}'
259+
260+
$startTime = [datetime]::Now.ToUniversalTime()
261+
262+
New-AzLogicApp `
263+
-ResourceGroupName $resourceGroupName `
264+
-Location westeurope `
265+
-Name $logicAppName `
266+
-Definition $workflowDefinition `
267+
-State Enabled
268+
269+
Start-Sleep -Seconds 5
270+
271+
try {
272+
# Act
273+
Resubmit-FailedAzLogicAppRuns -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName -StartTime $startTime
274+
275+
# Assert
276+
$runs = Get-AzLogicAppRunHistory -ResourceGroupName $resourceGroupName -Name $logicAppName |
277+
Where-Object {$_.StartTime -ge $startTime} | measure
278+
279+
$runs.Count | Should -BeGreaterThan 0
280+
281+
} finally {
282+
Remove-AzLogicApp -ResourceGroupName $resourceGroupName -Name $logicAppName -Force
283+
}
284+
}
285+
It "Resubmit all failed instances for a Logic App with specifying an EndTime"{
286+
# Arrange
287+
$resourceGroupName = $config.Arcus.ResourceGroupName
288+
$logicAppName = Create-AzLogicAppName
289+
$workflowDefinition = '{
290+
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
291+
"actions": {
292+
"Terminate": {
293+
"inputs": {
294+
"runStatus": "Failed"
295+
},
296+
"runAfter": {},
297+
"type": "Terminate"
298+
}
299+
},
300+
"outputs": {},
301+
"parameters": {},
302+
"triggers": {
303+
"Recurrence": {
304+
"recurrence": {
305+
"frequency": "Minute",
306+
"interval": 1
307+
},
308+
"type": "recurrence"
309+
}
310+
},
311+
"contentVersion": "1.0.0.0"
312+
}'
313+
314+
$startTime = [datetime]::Now.ToUniversalTime()
315+
$endTime = [datetime]::Now.AddDays(1).ToUniversalTime()
316+
317+
New-AzLogicApp `
318+
-ResourceGroupName $resourceGroupName `
319+
-Location westeurope `
320+
-Name $logicAppName `
321+
-Definition $workflowDefinition `
322+
-State Enabled
323+
324+
Start-Sleep -Seconds 5
325+
326+
try {
327+
# Act
328+
Resubmit-FailedAzLogicAppRuns -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName -StartTime $startTime -EndTime $endTime
329+
330+
# Assert
331+
$runs = Get-AzLogicAppRunHistory -ResourceGroupName $resourceGroupName -Name $logicAppName |
332+
Where-Object {$_.StartTime -ge $startTime} | measure
333+
334+
$runs.Count | Should -BeGreaterThan 0
335+
336+
} finally {
337+
Remove-AzLogicApp -ResourceGroupName $resourceGroupName -Name $logicAppName -Force
338+
}
339+
}
340+
}
230341
}
231342
}

0 commit comments

Comments
 (0)