From 0f1b6dafc9e9e27667e436e0110c62c4821d89d4 Mon Sep 17 00:00:00 2001 From: tcely Date: Tue, 21 Apr 2026 23:59:02 -0400 Subject: [PATCH] chore(gunicorn): review and update configuration --- .../root/etc/s6-overlay/s6-rc.d/gunicorn/run | 2 +- tubesync/tubesync/gunicorn.py | 74 ++++++++++++------- 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/config/root/etc/s6-overlay/s6-rc.d/gunicorn/run b/config/root/etc/s6-overlay/s6-rc.d/gunicorn/run index d94a0c62..78c05833 100755 --- a/config/root/etc/s6-overlay/s6-rc.d/gunicorn/run +++ b/config/root/etc/s6-overlay/s6-rc.d/gunicorn/run @@ -21,4 +21,4 @@ then fi exec s6-setuidgid app \ - /usr/local/bin/gunicorn -c /app/tubesync/gunicorn.py --capture-output tubesync.wsgi:application + /usr/local/bin/gunicorn --config /app/tubesync/gunicorn.py diff --git a/tubesync/tubesync/gunicorn.py b/tubesync/tubesync/gunicorn.py index 30547212..2c6ebce2 100644 --- a/tubesync/tubesync/gunicorn.py +++ b/tubesync/tubesync/gunicorn.py @@ -2,37 +2,61 @@ import os import multiprocessing -def get_num_workers(): - # Sane max workers to allow to be spawned - cpu_workers = multiprocessing.cpu_count() * 2 + 1 - # But default to 3 - try: - num_workers = int(os.getenv('GUNICORN_WORKERS', 3)) - except ValueError: - num_workers = cpu_workers - if 0 < num_workers < cpu_workers: - return num_workers - else: - return cpu_workers - - def get_bind(): host = os.getenv('LISTEN_HOST', '127.0.0.1') port = os.getenv('LISTEN_PORT', '8080') - return '{}:{}'.format(host, port) + return f'{host}:{port}' + +def get_num_workers(): + # Sane max workers to allow to be spawned + cpu_workers = multiprocessing.cpu_count() * 2 + 1 + # But default to 3 + num_workers = 3 + try: + gunicorn_workers = os.getenv('GUNICORN_WORKERS') + web_concurrency = os.getenv('WEB_CONCURRENCY') + if gunicorn_workers is not None: + try: + num_workers = int(gunicorn_workers) + except: + pass + elif web_concurrency is not None: + try: + num_workers = int(web_concurrency) + except: + pass + except: + pass + return max(1, min(num_workers, cpu_workers)) -workers = get_num_workers() -timeout = 90 -chdir = '/app' +### Configuration +wsgi_app = 'tubesync.wsgi:application' + +##### Logging +# Access logs are printed to stdout from nginx +accesslog = None +errorlog = '-' +loglevel = 'info' +capture_output = True + +##### Process +proc_name = 'gunicorn' daemon = False -control_socket_disable = True -control_socket = '/run/app/gunicorn.ctl' -pidfile = '/run/app/gunicorn.pid' user = 'app' group = 'app' -loglevel = 'info' -errorlog = '-' -accesslog = '/dev/null' # Access logs are printed to stdout from nginx -django_settings = 'django.settings' +chdir = '/app' +control_socket = '/run/app/gunicorn.ctl' +control_socket_disable = True +pidfile = '/run/app/gunicorn.pid' + +##### Server bind = get_bind() +django_settings = 'django.settings' +graceful_timeout = 120 +keepalive = 60 +max_requests = 1000 +max_requests_jitter = 100 +timeout = 90 +worker_class = 'sync' +workers = get_num_workers()