Dispatcharr/dispatcharr/wsgi.py
Endoze 9b6c793b11
fix(wsgi): ensure gevent monkey-patching before Django import chain
Celery's Redis connection pool was silently failing in modular
deployment mode because Django's setup imports celery.py before uWSGI's
gevent plugin reliably patches stdlib sockets. Moving patch_all() to the
top of wsgi.py guarantees all broker connections are gevent-aware
regardless of uWSGI initialization order.
2026-02-26 10:23:50 -05:00

23 lines
944 B
Python

"""
WSGI config for dispatcharr project.
"""
# When running under uWSGI with gevent, ensure monkey-patching is fully
# applied before any other imports. Django's setup triggers the import of
# dispatcharr/__init__.py → celery.py, which initialises the Celery broker
# transport. If those imports happen before sockets are patched, Celery's
# Redis connection pool holds unpatched sockets that silently fail under
# gevent's cooperative scheduling. Calling patch_all() here—before the
# Django import chain—guarantees every socket (including pooled broker
# connections) is gevent-aware. The call is idempotent, so it's harmless
# if uWSGI's gevent plugin has already patched.
try:
from gevent import monkey
monkey.patch_all()
except ImportError:
pass
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dispatcharr.settings')
application = get_wsgi_application()