File size: 4,040 Bytes
3fd3eea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/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