forked from mtusi2026/mtusi2026lab4
51 lines
1.4 KiB
Bash
Executable File
51 lines
1.4 KiB
Bash
Executable File
#!/bin/sh
|
|
# Graceful shutdown: SIGTERM → /health/ready becomes 503 while process drains.
|
|
# Targets one API container; restores it at the end.
|
|
set -eu
|
|
. "$(dirname "$0")/lib.sh"
|
|
|
|
COMPOSE="docker compose --env-file $ROOT/.env -f $ROOT/compose.yaml"
|
|
|
|
cid=$($COMPOSE ps -q api | head -n1)
|
|
[ -n "$cid" ] || die "no api container"
|
|
|
|
# Confirm ready before drain.
|
|
http_json GET /health/ready
|
|
expect_code 200
|
|
|
|
# docker stop → SIGTERM to PID1 (messenger after entrypoint exec).
|
|
# Probe the draining instance directly (LB may still hit the other replica).
|
|
docker stop -t 15 "$cid" >/dev/null &
|
|
stop_pid=$!
|
|
|
|
saw_drain=0
|
|
i=0
|
|
while [ "$i" -lt 40 ]; do
|
|
code=$(docker exec "$cid" curl -sS -m 2 -o /dev/null -w '%{http_code}' \
|
|
http://127.0.0.1:8080/health/ready 2>/dev/null || printf '000')
|
|
if [ "$code" = "503" ] || [ "$code" = "000" ]; then
|
|
saw_drain=1
|
|
break
|
|
fi
|
|
i=$((i + 1))
|
|
sleep 0.25
|
|
done
|
|
wait "$stop_pid" || true
|
|
[ "$saw_drain" = "1" ] || die "did not observe drain/stop (last code=${code:-none})"
|
|
|
|
# Restore replicas.
|
|
$COMPOSE up -d --no-deps api >/dev/null
|
|
j=0
|
|
HTTP_CODE=000
|
|
while [ "$j" -lt 40 ]; do
|
|
http_json GET /health/ready || true
|
|
if [ "${HTTP_CODE:-}" = "200" ]; then
|
|
break
|
|
fi
|
|
j=$((j + 1))
|
|
sleep 1
|
|
done
|
|
[ "${HTTP_CODE:-}" = "200" ] || die "stand not ready after recreate"
|
|
|
|
printf 'PASS test_graceful_drain\n'
|