|
5 | 5 | ENABLE_API="${HINDSIGHT_ENABLE_API:-true}" |
6 | 6 | ENABLE_CP="${HINDSIGHT_ENABLE_CP:-true}" |
7 | 7 |
|
8 | | -# Copy pre-cached PostgreSQL data if runtime directory is empty (first run with volume) |
9 | | -if [ "$ENABLE_API" = "true" ]; then |
10 | | - PG0_CACHE="/home/hindsight/.pg0-cache" |
11 | | - PG0_HOME="/home/hindsight/.pg0" |
12 | | - if [ -d "$PG0_CACHE" ] && [ "$(ls -A $PG0_CACHE 2>/dev/null)" ]; then |
13 | | - if [ ! "$(ls -A $PG0_HOME 2>/dev/null)" ]; then |
14 | | - echo "📦 Copying pre-cached PostgreSQL data..." |
15 | | - cp -r "$PG0_CACHE"/* "$PG0_HOME"/ 2>/dev/null || true |
16 | | - fi |
| 8 | +# ============================================================================= |
| 9 | +# Dependency waiting (opt-in via HINDSIGHT_WAIT_FOR_DEPS=true) |
| 10 | +# |
| 11 | +# Problem: When running with LM Studio, the LLM may take time to load models. |
| 12 | +# If Hindsight starts before LM Studio is ready, it fails on LLM verification. |
| 13 | +# This wait loop ensures dependencies are ready before starting. |
| 14 | +# ============================================================================= |
| 15 | +if [ "${HINDSIGHT_WAIT_FOR_DEPS:-false}" = "true" ]; then |
| 16 | + LLM_BASE_URL="${HINDSIGHT_API_LLM_BASE_URL:-http://host.docker.internal:1234/v1}" |
| 17 | + MAX_RETRIES="${HINDSIGHT_RETRY_MAX:-0}" # 0 = infinite |
| 18 | + RETRY_INTERVAL="${HINDSIGHT_RETRY_INTERVAL:-10}" |
| 19 | + |
| 20 | + # Check if external database is configured (skip check for embedded pg0) |
| 21 | + SKIP_DB_CHECK=false |
| 22 | + if [ -z "${HINDSIGHT_API_DATABASE_URL}" ]; then |
| 23 | + SKIP_DB_CHECK=true |
| 24 | + else |
| 25 | + DB_CHECK_HOST=$(echo "$HINDSIGHT_API_DATABASE_URL" | sed -E 's|.*@([^:/]+):([0-9]+)/.*|\1 \2|') |
17 | 26 | fi |
| 27 | + |
| 28 | + check_db() { |
| 29 | + if $SKIP_DB_CHECK; then |
| 30 | + return 0 |
| 31 | + fi |
| 32 | + if command -v pg_isready &> /dev/null; then |
| 33 | + pg_isready -h $(echo $DB_CHECK_HOST | cut -d' ' -f1) -p $(echo $DB_CHECK_HOST | cut -d' ' -f2) &>/dev/null |
| 34 | + else |
| 35 | + python3 -c "import socket; s=socket.socket(); s.settimeout(5); exit(0 if s.connect_ex(('$(echo $DB_CHECK_HOST | cut -d' ' -f1)', $(echo $DB_CHECK_HOST | cut -d' ' -f2))) == 0 else 1)" 2>/dev/null |
| 36 | + fi |
| 37 | + } |
| 38 | + |
| 39 | + check_llm() { |
| 40 | + curl -sf "${LLM_BASE_URL}/models" --connect-timeout 5 &>/dev/null |
| 41 | + } |
| 42 | + |
| 43 | + echo "⏳ Waiting for dependencies to be ready..." |
| 44 | + attempt=1 |
| 45 | + |
| 46 | + while true; do |
| 47 | + db_ok=false |
| 48 | + llm_ok=false |
| 49 | + |
| 50 | + if check_db; then |
| 51 | + db_ok=true |
| 52 | + fi |
| 53 | + |
| 54 | + if check_llm; then |
| 55 | + llm_ok=true |
| 56 | + fi |
| 57 | + |
| 58 | + if $db_ok && $llm_ok; then |
| 59 | + echo "✅ Dependencies ready!" |
| 60 | + break |
| 61 | + fi |
| 62 | + |
| 63 | + if [ "$MAX_RETRIES" -ne 0 ] && [ "$attempt" -ge "$MAX_RETRIES" ]; then |
| 64 | + echo "❌ Max retries ($MAX_RETRIES) reached. Dependencies not available." |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | + |
| 68 | + echo " Attempt $attempt: DB=$( $db_ok && echo 'ok' || echo 'waiting' ), LLM=$( $llm_ok && echo 'ok' || echo 'waiting' )" |
| 69 | + sleep "$RETRY_INTERVAL" |
| 70 | + ((attempt++)) |
| 71 | + done |
18 | 72 | fi |
19 | 73 |
|
20 | 74 | # Track PIDs for wait |
|
0 commit comments