Files
mtusi2026lab4/scripts/recover-api.sh
T

22 lines
792 B
Bash

#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
docker compose --env-file .env up -d --scale api=2 api lb >/dev/null
deadline=$((SECONDS + 60))
while (( SECONDS < deadline )); do
healthy=0
for container_id in $(docker compose --env-file .env ps -q api); do
state=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$container_id" 2>/dev/null || true)
[[ "$state" == healthy ]] && healthy=$((healthy + 1))
done
if (( healthy >= 2 )) && curl --fail --silent http://localhost:8080/health/ready >/dev/null; then
printf 'PASS: two API replicas are healthy through the load balancer.\n'
exit 0
fi
sleep 1
done
printf 'API replicas did not recover within 60 seconds.\n' >&2
docker compose --env-file .env ps
exit 1