@@ -17,81 +17,5 @@ function Start-DurableCleanup {
1717 param (
1818 [int ]$MaxDuration = 86400
1919 )
20-
21- $WarningPreference = ' SilentlyContinue'
22- $StorageContext = New-AzStorageContext - ConnectionString $env: AzureWebJobsStorage
23- $TargetTime = (Get-Date ).ToUniversalTime().AddSeconds(- $MaxDuration )
24- $Context = New-AzDataTableContext - ConnectionString $env: AzureWebJobsStorage
25- $InstancesTables = Get-AzDataTable - Context $Context | Where-Object { $_ -match ' Instances' }
26-
27- $CleanupCount = 0
28- $QueueCount = 0
29-
30- $FunctionsWithLongRunningOrchestrators = [System.Collections.Generic.List [object ]]::new()
31- $NonDeterministicOrchestrators = [System.Collections.Generic.List [object ]]::new()
32-
33- foreach ($Table in $InstancesTables ) {
34- $Table = Get-CippTable - TableName $Table
35- $FunctionName = $Table.TableName -replace ' Instances' , ' '
36- $Orchestrators = Get-CIPPAzDataTableEntity @Table - Filter " RuntimeStatus eq 'Running'" | Select-Object * - ExcludeProperty Input
37- $Queues = Get-AzStorageQueue - Context $StorageContext - Name (' {0}*' -f $FunctionName ) | Select-Object - Property Name, ApproximateMessageCount, QueueClient
38- $LongRunningOrchestrators = $Orchestrators | Where-Object { $_.CreatedTime.DateTime -lt $TargetTime }
39-
40- if ($LongRunningOrchestrators.Count -gt 0 ) {
41- $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- }
59- }
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 ++
76- }
77- }
78- }
79-
80- if (($LongRunningOrchestrators.Count -gt 0 -or $NonDeterministicOrchestrators.Count -gt 0 ) -and $Queues.ApproximateMessageCount -gt 0 ) {
81- $RunningQueues = $Queues | Where-Object { $_.ApproximateMessageCount -gt 0 }
82- foreach ($Queue in $RunningQueues ) {
83- Write-Information " - Removing queue: $ ( $Queue.Name ) , message count: $ ( $Queue.ApproximateMessageCount ) "
84- if ($PSCmdlet.ShouldProcess ($Queue.Name , ' Clear Queue' )) {
85- $Queue.QueueClient.ClearMessagesAsync () | Out-Null
86- }
87- $QueueCount ++
88- }
89- }
90- }
91-
92- if ($CleanupCount -gt 0 -or $QueueCount -gt 0 ) {
93- Write-LogMessage - api ' Durable Cleanup' - message " $CleanupCount orchestrators were terminated. $QueueCount queues were cleared." - sev ' Info' - LogData $FunctionsWithLongRunningOrchestrators
94- }
95-
96- Write-Information " Durable cleanup complete. $CleanupCount orchestrators were terminated. $QueueCount queues were cleared."
20+ Write-Information " This cleanup is no longer required."
9721}
0 commit comments