@@ -22,13 +22,24 @@ Function Invoke-ListMailboxRules {
22
22
$Table.Filter = " Tenant eq '$TenantFilter '"
23
23
}
24
24
$Rows = Get-CIPPAzDataTableEntity @Table | Where-Object - Property Timestamp -GT (Get-Date ).AddHours(-1 )
25
+ $PartitionKey = ' MailboxRules'
26
+ $QueueReference = ' {0}-{1}' -f $TenantFilter , $PartitionKey
27
+ $RunningQueue = Invoke-ListCippQueue | Where-Object { $_.Reference -eq $QueueReference -and $_.Status -notmatch ' Completed' -and $_.Status -notmatch ' Failed' }
25
28
26
29
$Metadata = @ {}
27
- if (! $Rows -or ($TenantFilter -eq ' AllTenants' -and ($Rows | Measure-Object ).Count -eq 1 )) {
30
+ # If a queue is running, we will not start a new one
31
+ if ($RunningQueue ) {
28
32
$Metadata = [PSCustomObject ]@ {
29
- QueueMessage = ' Loading data. Please check back in 1 minute'
33
+ QueueMessage = " Still loading data for $TenantFilter . Please check back in a few more minutes"
34
+ }
35
+ [PSCustomObject ]@ {
36
+ Waiting = $true
37
+ }
38
+ } elseif ((! $Rows -and ! $RunningQueue ) -or ($TenantFilter -eq ' AllTenants' -and ($Rows | Measure-Object ).Count -eq 1 )) {
39
+ # If no rows are found and no queue is running, we will start a new one
40
+ $Metadata = [PSCustomObject ]@ {
41
+ QueueMessage = " Loading data for $TenantFilter . Please check back in 1 minute"
30
42
}
31
- $GraphRequest = @ ()
32
43
33
44
if ($TenantFilter -eq ' AllTenants' ) {
34
45
$Tenants = Get-Tenants - IncludeErrors | Select-Object defaultDomainName
@@ -37,7 +48,7 @@ Function Invoke-ListMailboxRules {
37
48
$Tenants = @ (@ { defaultDomainName = $TenantFilter })
38
49
$Type = $TenantFilter
39
50
}
40
- $Queue = New-CippQueueEntry - Name " Mailbox Rules ($Type )" - TotalTasks ($Tenants | Measure-Object ).Count
51
+ $Queue = New-CippQueueEntry - Name " Mailbox Rules ($Type )" - Reference $QueueReference - TotalTasks ($Tenants | Measure-Object ).Count
41
52
$Batch = $Tenants | Select-Object defaultDomainName, @ {Name = ' FunctionName' ; Expression = { ' ListMailboxRulesQueue' } }, @ {Name = ' QueueName' ; Expression = { $_.defaultDomainName } }, @ {Name = ' QueueId' ; Expression = { $Queue.RowKey } }
42
53
if (($Batch | Measure-Object ).Count -gt 0 ) {
43
54
$InputObject = [PSCustomObject ]@ {
@@ -53,6 +64,7 @@ Function Invoke-ListMailboxRules {
53
64
} else {
54
65
if ($TenantFilter -ne ' AllTenants' ) {
55
66
$Rows = $Rows | Where-Object - Property Tenant -EQ $TenantFilter
67
+ $Rows = $Rows
56
68
}
57
69
$GraphRequest = $Rows | ForEach-Object {
58
70
$NewObj = $_.Rules | ConvertFrom-Json - ErrorAction SilentlyContinue
@@ -61,6 +73,8 @@ Function Invoke-ListMailboxRules {
61
73
}
62
74
}
63
75
76
+ # If no results are found, we will return an empty message to prevent null reference errors in the frontend
77
+ $GraphRequest = $GraphRequest ?? @ ()
64
78
$Body = @ {
65
79
Results = @ ($GraphRequest )
66
80
Metadata = $Metadata
0 commit comments