Skip to content

Commit 2b0e4e0

Browse files
committed
Improved Daily-Maintenance.ps1.
1 parent 156e0b7 commit 2b0e4e0

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed
Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
11
param(
2-
[switch]$Inner
2+
[switch]$Inner,
3+
[switch]$Force
34
)
45

56
$ErrorActionPreference = 'Stop'
67

7-
# Get the script's directory
88
$scriptDir = $PSScriptRoot
9-
10-
Set-Location $scriptDir
11-
129
$lastExecutionFile = Join-Path $scriptDir "last-execution.txt"
1310
$executionLogFile = Join-Path $scriptDir "Daily-Maintenance.log"
1411

1512
# If -Inner is not specified, call this script with -Inner and use Tee-Object for logging
16-
if (-not $Inner) {
17-
& $PSCommandPath -Inner 2>&1 | Tee-Object -FilePath $executionLogFile -Append
13+
if (-not $Inner) {
14+
$scriptArgs = "-Inner"
15+
if ($Force) { $scriptArgs += " -Force" }
16+
Write-Host "Calling $PSCommandPath $scriptArgs for logging."
17+
Invoke-Expression "& '$PSCommandPath' $scriptArgs 2>&1" | Tee-Object -FilePath $executionLogFile -Append
1818
exit $LASTEXITCODE
1919
}
2020

21+
Set-Location $scriptDir
22+
2123
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
2224
Write-Host "[$timestamp] === Daily Maintenance Script Started ==="
2325

2426

2527
# Check if the script has been executed in the last 24 hours
2628
$shouldExecute = $true
27-
if (Test-Path $lastExecutionFile) {
29+
if (-not $Force -and (Test-Path $lastExecutionFile)) {
2830
$lastExecutionTime = (Get-Item $lastExecutionFile).LastWriteTime
2931
$timeSinceLastExecution = (Get-Date) - $lastExecutionTime
3032

3133
if ($timeSinceLastExecution.TotalHours -lt 24) {
32-
Write-Host "Daily maintenance was already executed $([math]::Round($timeSinceLastExecution.TotalHours, 2)) hours ago. Skipping execution."
34+
Write-Host "Daily maintenance was already executed $([math]::Round($timeSinceLastExecution.TotalHours, 2)) hours ago. Skipping execution. Use -Force to execute the script anyway."
35+
Write-Host "Use -Force parameter to override this behavior."
3336
$shouldExecute = $false
3437
}
3538
}
@@ -43,16 +46,47 @@ Write-Host "Executing daily maintenance..."
4346
# Update the last execution timestamp by touching the file
4447
"Last maintenance executed at $(Get-Date)" | Out-File $lastExecutionFile -Encoding UTF8
4548

46-
# Remove all Docker images that have not been used for 7 days.
47-
docker image prune -a --filter "until=168h" --force
49+
# Progressive Docker image pruning based on available disk space
50+
$minFreeSpaceGB = 25
51+
$daysToTry = 7 # Start with 7 days
52+
$minDays = 1 # Don't go below 1 day
53+
54+
Write-Host "Starting progressive Docker image pruning..." -ForegroundColor Cyan
55+
56+
do {
57+
$hoursFilter = $daysToTry * 24
58+
Write-Host "Pruning Docker images unused for $daysToTry days (${hoursFilter}h)..."
59+
docker image prune -a --filter "until=${hoursFilter}h" --force
60+
61+
# Check available free space on C: drive
62+
$drive = Get-PSDrive -Name C
63+
$freeSpaceGB = [math]::Round($drive.Free / 1GB, 2)
64+
Write-Host "Free space on C: drive: $freeSpaceGB GB"
65+
66+
if ($freeSpaceGB -ge $minFreeSpaceGB) {
67+
Write-Host "Sufficient free space available ($freeSpaceGB GB >= $minFreeSpaceGB GB). Docker pruning completed." -ForegroundColor Green
68+
break
69+
}
70+
71+
if ($daysToTry -gt $minDays) {
72+
$daysToTry--
73+
Write-Host "Insufficient free space ($freeSpaceGB GB < $minFreeSpaceGB GB). Trying more aggressive pruning..." -ForegroundColor Yellow
74+
} else {
75+
Write-Host "Reached minimum pruning threshold (1 day). Current free space: $freeSpaceGB GB" -ForegroundColor Red
76+
break
77+
}
78+
79+
} while ($daysToTry -ge $minDays)
4880

4981
# Remove PostSharp and Metalama from the NuGet package cache
5082
# We assume that TeamCity runs as SYSTEM.
83+
Write-Host "Removing PostSharp and Metalama from the NuGet cache..." -ForegroundColor Cyan
5184
$packageDir = "C:\Windows\system32\config\systemprofile\.nuget\packages"
52-
Remove-Item $packageDir/Metalama* -Force -Recurse
53-
Remove-Item $packageDir/PostSharp* -Force -Recurse
85+
Remove-Item $packageDir/Metalama* -Force -Recurse -ErrorAction SilentlyContinue
86+
Remove-Item $packageDir/PostSharp* -Force -Recurse -ErrorAction SilentlyContinue
5487

5588
# Pull this repo.
89+
Write-Host "Pulling scripts from Git..." -ForegroundColor Cyan
5690
git pull
5791

58-
Write-Host "Daily maintenance completed successfully."
92+
Write-Host "Daily maintenance completed successfully." -ForegroundColor Green

0 commit comments

Comments
 (0)