@@ -22,13 +22,24 @@ Function Invoke-ListMailboxRules {
2222 $Table.Filter = " Tenant eq '$TenantFilter '"
2323 }
2424 $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' }
2528
2629 $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 ) {
2832 $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"
3042 }
31- $GraphRequest = @ ()
3243
3344 if ($TenantFilter -eq ' AllTenants' ) {
3445 $Tenants = Get-Tenants - IncludeErrors | Select-Object defaultDomainName
@@ -37,7 +48,7 @@ Function Invoke-ListMailboxRules {
3748 $Tenants = @ (@ { defaultDomainName = $TenantFilter })
3849 $Type = $TenantFilter
3950 }
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
4152 $Batch = $Tenants | Select-Object defaultDomainName, @ {Name = ' FunctionName' ; Expression = { ' ListMailboxRulesQueue' } }, @ {Name = ' QueueName' ; Expression = { $_.defaultDomainName } }, @ {Name = ' QueueId' ; Expression = { $Queue.RowKey } }
4253 if (($Batch | Measure-Object ).Count -gt 0 ) {
4354 $InputObject = [PSCustomObject ]@ {
@@ -53,6 +64,7 @@ Function Invoke-ListMailboxRules {
5364 } else {
5465 if ($TenantFilter -ne ' AllTenants' ) {
5566 $Rows = $Rows | Where-Object - Property Tenant -EQ $TenantFilter
67+ $Rows = $Rows
5668 }
5769 $GraphRequest = $Rows | ForEach-Object {
5870 $NewObj = $_.Rules | ConvertFrom-Json - ErrorAction SilentlyContinue
@@ -61,6 +73,8 @@ Function Invoke-ListMailboxRules {
6173 }
6274 }
6375
76+ # If no results are found, we will return an empty message to prevent null reference errors in the frontend
77+ $GraphRequest = $GraphRequest ?? @ ()
6478 $Body = @ {
6579 Results = @ ($GraphRequest )
6680 Metadata = $Metadata
0 commit comments