#!/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:lab4"); 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 (= stop_grace_period): on SIGTERM the API reports not-ready but keeps # serving for a 3 s drain window, then finishes in-flight requests and exits (~3.5 s). docker stop -t 15 "$found" >/dev/null exit 0