Skip to content

Commit bc720bd

Browse files
committed
improve table cleanup
1 parent 4e46f98 commit bc720bd

File tree

2 files changed

+95
-56
lines changed

2 files changed

+95
-56
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
function Push-TableCleanupTask {
2+
[CmdletBinding(SupportsShouldProcess = $true)]
3+
param (
4+
[Parameter(Mandatory = $true)]
5+
$Item
6+
)
7+
8+
$Type = $Item.Type
9+
Write-Information "#### Starting $($Type) task..."
10+
if ($PSCmdlet.ShouldProcess('Start-TableCleanup', 'Starting Table Cleanup')) {
11+
if ($Type -eq 'DeleteTable') {
12+
$DeleteTables = $Item.Tables
13+
foreach ($Table in $DeleteTables) {
14+
try {
15+
$Table = Get-CIPPTable -tablename $Table
16+
if ($Table) {
17+
Write-Information "Deleting table $($Table.Context.TableName)"
18+
try {
19+
Remove-AzDataTable -Context $Table.Context -Force
20+
} catch {
21+
#Write-LogMessage -API 'TableCleanup' -message "Failed to delete table $($Table.Context.TableName)" -sev Error -LogData (Get-CippException -Exception $_)
22+
}
23+
}
24+
} catch {
25+
Write-Information "Table $Table not found"
26+
}
27+
}
28+
} elseif ($Type -eq 'CleanupRule') {
29+
if ($Item.Where) {
30+
$Where = [scriptblock]::Create($Item.Where)
31+
} else {
32+
$Where = { $true }
33+
}
34+
35+
$DataTableProps = $Item.DataTableProps | ConvertTo-Json | ConvertFrom-Json -AsHashtable
36+
$Table = Get-CIPPTable -tablename $Item.TableName
37+
$CleanupCompleted = $false
38+
do {
39+
Write-Information "Fetching entities from $($Item.TableName) with filter: $($DataTableProps.Filter)"
40+
try {
41+
$Entities = Get-AzDataTableEntity @Table @DataTableProps | Where-Object $Where
42+
if ($Entities) {
43+
Write-Information "Removing $($Entities.Count) entities from $($Item.TableName)"
44+
try {
45+
Remove-AzDataTableEntity @Table -Entity $Entities -Force
46+
if ($DataTableProps.First -and $Entities.Count -lt $DataTableProps.First) {
47+
$CleanupCompleted = $true
48+
}
49+
} catch {
50+
Write-LogMessage -API 'TableCleanup' -message "Failed to remove entities from $($Item.TableName)" -sev Error -LogData (Get-CippException -Exception $_)
51+
$CleanupCompleted = $true
52+
}
53+
} else {
54+
Write-Information "No entities found for cleanup in $($Item.TableName)"
55+
$CleanupCompleted = $true
56+
}
57+
} catch {
58+
Write-Warning "Failed to fetch entities from $($Item.TableName): $($_.Exception.Message)"
59+
$CleanupCompleted = $true
60+
}
61+
} while (!$CleanupCompleted)
62+
} else {
63+
Write-Warning "Unknown task type: $Type"
64+
}
65+
}
66+
Write-Information "#### $($Type) task complete"
67+
}

Modules/CIPPCore/Public/Entrypoints/Timer Functions/Start-TableCleanup.ps1

