Files
mtusi2026lab2/scripts/restart.ps1

35 lines
1.7 KiB
PowerShell

# Windows PowerShell version of scripts/restart.sh (restart hook for the persistence test).
# Restarts API and ALL used stores (PostgreSQL, Redis) keeping volumes. No down -v, no flush, no re-seed.
# Container state before/after (StartedAt, PID) is appended to evidence\restart-hook.txt.
$ErrorActionPreference = 'Continue'
$root = Split-Path -Parent $PSScriptRoot
Set-Location $root
$envFile = if ($env:ENV_FILE) { $env:ENV_FILE } else { '.env' }
$log = if ($env:RESTART_EVIDENCE) { $env:RESTART_EVIDENCE } else { Join-Path $root 'evidence\restart-hook.txt' }
function Invoke-Compose {
& docker compose --env-file $envFile @args
if ($LASTEXITCODE -ne 0) { throw ('docker compose ' + ($args -join ' ') + ' failed') }
}
function Get-State {
foreach ($svc in 'api', 'postgres', 'redis') {
$cid = (& docker compose --env-file $envFile ps -q $svc | Select-Object -First 1)
& docker inspect -f ($svc + ' status={{.State.Status}} started={{.State.StartedAt}} pid={{.State.Pid}}') $cid
}
& docker volume ls --format '{{.Name}}' | Where-Object { $_ -match 'pgdata$' } | ForEach-Object { 'volume kept: ' + $_ }
}
try {
Add-Content -Path $log -Encoding UTF8 -Value ('== ' + (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ') + ' before restart')
Add-Content -Path $log -Encoding UTF8 -Value (Get-State)
Invoke-Compose stop api
Invoke-Compose restart postgres redis
Invoke-Compose start api
Add-Content -Path $log -Encoding UTF8 -Value ('== ' + (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ') + ' after restart')
Add-Content -Path $log -Encoding UTF8 -Value (Get-State)
exit 0
} catch {
[Console]::Error.WriteLine($_.Exception.Message)
exit 1
}