Recover failed port-forward during yearly fetch
build / build (push) Successful in 3m42s

This commit is contained in:
2026-09-30 17:26:10 +02:00
parent 29e1ae34fa
commit 8a20309263
+43 -11
View File
@@ -43,38 +43,65 @@ cleanup() {
if [[ -n $forward_pid ]]; then if [[ -n $forward_pid ]]; then
kill "$forward_pid" 2>/dev/null || true kill "$forward_pid" 2>/dev/null || true
wait "$forward_pid" 2>/dev/null || true wait "$forward_pid" 2>/dev/null || true
forward_pid=
fi fi
} }
trap cleanup EXIT trap cleanup EXIT
trap 'exit 130' INT trap 'exit 130' INT
trap 'exit 143' TERM trap 'exit 143' TERM
if ! service_ready; then start_forward() {
local attempt
cleanup
if ! command -v kubectl >/dev/null 2>&1; then if ! command -v kubectl >/dev/null 2>&1; then
echo "Service unavailable on localhost:4101 and kubectl is not installed." >&2 echo "Service unavailable on localhost:4101 and kubectl is not installed." >&2
exit 1 return 1
fi fi
echo "Starting kubectl port-forward for sejs/btcdata..." >&2 echo "Starting kubectl port-forward for sejs/btcdata..." >&2
kubectl -n sejs port-forward svc/btcdata 4101:4101 >&2 & kubectl -n sejs port-forward svc/btcdata 4101:4101 >&2 &
forward_pid=$! forward_pid=$!
ready=false
for (( attempt = 0; attempt < 30; attempt++ )); do for (( attempt = 0; attempt < 30; attempt++ )); do
if ! kill -0 "$forward_pid" 2>/dev/null; then if ! kill -0 "$forward_pid" 2>/dev/null; then
echo "Port-forward exited before the service became ready." >&2 echo "Port-forward exited before the service became ready." >&2
exit 1 cleanup
return 1
fi fi
if service_ready; then if service_ready; then
ready=true return 0
break
fi fi
sleep 1 sleep 1
done done
if [[ $ready != true ]]; then
echo "Timed out waiting for the service on localhost:4101." >&2 echo "Timed out waiting for the service on localhost:4101." >&2
exit 1 cleanup
return 1
}
ensure_service() {
local retry
if service_ready; then
return 0
fi fi
for (( retry = 1; retry <= 3; retry++ )); do
echo "Recovering port-forward (attempt $retry/3)..." >&2
if start_forward; then
return 0
fi fi
if (( retry < 3 )); then
sleep 2
fi
done
echo "Unable to restore the service on localhost:4101." >&2
return 1
}
fetch_date() {
curl --fail --silent --show-error --connect-timeout 5 -X GET \
"http://localhost:4101/api/price/day/$1" \
-H "accept: application/json" --write-out '\n'
}
ensure_service || exit 1
# Generate dates without relying on platform-specific date command options. # Generate dates without relying on platform-specific date command options.
status=0 status=0
@@ -90,9 +117,14 @@ for (( month_index = 0; month_index < 12; month_index++ )); do
day=$((days - day_index)) day=$((days - day_index))
fi fi
printf -v date '%s-%02d-%02d' "$year" "$month" "$day" printf -v date '%s-%02d-%02d' "$year" "$month" "$day"
if ! curl --fail --silent --show-error -X GET \ if ! fetch_date "$date"; then
"http://localhost:4101/api/price/day/$date" \ if ! service_ready; then
-H "accept: application/json" --write-out '\n'; then ensure_service || exit 1
echo "Retrying $date after tunnel recovery..." >&2
if fetch_date "$date"; then
continue
fi
fi
echo "Failed to fetch $date" >&2 echo "Failed to fetch $date" >&2
status=1 status=1
fi fi