From d0d5d97578d32a5a863b6c2789d4a492cfa2671d Mon Sep 17 00:00:00 2001 From: LUIS NOVO Date: Sun, 14 Dec 2025 11:39:59 -0300 Subject: [PATCH] fix: wait for API to be ready before starting frontend Users reported "Unable to Connect to API Server" errors on startup because the frontend started before the API finished initialization (database migrations, etc.). - Add wait-for-api.sh script that polls /health endpoint - Update supervisord configs to use wait script instead of sleep 5 - Waits up to 5 minutes for API to be ready before starting frontend - Applies to both single-container and multi-container deployments Fixes #315 --- Dockerfile | 4 ++++ Dockerfile.single | 4 ++++ scripts/wait-for-api.sh | 22 ++++++++++++++++++++++ supervisord.conf | 4 ++-- supervisord.single.conf | 4 ++-- 5 files changed, 34 insertions(+), 4 deletions(-) create mode 100755 scripts/wait-for-api.sh diff --git a/Dockerfile b/Dockerfile index 2c6c724..c21540f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -76,6 +76,10 @@ EXPOSE 8502 5055 RUN mkdir -p /app/data +# Copy and make executable the wait-for-api script +COPY scripts/wait-for-api.sh /app/scripts/wait-for-api.sh +RUN chmod +x /app/scripts/wait-for-api.sh + # Copy supervisord configuration COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf diff --git a/Dockerfile.single b/Dockerfile.single index 47ce210..86f98b5 100644 --- a/Dockerfile.single +++ b/Dockerfile.single @@ -77,6 +77,10 @@ COPY --from=builder /app/frontend/public /app/frontend/public # Create directories for data persistence RUN mkdir -p /app/data /mydata +# Copy and make executable the wait-for-api script +COPY scripts/wait-for-api.sh /app/scripts/wait-for-api.sh +RUN chmod +x /app/scripts/wait-for-api.sh + # Expose ports for Frontend and API EXPOSE 8502 5055 diff --git a/scripts/wait-for-api.sh b/scripts/wait-for-api.sh new file mode 100755 index 0000000..2848cd4 --- /dev/null +++ b/scripts/wait-for-api.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# Wait for the API to be healthy before starting the frontend +# This prevents the "Unable to Connect to API Server" error during startup + +API_URL="${INTERNAL_API_URL:-http://localhost:5055}" +MAX_RETRIES=60 # 60 retries * 5 seconds = 5 minutes max wait +RETRY_INTERVAL=5 + +echo "Waiting for API to be ready at ${API_URL}/health..." + +for i in $(seq 1 $MAX_RETRIES); do + if curl -s -f "${API_URL}/health" > /dev/null 2>&1; then + echo "API is ready! Starting frontend..." + exit 0 + fi + echo "Attempt $i/$MAX_RETRIES: API not ready yet, waiting ${RETRY_INTERVAL}s..." + sleep $RETRY_INTERVAL +done + +echo "ERROR: API did not become ready within $((MAX_RETRIES * RETRY_INTERVAL)) seconds" +echo "Starting frontend anyway - users may see connection errors initially" +exit 0 # Exit 0 so frontend still starts (better than nothing) diff --git a/supervisord.conf b/supervisord.conf index f98be12..ea4d1cf 100644 --- a/supervisord.conf +++ b/supervisord.conf @@ -26,7 +26,7 @@ autostart=true startsecs=3 [program:frontend] -command=bash -c "sleep 5 && npm run start" +command=bash -c "/app/scripts/wait-for-api.sh && npm run start" directory=/app/frontend environment=NODE_ENV="production",PORT="8502" passenv=API_URL,NEXT_PUBLIC_API_URL,INTERNAL_API_URL @@ -37,4 +37,4 @@ stderr_logfile_maxbytes=0 autorestart=true priority=30 autostart=true -startsecs=5 +startsecs=10 diff --git a/supervisord.single.conf b/supervisord.single.conf index afb9067..345d37b 100644 --- a/supervisord.single.conf +++ b/supervisord.single.conf @@ -38,7 +38,7 @@ autostart=true startsecs=3 [program:frontend] -command=bash -c "sleep 5 && npm run start" +command=bash -c "/app/scripts/wait-for-api.sh && npm run start" directory=/app/frontend environment=NODE_ENV="production",PORT="8502" passenv=API_URL,NEXT_PUBLIC_API_URL,INTERNAL_API_URL @@ -49,4 +49,4 @@ stderr_logfile_maxbytes=0 autorestart=true priority=30 autostart=true -startsecs=5 \ No newline at end of file +startsecs=10 \ No newline at end of file