Skip to content

Commit 1efed26

Browse files
Merge pull request KelvinTegelaar#1424 from kris6673/fix-mailboxrules-orchestrator
Fix: Fix mailbox rules orchestrator to handle multiple starts gracefully
2 parents e4b7e80 + 34b3f76 commit 1efed26

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

Modules/CIPPCore/Public/Entrypoints/Activity Triggers/Push-ListMailboxRulesQueue.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function Push-ListMailboxRulesQueue {
2626
Rules = [string]($Rule | ConvertTo-Json)
2727
RowKey = [string](New-Guid).guid
2828
Tenant = [string]$domainName
29-
PartitionKey = 'mailboxrules'
29+
PartitionKey = 'MailboxRules'
3030
}
3131

3232
}
@@ -38,7 +38,7 @@ function Push-ListMailboxRulesQueue {
3838
Rules = [string]$Rules
3939
RowKey = [string]$domainName
4040
Tenant = [string]$domainName
41-
PartitionKey = 'mailboxrules'
41+
PartitionKey = 'MailboxRules'
4242
}
4343
}
4444
} catch {
@@ -49,7 +49,7 @@ function Push-ListMailboxRulesQueue {
4949
Rules = [string]$Rules
5050
RowKey = [string]$domainName
5151
Tenant = [string]$domainName
52-
PartitionKey = 'mailboxrules'
52+
PartitionKey = 'MailboxRules'
5353
}
5454
}
5555
Add-CIPPAzDataTableEntity @Table -Entity $GraphRequest -Force | Out-Null

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Email-Exchange/Administration/Invoke-ListMailboxRules.ps1

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)