From b72c8d81d4e31f9f8cd4861b9412b10f5773cbd1 Mon Sep 17 00:00:00 2001 From: SpecialX <47072643+wangxiner55@users.noreply.github.com> Date: Thu, 9 Jul 2026 13:58:55 +0800 Subject: [PATCH] fix(infra): use conditional docker template to avoid Health key error Use {{if .State.Health}}...{{end}} instead of direct {{.State.Health.Status}}. --- scripts/health-check.ps1 | 16 ++++++++++------ scripts/start-all.ps1 | 26 ++++++++++++++++---------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/scripts/health-check.ps1 b/scripts/health-check.ps1 index bb7b0b2..f2b16ab 100644 --- a/scripts/health-check.ps1 +++ b/scripts/health-check.ps1 @@ -34,16 +34,20 @@ $infraContainers = @( $infraOk = 0 $infraFail = 0 foreach ($svc in $infraContainers) { - $status = docker inspect --format='{{.State.Health.Status}}' $svc.Container 2>$null - if ($status -eq "healthy") { + $running = docker inspect -f '{{.State.Running}}' $svc.Container 2>$null + if ($running -ne "true") { + Write-Host " [FAIL] $($svc.Name) not running" -ForegroundColor Red + $infraFail++ + continue + } + # 条件模板:Health 不存在时返回空字符串,不报错 + $health = docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{end}}' $svc.Container 2>$null + if ($health -eq "healthy") { Write-Host " [OK] $($svc.Name)" -ForegroundColor Green $infraOk++ - } elseif ($status -eq "running") { + } else { Write-Host " [WARN] $($svc.Name) (running, no healthcheck)" -ForegroundColor Yellow $infraOk++ - } else { - Write-Host " [FAIL] $($svc.Name) (status=$status)" -ForegroundColor Red - $infraFail++ } } diff --git a/scripts/start-all.ps1 b/scripts/start-all.ps1 index f1b3d47..094e26c 100644 --- a/scripts/start-all.ps1 +++ b/scripts/start-all.ps1 @@ -36,22 +36,28 @@ if (-not $SkipInfraCheck) { @{Name="Jaeger"; Container="edu-jaeger"} ) $allHealthy = $true + $prevEAP = $ErrorActionPreference + $ErrorActionPreference = "Continue" foreach ($svc in $infraServices) { - $status = docker inspect --format='{{.State.Health.Status}}' $svc.Container 2>$null - if ($status -eq "healthy") { - Write-Host " [OK] $($svc.Name) ($($svc.Container))" -ForegroundColor Green - } elseif ($status -eq "running") { - Write-Host " [WARN] $($svc.Name) running but no healthcheck" -ForegroundColor Yellow - } else { - Write-Host " [FAIL] $($svc.Name) not started or unhealthy (status=$status)" -ForegroundColor Red - Write-Host " Please start infrastructure first:" -ForegroundColor Red - Write-Host " docker compose -f infra/docker-compose.yml --profile p6 --profile observability up -d" -ForegroundColor Red + $running = docker inspect -f '{{.State.Running}}' $svc.Container 2>$null + if ($running -ne "true") { + Write-Host " [FAIL] $($svc.Name) not running" -ForegroundColor Red $allHealthy = $false + continue + } + # 条件模板:Health 不存在时返回空字符串,不报错 + $health = docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{end}}' $svc.Container 2>$null + if ($health -eq "healthy") { + Write-Host " [OK] $($svc.Name) ($($svc.Container))" -ForegroundColor Green + } else { + Write-Host " [WARN] $($svc.Name) running (no healthcheck)" -ForegroundColor Yellow } } + $ErrorActionPreference = $prevEAP if (-not $allHealthy) { Write-Host "" - Write-Host "Infrastructure not ready. Start it first." -ForegroundColor Red + Write-Host "Infrastructure not ready. Start it first:" -ForegroundColor Red + Write-Host " docker compose -f infra/docker-compose.yml --profile p6 --profile observability up -d" -ForegroundColor White exit 1 } Write-Host ""