Skip to content

Commit 0524970

Browse files
Testing rerun protection
1 parent 348d597 commit 0524970

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Modules/CIPPCore/Public/AuditLogs/New-CIPPAuditLogSearchResultsCache.ps1

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ function New-CIPPAuditLogSearchResultsCache {
1414
[string]$SearchId
1515
)
1616

17+
# Rerun protection - Check if we already tried downloading this search ID in the last 4 hours
18+
try {
19+
$FailedDownloadsTable = Get-CippTable -TableName 'FailedAuditLogDownloads'
20+
$fourHoursAgo = (Get-Date).AddHours(-4).ToUniversalTime()
21+
22+
# Check if this search ID has a failed attempt in the last 4 hours
23+
$failedEntity = Get-CIPPAzDataTableEntity @FailedDownloadsTable -Filter "PartitionKey eq '$TenantFilter' and SearchId eq '$SearchId' and Timestamp ge datetime'$($fourHoursAgo.ToString('yyyy-MM-ddTHH:mm:ssZ'))'"
24+
25+
if ($failedEntity) {
26+
$message = "Skipping search ID: $SearchId for tenant: $TenantFilter - Previous attempt failed within the last 4 hours"
27+
Write-LogMessage -API "AuditLog" -tenant $TenantFilter -message $message -Sev "Info"
28+
Write-Information $message
29+
exit 0
30+
}
31+
}
32+
catch {
33+
Write-Information "Error checking for failed downloads: $($_.Exception.Message)"
34+
# Continue with the process even if the rerun protection check fails
35+
}
36+
1737
try {
1838
Write-Information "Starting audit log cache process for tenant: $TenantFilter"
1939
$CacheWebhooksTable = Get-CippTable -TableName 'CacheWebhooks'
@@ -43,6 +63,21 @@ function New-CIPPAuditLogSearchResultsCache {
4363
}
4464
Write-Information "Successfully cached search ID: $($item.id) for tenant: $TenantFilter"
4565
} catch {
66+
# Record this failed attempt in the FailedAuditLogDownloads table
67+
try {
68+
$FailedDownloadsTable = Get-CippTable -TableName 'FailedAuditLogDownloads'
69+
$failedEntity = @{
70+
RowKey = [guid]::NewGuid().ToString()
71+
PartitionKey = $TenantFilter
72+
SearchId = $SearchId
73+
ErrorMessage = [string]$_.Exception.Message
74+
}
75+
Add-CIPPAzDataTableEntity @FailedDownloadsTable -Entity $failedEntity -Force
76+
Write-Information "Recorded failed download for search ID: $SearchId, tenant: $TenantFilter"
77+
}
78+
catch {
79+
Write-Information "Failed to record download failure: $($_.Exception.Message)"
80+
}
4681
throw $_
4782
}
4883

0 commit comments

Comments
 (0)