Skip to content

Commit 51eb239

Browse files
committed
polling wait on start
1 parent a60c55b commit 51eb239

File tree

2 files changed

+102
-8
lines changed

2 files changed

+102
-8
lines changed

scripts/start-influxdb-native.sh

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env bash
2+
# Start InfluxDB from a native binary (not Docker)
3+
# Used on macOS-ARM where Docker is not available
4+
5+
set -euo pipefail
6+
7+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
8+
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
9+
10+
cd "${PROJECT_ROOT}"
11+
12+
INFLUXDB_VERSION="${INFLUXDB_VERSION:-1.8.10}"
13+
INFLUXDB_BIN="./influxdb-${INFLUXDB_VERSION}-1/usr/bin/influxd"
14+
INFLUXDB_CONFIG="influxdb.conf"
15+
16+
if [ ! -f "${INFLUXDB_BIN}" ]; then
17+
echo "ERROR: InfluxDB binary not found at ${INFLUXDB_BIN}"
18+
echo "Make sure InfluxDB has been downloaded and extracted."
19+
exit 1
20+
fi
21+
22+
if [ ! -f "${INFLUXDB_CONFIG}" ]; then
23+
echo "ERROR: InfluxDB config not found at ${INFLUXDB_CONFIG}"
24+
exit 1
25+
fi
26+
27+
echo "Starting InfluxDB in background..."
28+
"${INFLUXDB_BIN}" -config "${INFLUXDB_CONFIG}" &
29+
INFLUXDB_PID=$!
30+
echo "InfluxDB PID: ${INFLUXDB_PID}"
31+
32+
# Export PID for cleanup (only if GITHUB_ENV is set, otherwise ignore)
33+
if [ -n "${GITHUB_ENV:-}" ]; then
34+
echo "INFLUXDB_PID=${INFLUXDB_PID}" >> "${GITHUB_ENV}"
35+
fi
36+
37+
# Give InfluxDB a moment to start the process
38+
sleep 1
39+
40+
# Verify the process is still running
41+
if ! kill -0 ${INFLUXDB_PID} 2>/dev/null; then
42+
echo "ERROR: InfluxDB process (PID ${INFLUXDB_PID}) failed to start or died immediately"
43+
exit 1
44+
fi
45+
46+
echo "Waiting for InfluxDB to be ready (polling with exponential backoff)..."
47+
max_attempts=60
48+
attempt=0
49+
wait_seconds=1
50+
51+
while [ $attempt -lt $max_attempts ]; do
52+
# Check if InfluxDB responds to /ping with HTTP 204
53+
# curl -f fails on HTTP error codes, but 204 (No Content) is success (2xx)
54+
# We explicitly check for 204 status code to be sure
55+
http_code=$(curl -s -o /dev/null -w "%{http_code}" -m 2 http://localhost:8086/ping 2>/dev/null || echo "000")
56+
57+
if [ "${http_code}" = "204" ]; then
58+
echo "✓ InfluxDB is ready after $attempt polling attempts!"
59+
exit 0
60+
fi
61+
62+
attempt=$((attempt + 1))
63+
echo "Attempt $attempt/$max_attempts: InfluxDB not ready yet (HTTP ${http_code}), waiting ${wait_seconds} second(s)..."
64+
65+
# Verify process is still running before continuing to wait
66+
if ! kill -0 ${INFLUXDB_PID} 2>/dev/null; then
67+
echo "ERROR: InfluxDB process (PID ${INFLUXDB_PID}) died unexpectedly"
68+
exit 1
69+
fi
70+
71+
sleep $wait_seconds
72+
73+
# Exponential backoff: increase wait time, cap at 5 seconds
74+
wait_seconds=$((wait_seconds + 1))
75+
if [ $wait_seconds -gt 5 ]; then
76+
wait_seconds=5
77+
fi
78+
done
79+
80+
echo "ERROR: InfluxDB failed to become ready after $max_attempts polling attempts"
81+
echo "Last HTTP response code: ${http_code:-unknown}"
82+
exit 1
83+

scripts/start-influxdb.bat

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,31 @@ if errorlevel 1 (
5252
)
5353

5454
echo Waiting for InfluxDB to be ready...
55-
ping 127.0.0.1 -n 8 >nul
56-
57-
REM Check if InfluxDB is responding using PowerShell with timeout
58-
set /a max_attempts=30
55+
REM Poll with exponential backoff: start with short waits, increase gradually
56+
set /a max_attempts=60
5957
set /a attempt=0
58+
set /a wait_seconds=1
6059
:health_check
61-
powershell -Command "try { $r = Invoke-WebRequest -Uri 'http://localhost:8086/ping' -TimeoutSec 1 -UseBasicParsing -ErrorAction Stop; if ($r.StatusCode -eq 204) { exit 0 } else { exit 1 } } catch { exit 1 }" >nul 2>&1
60+
REM Check if InfluxDB responds with HTTP 204 (No Content) which indicates readiness
61+
powershell -Command "try { $r = Invoke-WebRequest -Uri 'http://localhost:8086/ping' -TimeoutSec 2 -UseBasicParsing -ErrorAction Stop; if ($r.StatusCode -eq 204) { exit 0 } else { exit 1 } } catch { exit 1 }" >nul 2>&1
6262
if errorlevel 1 (
6363
set /a attempt+=1
6464
if !attempt! lss %max_attempts% (
65-
ping 127.0.0.1 -n 2 >nul
65+
REM Verify process is still running before continuing
66+
tasklist /FI "IMAGENAME eq influxd.exe" 2>nul | find /I "influxd.exe" >nul
67+
if errorlevel 1 (
68+
echo ERROR: InfluxDB process died unexpectedly
69+
exit /b 1
70+
)
71+
echo Attempt !attempt!/%max_attempts%: InfluxDB not ready yet, waiting !wait_seconds! second^(s^)...
72+
ping 127.0.0.1 -n !wait_seconds! >nul
73+
REM Exponential backoff: cap at 5 seconds
74+
set /a wait_seconds+=1
75+
if !wait_seconds! gtr 5 set /a wait_seconds=5
6676
goto :health_check
6777
)
68-
echo WARNING: InfluxDB may not be fully ready yet
78+
echo ERROR: InfluxDB failed to become ready after %max_attempts% attempts
79+
exit /b 1
6980
) else (
70-
echo InfluxDB is ready!
81+
echo InfluxDB is ready after !attempt! polling attempts!
7182
)

0 commit comments

Comments
 (0)