Skip to content

Commit

Permalink
feat: script for resubmitting failed Logic App instances (#385)
Browse files Browse the repository at this point in the history
Co-authored-by: Pim Simons <[email protected]>
  • Loading branch information
pim-simons and pim-simons authored May 10, 2023
1 parent 2baf459 commit 558caa5
Show file tree
Hide file tree
Showing 7 changed files with 401 additions and 4 deletions.
33 changes: 33 additions & 0 deletions docs/preview/03-Features/powershell/azure-logic-apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,39 @@ PS> Cancel-AzLogicAppRuns `
# Successfully cancelled all running instances for the Azure Logic App 'rcv-shopping-order-sftp' in resource group 'rg-common-dev'
```

## Resubmitting failed instances for an Azure Logic App

Use this script to re-run a failed Azure Logic App run.

| Parameter | Mandatory | Description |
| ------------------- | --------- | ---------------------------------------------------------------------------------------------------------- |
| `ResourceGroupName` | yes | The resource group containing the Azure Logic App. |
| `LogicAppName` | yes | The name of the Azure Logic App to be disabled. |
| `StartTime` | yes | The start time in UTC for retrieving the failed instances. |
| `EndTime` | no | The end time in UTC for retrieving the failed instances, if not supplied it will use the current datetime. |

**Example**

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.

```powershell
PS> Resubmit-FailedAzLogicAppRuns `
-ResourceGroupName "rg-common-dev" `
-LogicAppName "rcv-shopping-order-sftp" `
-StartTime "2023-05-01 00:00:00"
# 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'
```

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.

```powershell
PS> Resubmit-FailedAzLogicAppRuns `
-ResourceGroupName "rg-common-dev" `
-LogicAppName "rcv-shopping-order-sftp" `
-StartTime "2023-05-01 00:00:00" `
-EndTime "2023-05-01 10:00:00"
# 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'
```

## Disable an Azure Logic App

Expand Down
Binary file modified src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.psd1
Binary file not shown.
41 changes: 39 additions & 2 deletions src/Arcus.Scripting.LogicApps/Arcus.Scripting.LogicApps.psm1
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<#
.Synopsis
Cancel all running instances of a specific Logic App.
Cancel all running instances of a specific Azure Logic App.
.Description
Cancel all running instances of a specific Logic App.
Cancel all running instances of a specific Azure Logic App.
.Parameter ResourceGroupName
The resource group containing the Azure Logic App.
Expand All @@ -23,6 +23,43 @@ function Cancel-AzLogicAppRuns {

Export-ModuleMember -Function Cancel-AzLogicAppRuns

<#
.Synopsis
Resubmit all failed instances of a specific Azure Logic App.
.Description
Resubmit all failed instances of a specific Azure Logic App within a specified start and end time.
.Parameter ResourceGroupName
The resource group containing the Azure Logic App.
.Parameter LogicAppName
The name of the Azure Logic App.
.Parameter StartTime
The start time of the failed instances of the Azure Logic App.
.Parameter EndTime
The end time of the failed instances of the Azure Logic App.
#>
function Resubmit-FailedAzLogicAppRuns {
param(
[Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Name of the resource group is required"),
[Parameter(Mandatory = $true)][string] $LogicAppName = $(throw "Name of the logic app is required"),
[Parameter(Mandatory = $true)][datetime] $StartTime = $(throw "Start time is required"),
[Parameter(Mandatory = $false)][datetime] $EndTime
)

if ($EndTime) {
. $PSScriptRoot\Scripts\Resubmit-FailedAzLogicAppRuns.ps1 -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -StartTime $StartTime -EndTime $EndTime
} else {
. $PSScriptRoot\Scripts\Resubmit-FailedAzLogicAppRuns.ps1 -ResourceGroupName $ResourceGroupName -LogicAppName $LogicAppName -StartTime $StartTime
}
}

Export-ModuleMember -Function Resubmit-FailedAzLogicAppRuns

<#
.Synopsis
Disable a specific Logic App.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<ItemGroup>
<Compile Include="Arcus.Scripting.LogicApps.psd1" />
<Compile Include="Arcus.Scripting.LogicApps.psm1" />
<Compile Include="Scripts\Resubmit-FailedAzLogicAppRuns.ps1" />
<Compile Include="Scripts\Cancel-AzLogicAppRuns.ps1" />
<Compile Include="Scripts\Disable-AzLogicApp.ps1" />
<Compile Include="Scripts\Disable-AzLogicAppsFromConfig.ps1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
param(
[Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Name of the resource group is required"),
[Parameter(Mandatory = $true)][string] $LogicAppName = $(throw "Name of the logic app is required"),
[Parameter(Mandatory = $true)][datetime] $StartTime = $(throw "Start time is required"),
[Parameter(Mandatory = $false)][datetime] $EndTime
)

try{
if ($EndTime) {
$runs = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $LogicAppName |
Where-Object {$_.Status -eq 'Failed' -and $_.StartTime -ge $StartTime -and $_.EndTime -le $EndTime}
} else {
$runs = Get-AzLogicAppRunHistory -ResourceGroupName $ResourceGroupName -Name $LogicAppName |
Where-Object {$_.Status -eq 'Failed' -and $_.StartTime -ge $StartTime}
}

$token = Get-AzCachedAccessToken
$accessToken = $token.AccessToken
$subscriptionId = $token.SubscriptionId

foreach ($run in $runs) {
$triggerName = $run.Trigger.Name
$runId = $run.Name
$resubmitUrl = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Logic/workflows/$LogicAppName/triggers/$triggerName/histories/$runId/resubmit?api-version=2016-06-01"

$params = @{
Method = 'Post'
Headers = @{
'authorization'="Bearer $accessToken"
}
URI = $resubmitUrl
}

$web = Invoke-WebRequest @params -ErrorAction Stop

Write-Verbose "Resubmitted run $runId for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName'"
}

if ($EndTime) {
Write-Host "Successfully resubmitted all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime' and until '$EndTime'" -ForegroundColor Green
} else {
Write-Host "Successfully resubmitted all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime'" -ForegroundColor Green
}
} catch {
if ($EndTime) {
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)"
} else {
throw "Failed to resubmit all failed instances for the Azure Logic App '$LogicAppName' in resource group '$ResourceGroupName' from '$StartTime'. Details: $($_.Exception.Message)"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,116 @@ InModuleScope Arcus.Scripting.LogicApps {
}
}
}
Context "Resubmit Failed Logic Apps runs" {
It "Resubmit all failed instances for a Logic App"{
# Arrange
$resourceGroupName = $config.Arcus.ResourceGroupName
$logicAppName = Create-AzLogicAppName
$workflowDefinition = '{
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Terminate": {
"inputs": {
"runStatus": "Failed"
},
"runAfter": {},
"type": "Terminate"
}
},
"outputs": {},
"parameters": {},
"triggers": {
"Recurrence": {
"recurrence": {
"frequency": "Minute",
"interval": 1
},
"type": "recurrence"
}
},
"contentVersion": "1.0.0.0"
}'

$startTime = [datetime]::Now.ToUniversalTime()

New-AzLogicApp `
-ResourceGroupName $resourceGroupName `
-Location westeurope `
-Name $logicAppName `
-Definition $workflowDefinition `
-State Enabled

Start-Sleep -Seconds 5

try {
# Act
Resubmit-FailedAzLogicAppRuns -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName -StartTime $startTime

# Assert
$runs = Get-AzLogicAppRunHistory -ResourceGroupName $resourceGroupName -Name $logicAppName |
Where-Object {$_.StartTime -ge $startTime} | measure

$runs.Count | Should -BeGreaterThan 0

} finally {
Remove-AzLogicApp -ResourceGroupName $resourceGroupName -Name $logicAppName -Force
}
}
It "Resubmit all failed instances for a Logic App with specifying an EndTime"{
# Arrange
$resourceGroupName = $config.Arcus.ResourceGroupName
$logicAppName = Create-AzLogicAppName
$workflowDefinition = '{
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Terminate": {
"inputs": {
"runStatus": "Failed"
},
"runAfter": {},
"type": "Terminate"
}
},
"outputs": {},
"parameters": {},
"triggers": {
"Recurrence": {
"recurrence": {
"frequency": "Minute",
"interval": 1
},
"type": "recurrence"
}
},
"contentVersion": "1.0.0.0"
}'

$startTime = [datetime]::Now.ToUniversalTime()
$endTime = [datetime]::Now.AddDays(1).ToUniversalTime()

New-AzLogicApp `
-ResourceGroupName $resourceGroupName `
-Location westeurope `
-Name $logicAppName `
-Definition $workflowDefinition `
-State Enabled

Start-Sleep -Seconds 5

try {
# Act
Resubmit-FailedAzLogicAppRuns -ResourceGroupName $resourceGroupName -LogicAppName $logicAppName -StartTime $startTime -EndTime $endTime

# Assert
$runs = Get-AzLogicAppRunHistory -ResourceGroupName $resourceGroupName -Name $logicAppName |
Where-Object {$_.StartTime -ge $startTime} | measure

$runs.Count | Should -BeGreaterThan 0

} finally {
Remove-AzLogicApp -ResourceGroupName $resourceGroupName -Name $logicAppName -Force
}
}
}
}
}
Loading

0 comments on commit 558caa5

Please sign in to comment.