Rewrite generate config. Add timeout and retries count
All checks were successful
Deploy DNS Configuration / deploy (push) Successful in 1m44s

This commit is contained in:
Kirill Kodanev 2025-09-15 19:12:14 +03:00
parent ab197cf42a
commit 4aa270dae9

View file

@ -87,10 +87,10 @@ while IFS= read -r line || [ -n "$line" ]; do
normalized_ok=$((normalized_ok+1)) normalized_ok=$((normalized_ok+1))
dbg " -> NORMALIZED: $dom_norm" dbg " -> NORMALIZED: $dom_norm"
dbg " -> Querying API: ${API_URL}${dom_norm}" dbg " -> Querying API: ${API_URL}${dom_norm}"
# параметры retry # параметры retry
max_attempts=3 max_attempts=5
attempt=0 attempt=0
resp="" resp=""
http_code=0 http_code=0
@ -98,55 +98,48 @@ while IFS= read -r line || [ -n "$line" ]; do
while :; do while :; do
attempt=$((attempt+1)) attempt=$((attempt+1))
# делаем запрос, записываем тело и код отдельно raw="$(curl -sS --compressed \
# --compressed: поддержка gzip -m 10 --connect-timeout 5 \
# -sS: показывать ошибки curl в debug (если DEBUG enabled, они попадут в DEBUG_LOG) --retry 3 --retry-delay 1 \
# -m: общий timeout -H 'Accept: application/json' \
# --connect-timeout: timeout на установление соединения -w '\n%{http_code}\n%{content_type}' \
raw="$(curl -sS --compressed -m 20 --connect-timeout 8 -H 'Accept: application/json' -w '\n%{http_code}\n%{content_type}' "${API_URL}${dom_norm}" 2>>"$DEBUG_LOG" )" || true "${API_URL}${dom_norm}" 2>>"$DEBUG_LOG" || true)"
# последний \n%{http_code}\n%{content_type} — отделяем
http_code="$(printf '%s' "$raw" | tail -n1)" http_code="$(printf '%s' "$raw" | tail -n1)"
content_type="$(printf '%s' "$raw" | tail -n2 | head -n1)" content_type="$(printf '%s' "$raw" | tail -n2 | head -n1)"
resp="$(printf '%s' "$raw" | sed '$d' | sed '$d')" # всё кроме двух последних строк resp="$(printf '%s' "$raw" | sed '$d' | sed '$d')"
preview="$(printf '%s' "$resp" | tr '\n' ' ' | cut -c1-400)" preview="$(printf '%s' "$resp" | tr '\n' ' ' | cut -c1-400)"
dbg " -> Attempt #${attempt}: HTTP=${http_code}, Content-Type=${content_type}, preview=${preview}" dbg " -> Attempt #${attempt}: HTTP=${http_code}, Content-Type=${content_type}, preview=${preview}"
# если получили 200 и content-type похож на json — выходим
if [ "$http_code" = "200" ] && printf '%s' "$content_type" | grep -qEi 'application/(json|javascript)|^text/json'; then if [ "$http_code" = "200" ] && printf '%s' "$content_type" | grep -qEi 'application/(json|javascript)|^text/json'; then
break break
fi fi
# если тело выглядит как json (на всякий случай), тоже принимаем
if jq -e . >/dev/null 2>&1 <<<"$resp"; then if jq -e . >/dev/null 2>&1 <<<"$resp"; then
dbg " -> Body is valid JSON despite HTTP=${http_code}, accepting." dbg " -> Body is valid JSON despite HTTP=${http_code}, accepting."
break break
fi fi
# если исчерпали попытки — помечаем как ошибка
if [ "$attempt" -ge "$max_attempts" ]; then if [ "$attempt" -ge "$max_attempts" ]; then
api_error=$((api_error+1)) api_error=$((api_error+1))
ERRORS["$dom_norm"]="http_${http_code}_or_nonjson" ERRORS["$dom_norm"]="http_${http_code}_or_nonjson"
dbg " -> Failed after ${attempt} attempts: HTTP=${http_code}, preview=${preview}" dbg " -> Failed after ${attempt} attempts: HTTP=${http_code}, preview=${preview}"
resp="" # явный маркер resp=""
break break
fi fi
# backoff: 0.5s, 1s, 2s ... # backoff: 1s, 2s, 4s, 8s ...
sleep_time=$(awk "BEGIN {printf \"%.0f\", 0.5 * (2 ^ ($attempt - 1))}") sleep_time=$((2 ** (attempt-1)))
dbg " -> Retry after ${sleep_time}s..." dbg " -> Retry after ${sleep_time}s..."
sleep "$sleep_time" sleep "$sleep_time"
done done
# если нет валидного JSON — пропускаем, с логом
if [ -z "$resp" ] || ! jq -e . >/dev/null 2>&1 <<<"$resp"; then if [ -z "$resp" ] || ! jq -e . >/dev/null 2>&1 <<<"$resp"; then
dbg " -> non-JSON or empty response, skipping domain: $dom_norm" dbg " -> non-JSON or empty response, skipping domain: $dom_norm"
continue continue
fi fi
# error handling
if jq -e 'has("error")' <<<"$resp" >/dev/null; then if jq -e 'has("error")' <<<"$resp" >/dev/null; then
err_msg="$(jq -r '.error' <<<"$resp")" err_msg="$(jq -r '.error' <<<"$resp")"
dbg " -> API error: $err_msg" dbg " -> API error: $err_msg"
@ -165,7 +158,6 @@ while IFS= read -r line || [ -n "$line" ]; do
continue continue
fi fi
# default case: treat as service
DOM_ROLE["$dom_norm"]="service" DOM_ROLE["$dom_norm"]="service"
SOURCES["$dom_norm"]="base" SOURCES["$dom_norm"]="base"
EXPANDED["$dom_norm"]=1 EXPANDED["$dom_norm"]=1
@ -173,7 +165,6 @@ while IFS= read -r line || [ -n "$line" ]; do
continue continue
fi fi
# valid JSON without error -> must be site
api_success=$((api_success+1)) api_success=$((api_success+1))
DOM_ROLE["$dom_norm"]="site" DOM_ROLE["$dom_norm"]="site"
SOURCES["$dom_norm"]="base" SOURCES["$dom_norm"]="base"