forked from mtusi2026/mtusi2026lab3
Реализовать lab3 на оценку 4: HAProxy, реплики API и общая идемпотентность.
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Stop ONE API replica matching TARGET_INSTANCE_ID (= X-Instance-Id / hostname).
|
||||
# Leave it stopped; do not touch DB/Redis/LB/volumes.
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
found=""
|
||||
# Compose project containers for service api
|
||||
ids=$(docker compose --env-file .env ps -q api 2>/dev/null || true)
|
||||
for id in $ids; do
|
||||
[ -n "$id" ] || continue
|
||||
host=$(docker inspect -f '{{.Config.Hostname}}' "$id")
|
||||
if [ "$host" = "$TARGET_INSTANCE_ID" ]; then
|
||||
found=$id
|
||||
break
|
||||
fi
|
||||
iid=$(docker inspect -f '{{range .Config.Env}}{{println .}}{{end}}' "$id" | sed -n 's/^INSTANCE_ID=//p' | head -n1)
|
||||
if [ -n "$iid" ] && [ "$iid" = "$TARGET_INSTANCE_ID" ]; then
|
||||
found=$id
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -z "$found" ]; then
|
||||
# Fallback: any running container with matching hostname (scaled names differ).
|
||||
for id in $(docker ps -q --filter "ancestor=mtusi-messenger:lab3"); do
|
||||
host=$(docker inspect -f '{{.Config.Hostname}}' "$id")
|
||||
if [ "$host" = "$TARGET_INSTANCE_ID" ]; then
|
||||
found=$id
|
||||
break
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
if [ -z "$found" ]; then
|
||||
printf '%s\n' "stop-one: replica not found for TARGET_INSTANCE_ID" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Graceful stop; API marks not-ready on SIGTERM before exit (uvicorn grace ~8s).
|
||||
docker stop -t 10 "$found" >/dev/null
|
||||
exit 0
|
||||
Reference in New Issue
Block a user