forked from mtusi2026/mtusi2026lab3
Lab3 baseline: HAProxy, replicated stateless API, shared PostgreSQL/Valkey, idempotent messages
This commit is contained in:
Executable
+42
@@ -0,0 +1,42 @@
|
||||
#!/bin/sh
|
||||
# Stop exactly the API replica that served TARGET_INSTANCE_ID and leave it
|
||||
# stopped. This is the hook for:
|
||||
# make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
#
|
||||
# X-Instance-Id is the container hostname in lab3, so the mapping is an exact
|
||||
# identity match, not a guess from a header prefix. The script touches only
|
||||
# that one API container: no DB, no sessions, no volumes, no LB.
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
if [ ! -f .env ]; then
|
||||
echo "stop-one.sh: .env is missing; run make env first" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
target=""
|
||||
for container in $(./scripts/compose.sh --env-file .env ps -q api); do
|
||||
hostname=$(docker inspect -f '{{.Config.Hostname}}' "$container" 2>/dev/null || true)
|
||||
if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then
|
||||
target=$container
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -z "$target" ]; then
|
||||
echo "stop-one.sh: no running API container has hostname $TARGET_INSTANCE_ID" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "stop-one.sh: stopping API container $target (instance $TARGET_INSTANCE_ID)"
|
||||
# SIGTERM starts the drain window; compose stop_grace_period is 15 s and the
|
||||
# application's hard shutdown budget is 6 s, so this returns well within the
|
||||
# test's 10 s hook budget. The container stays stopped afterwards.
|
||||
docker stop --time 15 "$target" >/dev/null
|
||||
state=$(docker inspect -f '{{.State.Status}}' "$target")
|
||||
if [ "$state" != "exited" ] && [ "$state" != "dead" ]; then
|
||||
echo "stop-one.sh: container state is $state, expected exited" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "stop-one.sh: container $target is $state; other replicas keep serving"
|
||||
Reference in New Issue
Block a user