Source code for django_prodserver.backends.base

from collections.abc import Collection, Mapping
from typing import Any


[docs] class BaseProcessBackend: """ Base class to configure an individual process backend. You are required to override ``start_server`` in the subclass. Most backends should subclass :class:`BaseServerBackend` (for web/ASGI/WSGI servers) or :class:`BaseWorkerBackend` (for background task workers) rather than this class directly so the ``server`` and ``worker`` management commands can tell the two apart. { "BACKEND": "django_prodserver.backends.servers.gunicorn.GunicornServer", "ARGS": {"bind": "0.0.0.0:8111"} } """ def __init__(self, **server_args: Any) -> None: self.args = self._format_server_args_from_dict(server_args.get("ARGS", {}))
[docs] def start_server(self, *args: str) -> None: """ Function is called to start the process directly. This must be implemented in the subclass """ raise NotImplementedError
[docs] def prep_server_args(self) -> list[str]: """ Here we customisation of the arguments passed to the server process. Typically this is where fixed arguments are inserted into the args """ return self.args
def _format_server_args_from_dict( self, args: str | Mapping[str, str | Collection[str] | None] ) -> list[str]: """ Formatting server process arguments coming from settings. This function transforms the dictionary settings configuration from: { "bind": "0.0.0.0:8111", "preload": None, } to [ "--bind=0.0.0.0:8111", "--preload", ] """ if isinstance(args, str): return [args] def format_arg(arg_name: str, arg_value: str | Collection[str] | None) -> str: if arg_value is None: return f"--{arg_name}" return f"--{arg_name}={arg_value}" return [format_arg(arg_name, arg_value) for arg_name, arg_value in args.items()]
# def run_from_argv(self, argv): # TODO: The below should be looked into and implemented # if getattr(settings, "WEBSERVER_WARMUP", True): # app = get_internal_wsgi_application() # if getattr(settings, "WEBSERVER_WARMUP_HEALTHCHECK", None): # wsgi_healthcheck(app, settings.WEBSERVER_WARMUP_HEALTHCHECK) # # self.start_server(*self.prep_server_args())
[docs] class BaseServerBackend(BaseProcessBackend): """ Base class for web server backends (WSGI / ASGI). Backends that subclass this are runnable via ``python manage.py server``. """
[docs] class BaseWorkerBackend(BaseProcessBackend): """ Base class for background task worker backends. Backends that subclass this are runnable via ``python manage.py worker``. """