fix and update

This commit is contained in:
lendry
2026-06-30 17:01:19 +03:00
parent 31251be877
commit 209036c036
2 changed files with 173 additions and 29 deletions

View File

@@ -1,52 +1,72 @@
#!/bin/sh
# После recreate api-gateway Docker выдаёт новый IP; без reload nginx шлёт на старый адрес → 502.
# После recreate edge-сервисов 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}"
WATCH_HOSTS="${IDP_NGINX_WATCH_HOSTS:-lendry-id-api-gateway:3000/health lendry-id-frontend:3000/}"
log() {
echo "[idp-nginx-watch] $*"
}
wait_for_gateway() {
probe_url() {
target="$1"
host="${target%%:*}"
rest="${target#*:}"
port="${rest%%/*}"
path="/${rest#*/}"
[ "$path" = "/${rest}" ] && path="/"
wget -qO- --timeout=3 "http://${host}:${port}${path}" 2>/dev/null | head -c 64 | grep -q .
}
wait_for_upstreams() {
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})"
ok=1
for target in $WATCH_HOSTS; do
if ! probe_url "$target"; then
ok=0
break
fi
done
if [ "$ok" -eq 1 ]; then
log "upstream-ы доступны"
return 0
fi
attempt=$((attempt + 1))
sleep 1
done
log "WARN: api-gateway не ответил за 120с — nginx стартует без ожидания"
log "WARN: не все upstream-ы ответили за 120с — nginx стартует без ожидания"
return 0
}
watch_gateway() {
last_ip=""
watch_upstreams() {
last_ips=""
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
reload=0
current_ips=""
for target in $WATCH_HOSTS; do
host="${target%%:*}"
ip="$(getent hosts "$host" 2>/dev/null | awk '{print $1}' | head -n1 || true)"
current_ips="${current_ips}${host}=${ip};"
if ! probe_url "$target"; then
log "${host} недоступен — reload nginx"
reload=1
break
fi
done
if [ "$reload" -eq 0 ] && [ -n "$last_ips" ] && [ "$current_ips" != "$last_ips" ]; then
log "IP upstream изменился — reload nginx"
reload=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"
if [ "$reload" -eq 1 ]; then
nginx -s reload 2>/dev/null || true
fi
if [ -n "$current_ip" ]; then
last_ip="$current_ip"
fi
last_ips="$current_ips"
done
}
wait_for_gateway
watch_gateway &
wait_for_upstreams
watch_upstreams &