Lines changed: 28 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,97 +6,69 @@ function Start-TableCleanup {
66
[CmdletBinding(SupportsShouldProcess = $true)]
77
param()
88

9-
$CleanupRules = @(
9+
$Batch = @(
1010
@{
11+
FunctionName = 'TableCleanupTask'
12+
Type = 'CleanupRule'
13+
TableName = 'webhookTable'
1114
DataTableProps = @{
12-
Context = (Get-CIPPTable -tablename 'webhookTable').Context
1315
Property = @('PartitionKey', 'RowKey', 'ETag', 'Resource')
16+
First = 1000
1417
}
1518
Where = "`$_.Resource -match '^Audit'"
1619
}
1720
@{
21+
FunctionName = 'TableCleanupTask'
22+
Type = 'CleanupRule'
23+
TableName = 'AuditLogSearches'
1824
DataTableProps = @{
19-
Context = (Get-CIPPTable -tablename 'AuditLogSearches').Context
2025
Filter = "PartitionKey eq 'Search' and Timestamp lt datetime'$((Get-Date).AddHours(-12).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ'))'"
2126
First = 10000
2227
Property = @('PartitionKey', 'RowKey', 'ETag')
2328
}
2429
}
2530
@{
31+
FunctionName = 'TableCleanupTask'
32+
Type = 'CleanupRule'
33+
TableName = 'CippFunctionStats'
2634
DataTableProps = @{
27-
Context = (Get-CIPPTable -tablename 'CippFunctionStats').Context
2835
Filter = "PartitionKey eq 'Durable' and Timestamp lt datetime'$((Get-Date).AddDays(-7).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ'))'"
2936
First = 10000
3037
Property = @('PartitionKey', 'RowKey', 'ETag')
3138
}
3239
}
3340
@{
41+
FunctionName = 'TableCleanupTask'
42+
Type = 'CleanupRule'
43+
TableName = 'CippQueue'
3444
DataTableProps = @{
35-
Context = (Get-CIPPTable -tablename 'CippQueue').Context
3645
Filter = "PartitionKey eq 'CippQueue' and Timestamp lt datetime'$((Get-Date).AddDays(-7).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ'))'"
3746
First = 10000
3847
Property = @('PartitionKey', 'RowKey', 'ETag')
3948
}
4049
}
4150
@{
51+
FunctionName = 'TableCleanupTask'
52+
Type = 'CleanupRule'
53+
TableName = 'CippQueueTasks'
4254
DataTableProps = @{
43-
Context = (Get-CIPPTable -tablename 'CippQueueTasks').Context
4455
Filter = "PartitionKey eq 'Task' and Timestamp lt datetime'$((Get-Date).AddDays(-7).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ'))'"
4556
First = 10000
4657
Property = @('PartitionKey', 'RowKey', 'ETag')
4758
}
4859
}
49-
)
50-
51-
$DeleteTables = @(
52-
'knownlocationdb'
53-
)
54-
55-
if ($PSCmdlet.ShouldProcess('Start-TableCleanup', 'Starting Table Cleanup')) {
56-
foreach ($Table in $DeleteTables) {
57-
try {
58-
$Table = Get-CIPPTable -tablename $Table
59-
if ($Table) {
60-
Write-Information "Deleting table $($Table.Context.TableName)"
61-
try {
62-
Remove-AzDataTable -Context $Table.Context -Force
63-
} catch {
64-
#Write-LogMessage -API 'TableCleanup' -message "Failed to delete table $($Table.Context.TableName)" -sev Error -LogData (Get-CippException -Exception $_)
65-
}
66-
}
67-
} catch {
68-
Write-Information "Table $Table not found"
69-
}
60+
@{
61+
FunctionName = 'TableCleanupTask'
62+
Type = 'DeleteTable'
63+
Tables = @('knownlocationdb')
7064
}
65+
)
7166

72-
Write-Information 'Starting table cleanup'
73-
foreach ($Rule in $CleanupRules) {
74-
if ($Rule.Where) {
75-
$Where = [scriptblock]::Create($Rule.Where)
76-
} else {
77-
$Where = { $true }
78-
}
79-
$DataTableProps = $Rule.DataTableProps
80-
81-
$CleanupCompleted = $false
82-
do {
83-
$Entities = Get-AzDataTableEntity @DataTableProps | Where-Object $Where
84-
if ($Entities) {
85-
Write-Information "Removing $($Entities.Count) entities from $($Rule.DataTableProps.Context.TableName)"
86-
try {
87-
Remove-AzDataTableEntity -Context $DataTableProps.Context -Entity $Entities -Force
88-
if ($DataTableProps.First -and $Entities.Count -lt $DataTableProps.First) {
89-
$CleanupCompleted = $true
90-
}
91-
} catch {
92-
Write-LogMessage -API 'TableCleanup' -message "Failed to remove entities from $($DataTableProps.Context.TableName)" -sev Error -LogData (Get-CippException -Exception $_)
93-
$CleanupCompleted = $true
94-
}
95-
} else {
96-
$CleanupCompleted = $true
97-
}
98-
} while (!$CleanupCompleted)
99-
}
100-
Write-Information 'Table cleanup complete'
67+
$InputObject = @{
68+
Batch = @($Batch)
69+
OrchestratorName = 'TableCleanup'
70+
SkipLog = $true
10171
}
72+
73+
Start-NewOrchestration -FunctionName 'CIPPOrchestrator' -InputObject ($InputObject | ConvertTo-Json -Depth 5 -Compress)
10274
}

0 commit comments

Comments
 (0)