Files
mtusi2026lab6/scripts/incident-worker-stop.ps1
T

86 lines
3.5 KiB
PowerShell

param(
[int]$DurationSeconds = 150,
[int]$JobCount = 10,
[string]$BaseUrl = "http://localhost:8080",
[string]$EnvFile = ".env"
)
$ErrorActionPreference = "Stop"
$project = Split-Path -Parent $PSScriptRoot
$stamp = Get-Date -Format "yyyyMMdd-HHmmss"
$evidenceDir = Join-Path $project "evidence\lab6-$stamp-worker-stop"
New-Item -ItemType Directory -Force -Path $evidenceDir | Out-Null
Push-Location $project
try {
$workerContainer = docker compose --env-file $EnvFile ps -q worker
if (-not $workerContainer) {
throw "worker container does not exist; start the stack first"
}
docker stop $workerContainer
python tests/attachment_load.py --base-url $BaseUrl --count $JobCount `
--output (Join-Path $evidenceDir "load.json")
Start-Sleep -Seconds $DurationSeconds
$failureAlerts = Invoke-RestMethod "http://localhost:9090/api/v1/alerts"
$firingQueueAlert = @($failureAlerts.data.alerts | Where-Object {
$_.labels.alertname -eq "AttachmentQueueLagHigh" -and $_.state -eq "firing"
})
$queries = [ordered]@{
captured_at = (Get-Date).ToUniversalTime().ToString("o")
queue_depth = Invoke-RestMethod "http://localhost:9090/api/v1/query?query=max(messenger_queue_depth)"
oldest_job_age = Invoke-RestMethod "http://localhost:9090/api/v1/query?query=max(messenger_queue_oldest_job_age_seconds)"
alerts = $failureAlerts
alert_firing = ($firingQueueAlert.Count -gt 0)
}
$queries | ConvertTo-Json -Depth 20 | Set-Content -Encoding utf8 `
(Join-Path $evidenceDir "failure-observation.json")
if ($firingQueueAlert.Count -eq 0) {
throw "AttachmentQueueLagHigh did not reach firing state"
}
}
finally {
if ($workerContainer) {
docker start $workerContainer
}
$deadline = (Get-Date).AddMinutes(4)
do {
Start-Sleep -Seconds 5
try {
$depth = Invoke-RestMethod "http://localhost:9090/api/v1/query?query=max(messenger_queue_depth)"
$remaining = [double]$depth.data.result[0].value[1]
}
catch {
$remaining = [double]::PositiveInfinity
}
} while ($remaining -gt 0 -and (Get-Date) -lt $deadline)
$alertDeadline = (Get-Date).AddMinutes(2)
do {
Start-Sleep -Seconds 5
$recoveryAlerts = Invoke-RestMethod "http://localhost:9090/api/v1/alerts"
$queueAlert = @($recoveryAlerts.data.alerts | Where-Object {
$_.labels.alertname -eq "AttachmentQueueLagHigh"
})
} while ($queueAlert.Count -gt 0 -and (Get-Date) -lt $alertDeadline)
try {
$recovery = [ordered]@{
captured_at = (Get-Date).ToUniversalTime().ToString("o")
queue_depth = Invoke-RestMethod "http://localhost:9090/api/v1/query?query=max(messenger_queue_depth)"
oldest_job_age = Invoke-RestMethod "http://localhost:9090/api/v1/query?query=max(messenger_queue_oldest_job_age_seconds)"
jobs = Invoke-RestMethod "http://localhost:9090/api/v1/query?query=sum%20by%20(outcome)%20(increase(messenger_jobs_processed_total%5B10m%5D))"
alerts = $recoveryAlerts
alert_resolved = ($queueAlert.Count -eq 0)
}
$recovery | ConvertTo-Json -Depth 20 | Set-Content -Encoding utf8 `
(Join-Path $evidenceDir "recovery-observation.json")
if ($queueAlert.Count -gt 0) {
throw "AttachmentQueueLagHigh did not resolve before the deadline"
}
}
finally {
Pop-Location
}
}
Write-Host "Evidence saved to $evidenceDir"