diff --git a/docker-compose.yml b/docker-compose.yml index 873de6a5f..48de1f4e5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -55,43 +55,44 @@ services: - db - redis - celery_worker: - build: ./surfsense_backend - # image: ghcr.io/modsetter/surfsense_backend:latest - command: celery -A app.celery_app worker --loglevel=info --concurrency=1 --pool=solo - volumes: - - ./surfsense_backend:/app - - shared_temp:/tmp - env_file: - - ./surfsense_backend/.env - environment: - - DATABASE_URL=postgresql+asyncpg://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@db:5432/${POSTGRES_DB:-surfsense} - - CELERY_BROKER_URL=redis://redis:${REDIS_PORT:-6379}/0 - - CELERY_RESULT_BACKEND=redis://redis:${REDIS_PORT:-6379}/0 - - PYTHONPATH=/app - depends_on: - - db - - redis - - backend + # Run these services seperately in production + # celery_worker: + # build: ./surfsense_backend + # # image: ghcr.io/modsetter/surfsense_backend:latest + # command: celery -A app.celery_app worker --loglevel=info --concurrency=1 --pool=solo + # volumes: + # - ./surfsense_backend:/app + # - shared_temp:/tmp + # env_file: + # - ./surfsense_backend/.env + # environment: + # - DATABASE_URL=postgresql+asyncpg://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@db:5432/${POSTGRES_DB:-surfsense} + # - CELERY_BROKER_URL=redis://redis:${REDIS_PORT:-6379}/0 + # - CELERY_RESULT_BACKEND=redis://redis:${REDIS_PORT:-6379}/0 + # - PYTHONPATH=/app + # depends_on: + # - db + # - redis + # - backend - celery_beat: - build: ./surfsense_backend - # image: ghcr.io/modsetter/surfsense_backend:latest - command: celery -A app.celery_app beat --loglevel=info - volumes: - - ./surfsense_backend:/app - - shared_temp:/tmp - env_file: - - ./surfsense_backend/.env - environment: - - DATABASE_URL=postgresql+asyncpg://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@db:5432/${POSTGRES_DB:-surfsense} - - CELERY_BROKER_URL=redis://redis:${REDIS_PORT:-6379}/0 - - CELERY_RESULT_BACKEND=redis://redis:${REDIS_PORT:-6379}/0 - - PYTHONPATH=/app - depends_on: - - db - - redis - - celery_worker + # celery_beat: + # build: ./surfsense_backend + # # image: ghcr.io/modsetter/surfsense_backend:latest + # command: celery -A app.celery_app beat --loglevel=info + # volumes: + # - ./surfsense_backend:/app + # - shared_temp:/tmp + # env_file: + # - ./surfsense_backend/.env + # environment: + # - DATABASE_URL=postgresql+asyncpg://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@db:5432/${POSTGRES_DB:-surfsense} + # - CELERY_BROKER_URL=redis://redis:${REDIS_PORT:-6379}/0 + # - CELERY_RESULT_BACKEND=redis://redis:${REDIS_PORT:-6379}/0 + # - PYTHONPATH=/app + # depends_on: + # - db + # - redis + # - celery_worker # flower: # build: ./surfsense_backend diff --git a/surfsense_backend/Dockerfile b/surfsense_backend/Dockerfile index 914d1cd4c..cada2bbe9 100644 --- a/surfsense_backend/Dockerfile +++ b/surfsense_backend/Dockerfile @@ -63,10 +63,14 @@ RUN pip install playwright && \ # Copy source code COPY . . +# Copy and set permissions for entrypoint script +COPY scripts/docker/entrypoint.sh /app/scripts/docker/entrypoint.sh +RUN chmod +x /app/scripts/docker/entrypoint.sh + # Prevent uvloop compatibility issues ENV PYTHONPATH=/app ENV UVICORN_LOOP=asyncio # Run EXPOSE 8000 -CMD ["python", "main.py", "--reload"] \ No newline at end of file +CMD ["/app/scripts/docker/entrypoint.sh"] \ No newline at end of file diff --git a/surfsense_backend/scripts/docker/entrypoint.sh b/surfsense_backend/scripts/docker/entrypoint.sh new file mode 100644 index 000000000..85376c49b --- /dev/null +++ b/surfsense_backend/scripts/docker/entrypoint.sh @@ -0,0 +1,33 @@ +#!/bin/bash +set -e + +# Function to handle shutdown gracefully +cleanup() { + echo "Shutting down services..." + kill -TERM "$backend_pid" "$celery_worker_pid" "$celery_beat_pid" 2>/dev/null || true + wait "$backend_pid" "$celery_worker_pid" "$celery_beat_pid" 2>/dev/null || true + exit 0 +} + +trap cleanup SIGTERM SIGINT + +echo "Starting Celery Beat..." +celery -A app.celery_app beat --loglevel=info & +celery_beat_pid=$! + +echo "Starting Celery Worker..." +celery -A app.celery_app worker --loglevel=info --concurrency=1 --pool=solo & +celery_worker_pid=$! + +echo "Starting FastAPI Backend..." +python main.py --reload & +backend_pid=$! + +echo "All services started. PIDs: Backend=$backend_pid, Worker=$celery_worker_pid, Beat=$celery_beat_pid" + +# Wait for any process to exit +wait -n + +# If we get here, one process exited, so exit with its status +exit $? +