Files
mtusi2026lab6/scripts/restart.ps1
T

42 lines
2.4 KiB
PowerShell

# Windows PowerShell version of scripts/restart.sh (persistence hook): restart LB, ALL API replicas, workers,
# PostgreSQL, Redis, S3, NATS JetStream and observability (Collector, Prometheus, Loki, Tempo, Grafana) keeping volumes. State before/after -> 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 = Join-Path $root 'evidence\restart-hook.txt'
function Dc { & docker compose --env-file $envFile @args 2>$null | Out-Null; if ($LASTEXITCODE -ne 0) { throw ('docker compose ' + ($args -join ' ') + ' failed') } }
function Get-State {
foreach ($cid in (& docker compose --env-file $envFile ps -a -q)) {
& docker inspect -f '{{index .Config.Labels "com.docker.compose.service"}} {{.Name}} status={{.State.Status}} started={{.State.StartedAt}} pid={{.State.Pid}}' $cid
}
& docker volume ls --filter label=com.docker.compose.project=mtusi-lab6 --format 'volume {{.Name}}'
}
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)
$ttl = @(& docker compose --env-file $envFile ps -q api-ttl 2>$null).Count -gt 0
$workers = @(& docker compose --env-file $envFile ps -q worker 2>$null).Count -gt 0
Dc stop lb api worker
if ($ttl) { Dc stop api-ttl }
Dc restart postgres redis s3 nats otel-collector prometheus loki tempo grafana
# Wait for healthy storages (S3: gateway + master leader + writes; NATS: JetStream) before API/worker/LB.
foreach ($svc in 'postgres', 'redis', 's3', 'nats', 'prometheus') {
$cid = (& docker compose --env-file $envFile ps -q $svc | Select-Object -First 1)
for ($i = 0; $i -lt 120; $i++) {
if ((& docker inspect -f '{{.State.Health.Status}}' $cid) -eq 'healthy') { break }
Start-Sleep -Milliseconds 500
}
}
Dc start api
if ($ttl) { Dc start api-ttl }
if ($workers) { Dc start worker }
Dc start lb
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
}