data_clickhouse / init_clickhouse.sh
Subham9126's picture
Upload 10 files
3fd3eea verified
Raw
History Blame Contribute Delete
4.04 kB
#!/bin/bash
# ─────────────────────────────────────────────────────────────────────────────
# Market-Data Observatory β€” Init Script
#
# Boot sequence:
# 1. Start ClickHouse server (port 8123, internal)
# 2. Start background refresh loop (git pull every N seconds)
# 3. Start Python gateway (port 7860, public-facing)
# β†’ gateway waits for ClickHouse, then does initial clone + view creation
#
# The gateway handles:
# /api/refresh β†’ on-demand git pull + view recreation
# /api/sources β†’ current config as JSON
# /* β†’ proxy to ClickHouse
# ─────────────────────────────────────────────────────────────────────────────
set -e
CONFIG="/app/sources.yaml"
echo "╔══════════════════════════════════════════════════════════╗"
echo "β•‘ Market-Data Observatory β€” Boot Sequence β•‘"
echo "β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•"
echo ""
# ── Validate config ──────────────────────────────────────────────────────────
if [ ! -f "$CONFIG" ]; then
echo "[FATAL] Config file $CONFIG not found!"
exit 1
fi
REFRESH_INTERVAL=$(yq '.refresh_interval_seconds // 300' "$CONFIG")
SOURCE_COUNT=$(yq '.sources | length' "$CONFIG")
echo "[config] $SOURCE_COUNT source(s), background refresh every ${REFRESH_INTERVAL}s"
echo ""
# ── 1. Start ClickHouse (background, port 8123) ─────────────────────────────
echo "[1/3] Starting ClickHouse on port 8123 (internal)..."
clickhouse server --config-file=/app/clickhouse-config.xml &
CH_PID=$!
echo " PID: $CH_PID"
echo ""
# ── 2. Background auto-refresh loop ─────────────────────────────────────────
if [ "$REFRESH_INTERVAL" -gt 0 ] 2>/dev/null; then
echo "[2/3] Starting background refresh loop (every ${REFRESH_INTERVAL}s)..."
(
# Wait for initial setup to complete before starting loop
sleep "$REFRESH_INTERVAL"
while true; do
echo "[refresh-loop] Syncing all sources..."
python3 /app/refresh_sources.py sync_only > /dev/null 2>&1 || true
sleep "$REFRESH_INTERVAL"
done
) &
else
echo "[2/3] Background refresh loop DISABLED (refresh_interval_seconds is 0)."
echo " You must manually call /api/refresh to update data."
fi
echo ""
# ── 3. Start gateway (foreground, port 7860) ────────────────────────────────
echo "[3/3] Starting gateway on port 7860 (public)..."
echo ""
echo "╔══════════════════════════════════════════════════════════╗"
echo "β•‘ Endpoints: β•‘"
echo "β•‘ /api/refresh β†’ instant git pull + view recreation β•‘"
echo "β•‘ /api/sources β†’ current config as JSON β•‘"
echo "β•‘ /?query=... β†’ ClickHouse SQL (proxied) β•‘"
echo "β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•"
echo ""
# Gateway runs in foreground β€” keeps the container alive
# If it dies, the container restarts
exec python3 /app/gateway.py