Files
mtusi2026lab3/scripts/rolling-restart.sh
T

36 lines
1.1 KiB
Bash
Executable File

#!/bin/sh
set -eu
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd "$ROOT"
docker compose --env-file .env up -d --scale api=2 --wait --wait-timeout 60
containers=$(docker compose --env-file .env ps -q api)
if [ -z "$containers" ]; then
printf '%s\n' 'No running API containers found.' >&2
exit 1
fi
for container in $containers; do
docker stop --time 15 "$container" >/dev/null
docker start "$container" >/dev/null
attempts=0
while [ "$attempts" -lt 40 ]; do
state=$(docker inspect --format '{{.State.Health.Status}}' "$container")
if [ "$state" = healthy ]; then
printf 'PASS: API container %s restarted and is healthy\n' "$container"
break
fi
if [ "$state" = unhealthy ] || [ "$state" = none ]; then
printf 'API container %s failed health check (state: %s)\n' "$container" "$state" >&2
exit 1
fi
attempts=$((attempts + 1))
sleep 1
done
if [ "$attempts" -eq 40 ]; then
printf 'API container %s did not become healthy in 40 seconds\n' "$container" >&2
exit 1
fi
done