Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

uwsgi: Force Django load before returning the uwsgi worker function.

Alex Vandivereef65d7e3048
diff --git a/zproject/wsgi.py b/zproject/wsgi.py
index b2a41747351..991a2ff7304 100644
--- a/zproject/wsgi.py
+++ b/zproject/wsgi.py
@@ -25,6 +25,9 @@ setup_path()
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zproject.settings")
+from collections.abc import Callable
+from typing import Any
+
from django.core.wsgi import get_wsgi_application
try:
@@ -33,6 +36,37 @@ try:
# setting points here.
application = get_wsgi_application()
+
+ # We force loading of the main parts of the application now, by
+ # handing it a fake request, rather than have to pay that price
+ # during the first request served by this process. Hitting the
+ # /health endpoint will not only load Django and all of the views
+ # (by loading the URL dispatcher) but will also force open any
+ # lazy-loaded service connections.
+ #
+ # The return value (and thus response status) of this healthcheck
+ # request is ignored, so we do return the application handler even
+ # if connections are not fully available yet. This at least
+ # allows application logging to handle any such errors, instead of
+ # arcane errors from uwsgi not being able to load its handler
+ # function.
+ def ignored_start_response(
+ status: str, headers: list[tuple[str, str]], exc_info: Any = None, /
+ ) -> Callable[[bytes], object]:
+ return lambda x: None
+
+ application(
+ {
+ "REQUEST_METHOD": "GET",
+ "SERVER_NAME": "127.0.0.1",
+ "SERVER_PORT": "443",
+ "PATH_INFO": "/health",
+ "REMOTE_ADDR": "127.0.0.1",
+ "wsgi.input": sys.stdin,
+ "wsgi.url_scheme": "https",
+ },
+ ignored_start_response,
+ )
except Exception:
# If /etc/zulip/settings.py contains invalid syntax, Django
# initialization will fail in django.setup(). In this case, our