Skip to content

Fix: Fix mailbox rules orchestrator to handle multiple starts gracefully #1424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function Push-ListMailboxRulesQueue {
Rules = [string]($Rule | ConvertTo-Json)
RowKey = [string](New-Guid).guid
Tenant = [string]$domainName
PartitionKey = 'mailboxrules'
PartitionKey = 'MailboxRules'
}

}
Expand All @@ -38,7 +38,7 @@ function Push-ListMailboxRulesQueue {
Rules = [string]$Rules
RowKey = [string]$domainName
Tenant = [string]$domainName
PartitionKey = 'mailboxrules'
PartitionKey = 'MailboxRules'
}
}
} catch {
Expand All @@ -49,7 +49,7 @@ function Push-ListMailboxRulesQueue {
Rules = [string]$Rules
RowKey = [string]$domainName
Tenant = [string]$domainName
PartitionKey = 'mailboxrules'
PartitionKey = 'MailboxRules'
}
}
Add-CIPPAzDataTableEntity @Table -Entity $GraphRequest -Force | Out-Null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,24 @@ Function Invoke-ListMailboxRules {
$Table.Filter = "Tenant eq '$TenantFilter'"
}
$Rows = Get-CIPPAzDataTableEntity @Table | Where-Object -Property Timestamp -GT (Get-Date).AddHours(-1)
$PartitionKey = 'MailboxRules'
$QueueReference = '{0}-{1}' -f $TenantFilter, $PartitionKey
$RunningQueue = Invoke-ListCippQueue | Where-Object { $_.Reference -eq $QueueReference -and $_.Status -notmatch 'Completed' -and $_.Status -notmatch 'Failed' }

$Metadata = @{}
if (!$Rows -or ($TenantFilter -eq 'AllTenants' -and ($Rows | Measure-Object).Count -eq 1)) {
# If a queue is running, we will not start a new one
if ($RunningQueue) {
$Metadata = [PSCustomObject]@{
QueueMessage = 'Loading data. Please check back in 1 minute'
QueueMessage = "Still loading data for $TenantFilter. Please check back in a few more minutes"
}
[PSCustomObject]@{
Waiting = $true
}
} elseif ((!$Rows -and !$RunningQueue) -or ($TenantFilter -eq 'AllTenants' -and ($Rows | Measure-Object).Count -eq 1)) {
# If no rows are found and no queue is running, we will start a new one
$Metadata = [PSCustomObject]@{
QueueMessage = "Loading data for $TenantFilter. Please check back in 1 minute"
}
$GraphRequest = @()

if ($TenantFilter -eq 'AllTenants') {
$Tenants = Get-Tenants -IncludeErrors | Select-Object defaultDomainName
Expand All @@ -37,7 +48,7 @@ Function Invoke-ListMailboxRules {
$Tenants = @(@{ defaultDomainName = $TenantFilter })
$Type = $TenantFilter
}
$Queue = New-CippQueueEntry -Name "Mailbox Rules ($Type)" -TotalTasks ($Tenants | Measure-Object).Count
$Queue = New-CippQueueEntry -Name "Mailbox Rules ($Type)" -Reference $QueueReference -TotalTasks ($Tenants | Measure-Object).Count
$Batch = $Tenants | Select-Object defaultDomainName, @{Name = 'FunctionName'; Expression = { 'ListMailboxRulesQueue' } }, @{Name = 'QueueName'; Expression = { $_.defaultDomainName } }, @{Name = 'QueueId'; Expression = { $Queue.RowKey } }
if (($Batch | Measure-Object).Count -gt 0) {
$InputObject = [PSCustomObject]@{
Expand All @@ -53,6 +64,7 @@ Function Invoke-ListMailboxRules {
} else {
if ($TenantFilter -ne 'AllTenants') {
$Rows = $Rows | Where-Object -Property Tenant -EQ $TenantFilter
$Rows = $Rows
}
$GraphRequest = $Rows | ForEach-Object {
$NewObj = $_.Rules | ConvertFrom-Json -ErrorAction SilentlyContinue
Expand All @@ -61,6 +73,8 @@ Function Invoke-ListMailboxRules {
}
}

# If no results are found, we will return an empty message to prevent null reference errors in the frontend
$GraphRequest = $GraphRequest ?? @()
$Body = @{
Results = @($GraphRequest)
Metadata = $Metadata
Expand Down