From e8d899d2730db701ecf58d680d324bf1204057b7 Mon Sep 17 00:00:00 2001 From: meeb Date: Sun, 14 Aug 2022 15:43:49 +1000 Subject: [PATCH] use container config base dir for cookies when using container local settings, resolves #259 --- config/root/etc/services.d/celery-beat/run | 25 +++++++++++++++++++ config/root/etc/services.d/celery-worker/run | 24 ++++++++++++++++++ tubesync/tubesync/celery.py | 19 ++++++++++++++ tubesync/tubesync/local_settings.py.container | 1 + 4 files changed, 69 insertions(+) create mode 100755 config/root/etc/services.d/celery-beat/run create mode 100755 config/root/etc/services.d/celery-worker/run create mode 100644 tubesync/tubesync/celery.py diff --git a/config/root/etc/services.d/celery-beat/run b/config/root/etc/services.d/celery-beat/run new file mode 100755 index 0000000..46b03a6 --- /dev/null +++ b/config/root/etc/services.d/celery-beat/run @@ -0,0 +1,25 @@ +#!/usr/bin/with-contenv bash + +UMASK_SET=${UMASK_SET:-022} +umask "$UMASK_SET" + +cd /app || exit + +PIDFILE=/run/app/celery-beat.pid +SCHEDULE=/tmp/tubesync-celerybeat-schedule + +if [ -f "${PIDFILE}" ] +then + PID=$(cat $PIDFILE) + echo "Unexpected PID file exists at ${PIDFILE} with PID: ${PID}" + if kill -0 $PID + then + echo "Killing old gunicorn process with PID: ${PID}" + kill -9 $PID + fi + echo "Removing stale PID file: ${PIDFILE}" + rm ${PIDFILE} +fi + +#exec s6-setuidgid app \ +# /usr/local/bin/celery --workdir /app -A tubesync beat --pidfile ${PIDFILE} -s ${SCHEDULE} diff --git a/config/root/etc/services.d/celery-worker/run b/config/root/etc/services.d/celery-worker/run new file mode 100755 index 0000000..04e2a32 --- /dev/null +++ b/config/root/etc/services.d/celery-worker/run @@ -0,0 +1,24 @@ +#!/usr/bin/with-contenv bash + +UMASK_SET=${UMASK_SET:-022} +umask "$UMASK_SET" + +cd /app || exit + +PIDFILE=/run/app/celery-worker.pid + +if [ -f "${PIDFILE}" ] +then + PID=$(cat $PIDFILE) + echo "Unexpected PID file exists at ${PIDFILE} with PID: ${PID}" + if kill -0 $PID + then + echo "Killing old gunicorn process with PID: ${PID}" + kill -9 $PID + fi + echo "Removing stale PID file: ${PIDFILE}" + rm ${PIDFILE} +fi + +#exec s6-setuidgid app \ +# /usr/local/bin/celery --workdir /app -A tubesync worker --pidfile ${PIDFILE} -l INFO diff --git a/tubesync/tubesync/celery.py b/tubesync/tubesync/celery.py new file mode 100644 index 0000000..aad3092 --- /dev/null +++ b/tubesync/tubesync/celery.py @@ -0,0 +1,19 @@ +import os +from celery import Celery + + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tubesync.settings') +REDIS_CONNECTION = os.getenv('REDIS_CONNECTION', 'redis://localhost:6379/0') + + +app = Celery('tubesync') +app.config_from_object('django.conf:settings', namespace='CELERY') +app.autodiscover_tasks() +app.conf.broker_url = REDIS_CONNECTION +app.conf.beat_schedule = { + '10-second-beat': { + 'task': 'sync.tasks.housekeeping_task', + 'schedule': 60.0, + 'args': () + }, +} diff --git a/tubesync/tubesync/local_settings.py.container b/tubesync/tubesync/local_settings.py.container index a105058..b86d04d 100644 --- a/tubesync/tubesync/local_settings.py.container +++ b/tubesync/tubesync/local_settings.py.container @@ -64,6 +64,7 @@ if BACKGROUND_TASK_ASYNC_THREADS > MAX_BACKGROUND_TASK_ASYNC_THREADS: MEDIA_ROOT = CONFIG_BASE_DIR / 'media' DOWNLOAD_ROOT = DOWNLOADS_BASE_DIR YOUTUBE_DL_CACHEDIR = CONFIG_BASE_DIR / 'cache' +COOKIES_FILE = CONFIG_BASE_DIR / 'cookies.txt' BASICAUTH_USERNAME = os.getenv('HTTP_USER', '').strip()