Files
mtusi2026lab3/scripts/restore.sh
T
2026-11-06 18:09:01 +03:00

27 lines
1.0 KiB
Bash

#!/bin/sh
# Restore the API service to 2 replicas after scripts/stop-one.sh.
set -eu
stack=${STACK_NAME:-lab3}
service="${stack}_api"
# Swarm normally creates the replacement itself after stopping one task. This hook
# restores the declared replica count and waits for two healthy tasks; a forced
# rolling update is used only when the service is still under-replicated.
docker service update --restart-condition any --replicas 2 "$service" >/dev/null
running=$(docker service ps -q "$service" --filter desired-state=running | wc -l | tr -d ' ')
if [ "$running" -lt 2 ]; then
docker service update --force "$service" >/dev/null
fi
i=0
while [ "$i" -lt 60 ]; do
running=$(docker service ps -q "$service" --filter desired-state=running | wc -l | tr -d ' ')
if [ "$running" -ge 2 ] && curl -fsS "http://127.0.0.1:${APP_PORT:-8080}/health/ready" >/dev/null 2>&1; then
printf 'api restored: %s running tasks and readiness OK\n' "$running"
exit 0
fi
i=$((i + 1))
sleep 2
done
printf 'api did not recover within 120s\n' >&2
exit 1