@@ -28,33 +28,56 @@ function Start-DurableCleanup {
2828 $QueueCount = 0
2929
3030 $FunctionsWithLongRunningOrchestrators = [System.Collections.Generic.List [object ]]::new()
31+ $NonDeterministicOrchestrators = [System.Collections.Generic.List [object ]]::new()
32+
3133 foreach ($Table in $InstancesTables ) {
3234 $Table = Get-CippTable - TableName $Table
3335 $FunctionName = $Table.TableName -replace ' Instances' , ' '
3436 $Orchestrators = Get-CIPPAzDataTableEntity @Table - Filter " RuntimeStatus eq 'Running'" | Select-Object * - ExcludeProperty Input
3537 $Queues = Get-AzStorageQueue - Context $StorageContext - Name (' {0}*' -f $FunctionName ) | Select-Object - Property Name, ApproximateMessageCount, QueueClient
3638 $LongRunningOrchestrators = $Orchestrators | Where-Object { $_.CreatedTime.DateTime -lt $TargetTime }
39+
3740 if ($LongRunningOrchestrators.Count -gt 0 ) {
3841 $FunctionsWithLongRunningOrchestrators.Add (@ {' FunctionName' = $FunctionName })
42+ foreach ($Orchestrator in $LongRunningOrchestrators ) {
43+ $CreatedTime = [DateTime ]::SpecifyKind($Orchestrator.CreatedTime.DateTime , [DateTimeKind ]::Utc)
44+ $TimeSpan = New-TimeSpan - Start $CreatedTime - End (Get-Date ).ToUniversalTime()
45+ $RunningDuration = [math ]::Round($TimeSpan.TotalMinutes , 2 )
46+ Write-Information " Orchestrator: $ ( $Orchestrator.PartitionKey ) , created: $CreatedTime , running for: $RunningDuration minutes"
47+ if ($PSCmdlet.ShouldProcess ($_.PartitionKey , ' Terminate Orchestrator' )) {
48+ $Orchestrator = Get-CIPPAzDataTableEntity @Table - Filter " PartitionKey eq '$ ( $Orchestrator.PartitionKey ) '"
49+ $Orchestrator.RuntimeStatus = ' Failed'
50+ if ($Orchestrator.PSObject.Properties.Name -contains ' CustomStatus' ) {
51+ $Orchestrator.CustomStatus = " Terminated by Durable Cleanup - Exceeded max duration of $MaxDuration seconds"
52+ } else {
53+ $Orchestrator | Add-Member - MemberType NoteProperty - Name CustomStatus - Value " Terminated by Durable Cleanup - Exceeded max duration of $MaxDuration seconds"
54+ }
55+ Update-AzDataTableEntity @Table - Entity $Orchestrator
56+ $CleanupCount ++
57+ }
58+ }
3959 }
40- foreach ($Orchestrator in $LongRunningOrchestrators ) {
41- $CreatedTime = [DateTime ]::SpecifyKind($Orchestrator.CreatedTime.DateTime , [DateTimeKind ]::Utc)
42- $TimeSpan = New-TimeSpan - Start $CreatedTime - End (Get-Date ).ToUniversalTime()
43- $RunningDuration = [math ]::Round($TimeSpan.TotalMinutes , 2 )
44- Write-Information " Orchestrator: $ ( $Orchestrator.PartitionKey ) , created: $CreatedTime , running for: $RunningDuration minutes"
45- if ($PSCmdlet.ShouldProcess ($_.PartitionKey , ' Terminate Orchestrator' )) {
46- $Orchestrator = Get-CIPPAzDataTableEntity @Table - Filter " PartitionKey eq '$ ( $Orchestrator.PartitionKey ) '"
47- $Orchestrator.RuntimeStatus = ' Failed'
48- if ($Orchestrator.PSObject.Properties.Name -contains ' CustomStatus' ) {
49- $Orchestrator.CustomStatus = " Terminated by Durable Cleanup - Exceeded max duration of $MaxDuration seconds"
50- } else {
51- $Orchestrator | Add-Member - MemberType NoteProperty - Name CustomStatus - Value " Terminated by Durable Cleanup - Exceeded max duration of $MaxDuration seconds"
60+
61+ $NonDeterministicOrchestrators = $Orchestrators | Where-Object { $_.Output -match ' Non-Deterministic workflow detected' }
62+ if ($NonDeterministicOrchestrators.Count -gt 0 ) {
63+ $NonDeterministicOrchestrators.Add (@ {' FunctionName' = $FunctionName })
64+ foreach ($Orchestrator in $NonDeterministicOrchestrators ) {
65+ Write-Information " Orchestrator: $ ( $Orchestrator.PartitionKey ) is Non-Deterministic"
66+ if ($PSCmdlet.ShouldProcess ($_.PartitionKey , ' Terminate Orchestrator' )) {
67+ $Orchestrator = Get-CIPPAzDataTableEntity @Table - Filter " PartitionKey eq '$ ( $Orchestrator.PartitionKey ) '"
68+ $Orchestrator.RuntimeStatus = ' Failed'
69+ if ($Orchestrator.PSObject.Properties.Name -contains ' CustomStatus' ) {
70+ $Orchestrator.CustomStatus = ' Terminated by Durable Cleanup - Non-Deterministic workflow detected'
71+ } else {
72+ $Orchestrator | Add-Member - MemberType NoteProperty - Name CustomStatus - Value ' Terminated by Durable Cleanup - Non-Deterministic workflow detected'
73+ }
74+ Update-AzDataTableEntity @Table - Entity $Orchestrator
75+ $CleanupCount ++
5276 }
53- Update-AzDataTableEntity @Table - Entity $Orchestrator
54- $CleanupCount ++
5577 }
5678 }
57- if ($LongRunningOrchestrators.Count -gt 0 -and $Queues.ApproximateMessageCount -gt 0 ) {
79+
80+ if (($LongRunningOrchestrators.Count -gt 0 -or $NonDeterministicOrchestrators.Count -gt 0 ) -and $Queues.ApproximateMessageCount -gt 0 ) {
5881 $RunningQueues = $Queues | Where-Object { $_.ApproximateMessageCount -gt 0 }
5982 foreach ($Queue in $RunningQueues ) {
6083 Write-Information " - Removing queue: $ ( $Queue.Name ) , message count: $ ( $Queue.ApproximateMessageCount ) "
0 commit comments