This commit is contained in:
+43
-11
@@ -43,38 +43,65 @@ cleanup() {
|
||||
if [[ -n $forward_pid ]]; then
|
||||
kill "$forward_pid" 2>/dev/null || true
|
||||
wait "$forward_pid" 2>/dev/null || true
|
||||
forward_pid=
|
||||
fi
|
||||
}
|
||||
trap cleanup EXIT
|
||||
trap 'exit 130' INT
|
||||
trap 'exit 143' TERM
|
||||
|
||||
if ! service_ready; then
|
||||
start_forward() {
|
||||
local attempt
|
||||
cleanup
|
||||
if ! command -v kubectl >/dev/null 2>&1; then
|
||||
echo "Service unavailable on localhost:4101 and kubectl is not installed." >&2
|
||||
exit 1
|
||||
return 1
|
||||
fi
|
||||
echo "Starting kubectl port-forward for sejs/btcdata..." >&2
|
||||
kubectl -n sejs port-forward svc/btcdata 4101:4101 >&2 &
|
||||
forward_pid=$!
|
||||
|
||||
ready=false
|
||||
for (( attempt = 0; attempt < 30; attempt++ )); do
|
||||
if ! kill -0 "$forward_pid" 2>/dev/null; then
|
||||
echo "Port-forward exited before the service became ready." >&2
|
||||
exit 1
|
||||
cleanup
|
||||
return 1
|
||||
fi
|
||||
if service_ready; then
|
||||
ready=true
|
||||
break
|
||||
return 0
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
if [[ $ready != true ]]; then
|
||||
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
|
||||
for (( retry = 1; retry <= 3; retry++ )); do
|
||||
echo "Recovering port-forward (attempt $retry/3)..." >&2
|
||||
if start_forward; then
|
||||
return 0
|
||||
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.
|
||||
status=0
|
||||
@@ -90,9 +117,14 @@ for (( month_index = 0; month_index < 12; month_index++ )); do
|
||||
day=$((days - day_index))
|
||||
fi
|
||||
printf -v date '%s-%02d-%02d' "$year" "$month" "$day"
|
||||
if ! curl --fail --silent --show-error -X GET \
|
||||
"http://localhost:4101/api/price/day/$date" \
|
||||
-H "accept: application/json" --write-out '\n'; then
|
||||
if ! fetch_date "$date"; then
|
||||
if ! service_ready; 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
|
||||
status=1
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user