From 02cf53383d714575eef0d02aa727633e59ebbd29 Mon Sep 17 00:00:00 2001 From: dekzter Date: Sun, 2 Mar 2025 14:56:18 -0500 Subject: [PATCH] fixing docker build --- docker/Dockerfile | 4 +- docker/entrypoint.aio.sh | 123 +++++++++++++++++++++++++++++++++------ uwsgi.aio.ini | 19 ------ uwsgi.dev.ini | 20 ------- uwsgi.ini | 16 ----- 5 files changed, 105 insertions(+), 77 deletions(-) delete mode 100644 uwsgi.aio.ini delete mode 100644 uwsgi.dev.ini delete mode 100644 uwsgi.ini diff --git a/docker/Dockerfile b/docker/Dockerfile index 9db6b017..ef9799e7 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -15,9 +15,7 @@ RUN apt-get update && \ libpq-dev \ lsb-release \ python3-virtualenv \ - streamlink \ - uwsgi \ - python3-django-uwsgi + streamlink RUN \ curl -sL https://deb.nodesource.com/setup_23.x -o /tmp/nodesource_setup.sh && \ diff --git a/docker/entrypoint.aio.sh b/docker/entrypoint.aio.sh index f5b37172..244df3ab 100755 --- a/docker/entrypoint.aio.sh +++ b/docker/entrypoint.aio.sh @@ -1,20 +1,105 @@ -#!/bin/sh +#!/bin/bash -# Check the value of DISPATCHARR_ENV and run the corresponding program -case "$DISPATCHARR_ENV" in - "dev") - echo "DISPATCHARR_ENV is set to 'dev'. Running Development Program..." - apt-get update && apt-get install -y nodejs - cd /app/frontend && npm install - cd /app - exec /usr/bin/uwsgi --ini uwsgi.dev.ini - ;; - "aio") - echo "DISPATCHARR_ENV is set to 'aio'. Running All-in-One Program..." - exec /usr/bin/uwsgi --ini uwsgi.aio.ini - ;; - *) - echo "DISPATCHARR_ENV is not set or has an unexpected value. Running standalone..." - exec /usr/bin/uwsgi --ini uwsgi.ini - ;; -esac +# Run Django migrations and collect static files +python manage.py collectstatic --noinput +python manage.py migrate --noinput + +# Function to clean up only running processes +cleanup() { + echo "🔥 Cleanup triggered! Stopping services..." + for pid in "${pids[@]}"; do + if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then + echo "⛔ Stopping process (PID: $pid)..." + kill -TERM "$pid" 2>/dev/null + else + echo "✅ Process (PID: $pid) already stopped." + fi + done + wait +} + +# Catch termination signals (CTRL+C, Docker Stop, etc.) +trap cleanup TERM INT + +# Initialize an array to store PIDs +pids=() + +GUNICORN_PORT=9191 + +# If running in development mode, install and start frontend +if [ "$DISPATCHARR_ENV" = "dev" ]; then + echo "🚀 Development Mode - Setting up Frontend..." + GUNICORN_PORT=5656 + + # Install Node.js + apt-get update && apt-get install -y nodejs + + # Install frontend dependencies + cd /app/frontend && npm install + cd /app + + # Start React development server + echo "🚀 Starting React Dev Server..." + cd /app/frontend + PORT=9191 ./node_modules/pm2/bin/pm2 --name test start npm -- start + ./node_modules/pm2/bin/pm2 logs & + react_pid=$! + echo "✅ React started with PID $react_pid" + pids+=("$react_pid") + cd /app +fi + +# If running in `dev` or `aio`, start Redis and Celery +if [ "$DISPATCHARR_ENV" = "dev" ] || [ "$DISPATCHARR_ENV" = "aio" ]; then + echo "🚀 Running Redis and Celery for '$DISPATCHARR_ENV'..." + + # Start Redis + echo "🚀 Starting Redis..." + redis-server --daemonize no & + sleep 1 # Give Redis time to start + redis_pid=$(pgrep -x redis-server) + if [ -n "$redis_pid" ]; then + echo "✅ Redis started with PID $redis_pid" + pids+=("$redis_pid") + else + echo "❌ Redis failed to start!" + fi + + # Start Celery + echo "🚀 Starting Celery..." + celery -A dispatcharr worker -l info & + celery_pid=$! + echo "✅ Celery started with PID $celery_pid" + pids+=("$celery_pid") +fi + +# Always start Gunicorn +echo "🚀 Starting Gunicorn..." +gunicorn --workers=4 --worker-class=gevent --timeout=300 --bind 0.0.0.0:${GUNICORN_PORT} dispatcharr.wsgi:application & +gunicorn_pid=$! +echo "✅ Gunicorn started with PID $gunicorn_pid" +pids+=("$gunicorn_pid") + +# Log PIDs +echo "📝 Process PIDs: ${pids[*]}" + +# Wait for at least one process to exit and log the process that exited first +if [ ${#pids[@]} -gt 0 ]; then + echo "⏳ Waiting for processes to exit..." + ps -aux | grep -E 'redis-server|celery|gunicorn|npm' + wait -n "${pids[@]}" + echo "🚨 One of the processes exited! Checking which one..." + + for pid in "${pids[@]}"; do + if ! kill -0 "$pid" 2>/dev/null; then + process_name=$(ps -p "$pid" -o comm=) + echo "❌ Process $process_name (PID: $pid) has exited!" + fi + done +else + echo "❌ No processes started. Exiting." + exit 1 +fi + +# Cleanup and stop remaining processes +cleanup diff --git a/uwsgi.aio.ini b/uwsgi.aio.ini deleted file mode 100644 index 46b722f4..00000000 --- a/uwsgi.aio.ini +++ /dev/null @@ -1,19 +0,0 @@ -[uwsgi] -exec-pre-app = python manage.py collectstatic --noinput -exec-pre-app = python manage.py migrate --noinput - -http-socket = [::]:9191 -buffer-size = 32768 -enable-threads -plugin = python3 -module = dispatcharr.wsgi:application -static-map = /static=staticfiles -thunder-lock -disable-write-exception - -virtualenv = /dispatcharrpy - -max-fd = 10000 - -attach-daemon = celery -A dispatcharr worker -l info -attach-daemon = redis-server diff --git a/uwsgi.dev.ini b/uwsgi.dev.ini deleted file mode 100644 index 8b3415ab..00000000 --- a/uwsgi.dev.ini +++ /dev/null @@ -1,20 +0,0 @@ -[uwsgi] -exec-pre-app = python manage.py collectstatic --noinput -exec-pre-app = python manage.py migrate --noinput - -http-socket = [::]:5656 -buffer-size = 32768 -enable-threads -plugin = python3 -module = dispatcharr.wsgi:application -static-map = /static=staticfiles -thunder-lock -disable-write-exception - -virtualenv = /dispatcharrpy - -max-fd = 10000 - -attach-daemon = celery -A dispatcharr worker -l info -attach-daemon = redis-server -attach-daemon = cd /app/frontend && npm run start diff --git a/uwsgi.ini b/uwsgi.ini deleted file mode 100644 index 188b3bc5..00000000 --- a/uwsgi.ini +++ /dev/null @@ -1,16 +0,0 @@ -[uwsgi] -exec-pre-app = python manage.py collectstatic --noinput -exec-pre-app = python manage.py migrate --noinput - -http-socket = [::]:9191 -buffer-size = 32768 -enable-threads -plugin = python3 -module = dispatcharr.wsgi:application -static-map = /static=staticfiles -thunder-lock -disable-write-exception - -virtualenv = /dispatcharrpy - -max-fd = 10000