53 lines
1.6 KiB
Bash
53 lines
1.6 KiB
Bash
#!/bin/sh
|
||
# После recreate api-gateway Docker выдаёт новый IP; без reload nginx шлёт на старый адрес → 502.
|
||
set -eu
|
||
|
||
GW_HOST="${IDP_API_GATEWAY_HOST:-lendry-id-api-gateway}"
|
||
GW_PORT="${IDP_API_GATEWAY_PORT:-3000}"
|
||
WATCH_INTERVAL="${IDP_NGINX_WATCH_INTERVAL:-5}"
|
||
|
||
log() {
|
||
echo "[idp-nginx-watch] $*"
|
||
}
|
||
|
||
wait_for_gateway() {
|
||
attempt=0
|
||
while [ "$attempt" -lt 120 ]; do
|
||
if wget -qO- --timeout=3 "http://${GW_HOST}:${GW_PORT}/health" 2>/dev/null | grep -q '"status"'; then
|
||
log "api-gateway доступен (${GW_HOST}:${GW_PORT})"
|
||
return 0
|
||
fi
|
||
attempt=$((attempt + 1))
|
||
sleep 1
|
||
done
|
||
log "WARN: api-gateway не ответил за 120с — nginx стартует без ожидания"
|
||
return 0
|
||
}
|
||
|
||
watch_gateway() {
|
||
last_ip=""
|
||
while true; do
|
||
sleep "$WATCH_INTERVAL"
|
||
current_ip="$(getent hosts "$GW_HOST" 2>/dev/null | awk '{print $1}' | head -n1 || true)"
|
||
healthy=0
|
||
if wget -qO- --timeout=3 "http://${GW_HOST}:${GW_PORT}/health" 2>/dev/null | grep -q '"status"'; then
|
||
healthy=1
|
||
fi
|
||
if [ "$healthy" -eq 0 ]; then
|
||
log "api-gateway недоступен — reload nginx"
|
||
nginx -s reload 2>/dev/null || true
|
||
continue
|
||
fi
|
||
if [ -n "$current_ip" ] && [ -n "$last_ip" ] && [ "$current_ip" != "$last_ip" ]; then
|
||
log "IP ${GW_HOST} изменился (${last_ip} → ${current_ip}) — reload nginx"
|
||
nginx -s reload 2>/dev/null || true
|
||
fi
|
||
if [ -n "$current_ip" ]; then
|
||
last_ip="$current_ip"
|
||
fi
|
||
done
|
||
}
|
||
|
||
wait_for_gateway
|
||
watch_gateway &
|