Files
mtusi2026lab6/scripts/restart.sh

44 lines
1.4 KiB
Bash
Executable File

#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
if [ -f .env ]; then
compose() { docker compose --env-file .env "$@"; }
else
: "${COMPOSE_PROJECT_NAME:?COMPOSE_PROJECT_NAME is required when .env is absent}"
compose() { docker compose "$@"; }
fi
wait_healthy() {
for service in "$@"; do
containers=$(compose ps -q "$service")
[ -n "$containers" ] || { echo "$service has no running containers" >&2; return 1; }
for container in $containers; do
attempts=0
until [ "$(docker inspect --format '{{.State.Health.Status}}' "$container")" = healthy ]; do
attempts=$((attempts + 1))
[ "$attempts" -lt 30 ] || { echo "$service did not become healthy" >&2; return 1; }
sleep 1
done
done
done
}
restart_services() {
for service in "$@"; do
containers=$(compose ps -a -q "$service")
[ -n "$containers" ] || { echo "$service has no containers" >&2; return 1; }
docker restart --timeout 20 $containers >/dev/null
done
}
# Restart only long-running services and preserve their containers and volumes.
compose stop worker relay attachment-cleanup >/dev/null
restart_services postgres redis s3 rabbitmq
wait_healthy postgres redis s3 rabbitmq
restart_services api
wait_healthy api
restart_services lb
restart_services relay worker attachment-cleanup