|
| 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 | + |
0 commit comments