stop-one: alias реплик читается параллельно, docker stop в фоне (хук < 10 с на Windows); check_dashboard проверяет Grafana публичным CA

This commit is contained in:
Alexey Bodanyuk
2026-11-28 15:06:29 +03:00
parent 4c9b0f284b
commit 920d9b5694
3 changed files with 30 additions and 17 deletions
+25 -12
View File
@@ -8,7 +8,11 @@
# scripts/restore-api.sh (make restore).
# STOP_MODE=stop (default) SIGTERM: readiness 503, 3 s drain, graceful exit (~4 s)
# STOP_MODE=kill SIGKILL at once (`docker stop -t 0`): crash of the process
# Returns within 10 s. The test hides the output; run by hand to see it (no secrets printed).
# The test requires the hook to return within 10 s. On Windows one `docker exec` costs ~1.8 s, so
# the aliases are read IN PARALLEL, and `docker stop` runs in the background: Docker marks the
# container as stopped by the user at once (the restart policy will not bring it back), the drain
# and exit happen while the test is already sending requests. WAIT=1 waits for the stop (by hand).
# The test hides the output; run by hand to see it (no secrets printed).
set -eu
cd "$(dirname "$0")/.."
export MSYS_NO_PATHCONV=1 # Git Bash: keep /tmp/instance_id a container path
@@ -17,22 +21,31 @@ case "$TARGET_INSTANCE_ID" in
*[!A-Za-z0-9._-]*) echo "stop-one: invalid TARGET_INSTANCE_ID" >&2; exit 2 ;;
esac
mode="${STOP_MODE:-stop}"
case "$mode" in
stop) grace=15 ;;
kill) grace=0 ;;
*) echo "stop-one: STOP_MODE must be stop or kill" >&2; exit 2 ;;
esac
target=""
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT
# Only RUNNING containers of THIS compose project's service `api` are candidates.
for id in $(docker compose ps -q api); do
if [ "$(docker exec "$id" cat /tmp/instance_id 2>/dev/null | tr -d '\r\n')" = "$TARGET_INSTANCE_ID" ]; then
target="$id"
fi
(docker exec "$id" cat /tmp/instance_id 2>/dev/null | tr -d '\r\n' > "$tmp/$id") &
done
wait
target=""
for f in "$tmp"/*; do
[ -f "$f" ] && [ "$(cat "$f")" = "$TARGET_INSTANCE_ID" ] && target="$(basename "$f")"
done
if [ -z "$target" ]; then
echo "stop-one: no running api replica with X-Instance-Id=$TARGET_INSTANCE_ID" >&2
exit 3
fi
name="$(docker inspect -f '{{.Name}}' "$target")"
case "$mode" in
stop) docker stop --time 15 "$target" >/dev/null ;;
kill) docker stop --time 0 "$target" >/dev/null ;;
*) echo "stop-one: STOP_MODE must be stop or kill" >&2; exit 2 ;;
esac
echo "stop-one: $mode $name (X-Instance-Id=$TARGET_INSTANCE_ID) -> $(docker inspect -f '{{.State.Status}} exit={{.State.ExitCode}}' "$target")"
if [ "${WAIT:-0}" = 1 ]; then
docker stop --time "$grace" "$target" >/dev/null
echo "stop-one: $mode $target (X-Instance-Id=$TARGET_INSTANCE_ID) -> $(docker inspect -f '{{.State.Status}} exit={{.State.ExitCode}}' "$target")"
else
nohup docker stop --time "$grace" "$target" >/dev/null 2>&1 &
echo "stop-one: $mode $target (X-Instance-Id=$TARGET_INSTANCE_ID) requested; the container stops in the background"
fi