Skip to content

Commit ada0f46

Browse files
authored
Merge pull request KelvinTegelaar#1413 from KelvinTegelaar/dev
fix for out of memory issue
2 parents 881cd0b + 8b3fdc6 commit ada0f46

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

Modules/CIPPCore/Public/Entrypoints/Orchestrator Functions/Start-AuditLogProcessingOrchestrator.ps1

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,52 @@ function Start-AuditLogProcessingOrchestrator {
77
param()
88
Write-Information 'Starting audit log processing in batches of 1000, per tenant'
99
$WebhookCacheTable = Get-CippTable -TableName 'CacheWebhooks'
10-
$WebhookCache = Get-CIPPAzDataTableEntity @WebhookCacheTable
11-
$TenantGroups = $WebhookCache | Group-Object -Property PartitionKey
1210

13-
if ($TenantGroups) {
14-
Write-Information "Processing webhook cache for $($TenantGroups.Count) tenants"
15-
#Write-Warning "AuditLogJobs are: $($TenantGroups.Count) tenants. Tenants: $($TenantGroups.name | ConvertTo-Json -Compress) "
16-
#Write-Warning "Here are the groups: $($TenantGroups | ConvertTo-Json -Compress)"
17-
$ProcessQueue = New-CippQueueEntry -Name 'Audit Logs Process' -Reference 'AuditLogsProcess' -TotalTasks ($TenantGroups | Measure-Object -Property Count -Sum).Sum
18-
$ProcessBatch = foreach ($TenantGroup in $TenantGroups) {
19-
$TenantFilter = $TenantGroup.Name
20-
$RowIds = @($TenantGroup.Group.RowKey)
21-
for ($i = 0; $i -lt $RowIds.Count; $i += 1000) {
22-
Write-Host "Processing $TenantFilter with $($RowIds.Count) row IDs. We're processing id $($RowIds[$i]) to $($RowIds[[Math]::Min($i + 999, $RowIds.Count - 1)])"
23-
$BatchRowIds = $RowIds[$i..([Math]::Min($i + 999, $RowIds.Count - 1))]
24-
[PSCustomObject]@{
25-
TenantFilter = $TenantFilter
26-
RowIds = $BatchRowIds
27-
QueueId = $ProcessQueue.RowKey
28-
FunctionName = 'AuditLogTenantProcess'
11+
$DataTableQuery = @{
12+
First = 20000
13+
Skip = 0
14+
}
15+
16+
do {
17+
$WebhookCache = Get-CIPPAzDataTableEntity @WebhookCacheTable @DataTableQuery
18+
$TenantGroups = $WebhookCache | Group-Object -Property PartitionKey
19+
20+
if ($TenantGroups) {
21+
Write-Information "Processing webhook cache for $($TenantGroups.Count) tenants"
22+
#Write-Warning "AuditLogJobs are: $($TenantGroups.Count) tenants. Tenants: $($TenantGroups.name | ConvertTo-Json -Compress) "
23+
#Write-Warning "Here are the groups: $($TenantGroups | ConvertTo-Json -Compress)"
24+
$ProcessQueue = New-CippQueueEntry -Name 'Audit Logs Process' -Reference 'AuditLogsProcess' -TotalTasks ($TenantGroups | Measure-Object -Property Count -Sum).Sum
25+
$ProcessBatch = foreach ($TenantGroup in $TenantGroups) {
26+
$TenantFilter = $TenantGroup.Name
27+
$RowIds = @($TenantGroup.Group.RowKey)
28+
for ($i = 0; $i -lt $RowIds.Count; $i += 1000) {
29+
Write-Host "Processing $TenantFilter with $($RowIds.Count) row IDs. We're processing id $($RowIds[$i]) to $($RowIds[[Math]::Min($i + 999, $RowIds.Count - 1)])"
30+
$BatchRowIds = $RowIds[$i..([Math]::Min($i + 999, $RowIds.Count - 1))]
31+
[PSCustomObject]@{
32+
TenantFilter = $TenantFilter
33+
RowIds = $BatchRowIds
34+
QueueId = $ProcessQueue.RowKey
35+
FunctionName = 'AuditLogTenantProcess'
36+
}
2937
}
3038
}
31-
}
32-
if ($ProcessBatch) {
33-
$ProcessInputObject = [PSCustomObject]@{
34-
OrchestratorName = 'AuditLogTenantProcess'
35-
Batch = @($ProcessBatch)
36-
SkipLog = $true
39+
if ($ProcessBatch) {
40+
$ProcessInputObject = [PSCustomObject]@{
41+
OrchestratorName = 'AuditLogTenantProcess'
42+
Batch = @($ProcessBatch)
43+
SkipLog = $true
44+
}
45+
Start-NewOrchestration -FunctionName 'CIPPOrchestrator' -InputObject ($ProcessInputObject | ConvertTo-Json -Depth 5 -Compress)
46+
Write-Information "Started audit log processing orchestration with $($ProcessBatch.Count) batches"
3747
}
38-
Start-NewOrchestration -FunctionName 'CIPPOrchestrator' -InputObject ($ProcessInputObject | ConvertTo-Json -Depth 5 -Compress)
39-
Write-Information "Started audit log processing orchestration with $($ProcessBatch.Count) batches"
4048
}
41-
}
49+
50+
if ($WebhookCache.Count -lt 20000) {
51+
Write-Information 'No more rows to process'
52+
break
53+
}
54+
Write-Information "Processed $($WebhookCache.Count) rows"
55+
$DataTableQuery.Skip += 20000
56+
Write-Information "Getting next batch of $($DataTableQuery.First) rows"
57+
} while ($WebhookCache.Count -eq 20000)
4258
}

0 commit comments

Comments
 (0)