From 4666e0938514f15c1836d23a9532e2e80f95bdfa Mon Sep 17 00:00:00 2001 From: Bill Date: Thu, 30 Oct 2025 21:00:12 -0400 Subject: [PATCH] fix: prevent restart loop on missing API keys Add validation at startup to check required environment variables: - OPENAI_API_KEY - ALPHAADVANTAGE_API_KEY - JINA_API_KEY If any are missing, display clear error message with setup instructions and exit immediately (no restart loop). Change restart policy from 'unless-stopped' to 'on-failure:3' to limit restart attempts and prevent endless loops on configuration errors. --- docker-compose.yml | 2 +- entrypoint.sh | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 33eb2f3..9cded4a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -34,4 +34,4 @@ services: - "${TRADE_HTTP_PORT:-8002}:8002" - "${GETPRICE_HTTP_PORT:-8003}:8003" - "${WEB_HTTP_PORT:-8888}:8888" - restart: unless-stopped + restart: on-failure:3 # Restart max 3 times on failure, prevents endless loops diff --git a/entrypoint.sh b/entrypoint.sh index 9c80ea1..604492a 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -3,6 +3,40 @@ set -e # Exit on any error echo "🚀 Starting AI-Trader..." +# Validate required environment variables +echo "🔍 Validating environment variables..." +MISSING_VARS=() + +if [ -z "$OPENAI_API_KEY" ]; then + MISSING_VARS+=("OPENAI_API_KEY") +fi + +if [ -z "$ALPHAADVANTAGE_API_KEY" ]; then + MISSING_VARS+=("ALPHAADVANTAGE_API_KEY") +fi + +if [ -z "$JINA_API_KEY" ]; then + MISSING_VARS+=("JINA_API_KEY") +fi + +if [ ${#MISSING_VARS[@]} -gt 0 ]; then + echo "" + echo "❌ ERROR: Missing required environment variables:" + for var in "${MISSING_VARS[@]}"; do + echo " - $var" + done + echo "" + echo "Please set these variables in your .env file:" + echo " 1. Copy .env.example to .env" + echo " 2. Edit .env and add your API keys" + echo " 3. Restart the container" + echo "" + echo "See docs/DOCKER.md for more information." + exit 1 +fi + +echo "✅ Environment variables validated" + # Step 1: Data preparation echo "📊 Fetching and merging price data..." # Run scripts from /app/scripts but output to /app/data