Skip to content

Commit 35895bd

Browse files
better rerun protection
1 parent 0524970 commit 35895bd

File tree

1 file changed

+47
-24
lines changed

1 file changed

+47
-24
lines changed

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

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ function New-CIPPAuditLogSearchResultsCache {
2424

2525
if ($failedEntity) {
2626
$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"
27+
Write-LogMessage -API 'AuditLog' -tenant $TenantFilter -message $message -Sev 'Info'
2828
Write-Information $message
2929
exit 0
3030
}
31-
}
32-
catch {
31+
} catch {
3332
Write-Information "Error checking for failed downloads: $($_.Exception.Message)"
3433
# Continue with the process even if the rerun protection check fails
3534
}
@@ -38,18 +37,39 @@ function New-CIPPAuditLogSearchResultsCache {
3837
Write-Information "Starting audit log cache process for tenant: $TenantFilter"
3938
$CacheWebhooksTable = Get-CippTable -TableName 'CacheWebhooks'
4039
$CacheWebhookStatsTable = Get-CippTable -TableName 'CacheWebhookStats'
40+
41+
# Check if we haven't already downloaded this search by checking the cache table
42+
$searchEntity = Get-CIPPAzDataTableEntity @CacheWebhooksTable -Filter "PartitionKey eq '$TenantFilter' and SearchId eq '$SearchId'"
43+
if ($searchEntity) {
44+
Write-Information "Search ID: $SearchId already cached for tenant: $TenantFilter"
45+
exit 0
46+
}
47+
48+
# Record this attempt in the FailedAuditLogDownloads table BEFORE starting the download
49+
# This way, if the function is killed before completion, the record will remain
50+
try {
51+
$FailedDownloadsTable = Get-CippTable -TableName 'FailedAuditLogDownloads'
52+
$attemptId = [guid]::NewGuid().ToString()
53+
$failedEntity = @{
54+
RowKey = $attemptId
55+
PartitionKey = $TenantFilter
56+
SearchId = $SearchId
57+
ErrorMessage = 'Download attempt in progress'
58+
}
59+
Add-CIPPAzDataTableEntity @FailedDownloadsTable -Entity $failedEntity -Force
60+
Write-Information "Recorded download attempt for search ID: $SearchId, tenant: $TenantFilter"
61+
} catch {
62+
Write-Information "Failed to record download attempt: $($_.Exception.Message)"
63+
# Continue with the process even if recording the attempt fails
64+
}
65+
4166
# Start tracking download time
4267
$downloadStartTime = Get-Date
68+
4369
# Process each search and store results in cache
4470
try {
4571
Write-Information "Processing search ID: $($SearchId) for tenant: $TenantFilter"
4672
# Get the search results
47-
#check if we haven't already downloaded this search by checking the cache table, if there are items with the same search id and tenant, we skip this search
48-
$searchEntity = Get-CIPPAzDataTableEntity @CacheWebhooksTable -Filter "PartitionKey eq '$TenantFilter' and SearchId eq '$SearchId'"
49-
if ($searchEntity) {
50-
Write-Information "Search ID: $SearchId already cached for tenant: $TenantFilter"
51-
exit 0
52-
}
5373
$searchResults = Get-CippAuditLogSearchResults -TenantFilter $TenantFilter -QueryId $SearchId
5474
# Store the results in the cache table
5575
foreach ($searchResult in $searchResults) {
@@ -61,23 +81,26 @@ function New-CIPPAuditLogSearchResultsCache {
6181
}
6282
Add-CIPPAzDataTableEntity @CacheWebhooksTable -Entity $cacheEntity -Force
6383
}
64-
Write-Information "Successfully cached search ID: $($item.id) for tenant: $TenantFilter"
65-
} catch {
66-
# Record this failed attempt in the FailedAuditLogDownloads table
84+
Write-Information "Successfully cached search ID: $($SearchId) for tenant: $TenantFilter"
85+
86+
# If we get here, the download was successful, so remove the failed download record
6787
try {
6888
$FailedDownloadsTable = Get-CippTable -TableName 'FailedAuditLogDownloads'
69-
$failedEntity = @{
70-
RowKey = [guid]::NewGuid().ToString()
71-
PartitionKey = $TenantFilter
72-
SearchId = $SearchId
73-
ErrorMessage = [string]$_.Exception.Message
89+
# Get all records for this tenant and search ID
90+
$failedEntities = Get-CIPPAzDataTableEntity @FailedDownloadsTable -Filter "PartitionKey eq '$TenantFilter' and SearchId eq '$SearchId'"
91+
92+
# Remove each record
93+
foreach ($entity in $failedEntities) {
94+
Remove-CIPPAzDataTableEntity @FailedDownloadsTable -Entity $entity
7495
}
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)"
96+
97+
if ($failedEntities) {
98+
Write-Information "Removed failed download records for search ID: $SearchId, tenant: $TenantFilter"
99+
}
100+
} catch {
101+
Write-Information "Failed to remove download attempt record: $($_.Exception.Message)"
80102
}
103+
} catch {
81104
throw $_
82105
}
83106

@@ -90,14 +113,14 @@ function New-CIPPAuditLogSearchResultsCache {
90113
RowKey = $TenantFilter
91114
PartitionKey = 'Stats'
92115
DownloadSecs = [string]$downloadSeconds
93-
SearchCount = [string]$logSearches.Count
116+
SearchCount = [string]($searchResults ? $searchResults.Count : 0)
94117
}
95118

96119
Add-CIPPAzDataTableEntity @CacheWebhookStatsTable -Entity $statsEntity -Force
97120

98121
Write-Information "Completed audit log cache process for tenant: $TenantFilter. Download time: $downloadSeconds seconds"
99122

100-
return $logSearches.Count
123+
return ($searchResults ? $searchResults.Count : 0)
101124
} catch {
102125
Write-Information "Error in New-CIPPAuditLogSearchResultsCache for tenant: $TenantFilter. Error: $($_.Exception.Message)"
103126
throw $_

0 commit comments

Comments
 (0)