diff options
author | 2024-06-07 21:28:29 +0100 | |
---|---|---|
committer | 2024-06-07 21:28:29 +0100 | |
commit | 369853209f930cdd6238d4314a0c67441335d01b (patch) | |
tree | 0536ea0dd7ceccca6a156f561c525ef0792b17e2 | |
parent | Merge pull request #1335 from python-discord/dependabot/pip/ruff-0.4.8 (diff) | |
parent | Update taskipy start task to remove redundant --debug flag (diff) |
Merge pull request #1338 from python-discord/jb3/deployment/gunicorn-from-docker
Update production deployment configuration
-rw-r--r-- | Dockerfile | 8 | ||||
-rw-r--r-- | docker-compose.yml | 2 | ||||
-rwxr-xr-x | manage.py | 65 | ||||
-rw-r--r-- | pyproject.toml | 2 |
4 files changed, 28 insertions, 49 deletions
@@ -39,6 +39,8 @@ RUN if [ $STATIC_BUILD = "TRUE" ] ; \ then SECRET_KEY=dummy_value poetry run python manage.py distill-local build --traceback --force ; \ fi -# Run web server through custom manager -ENTRYPOINT ["poetry", "run", "python", "manage.py"] -CMD ["run"] +ENTRYPOINT ["poetry", "run"] +CMD ["gunicorn", "--preload", "-b", "0.0.0.0:8000", \ + "pydis_site.wsgi:application", "-w", "2", "--statsd-host", \ + "graphite.default.svc.cluster.local:8125", "--statsd-prefix", "site", \ + "--config", "file:gunicorn.conf.py"] diff --git a/docker-compose.yml b/docker-compose.yml index 61554ae4..d2192f5c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -30,7 +30,7 @@ services: build: context: . dockerfile: Dockerfile - command: ["run", "--debug"] + command: ["python", "manage.py", "run"] networks: default: aliases: @@ -28,21 +28,21 @@ for key, value in DEFAULT_ENVS.items(): class SiteManager: """ - Manages the preparation and serving of the website. + Manages the preparation and serving of the website for local use. - Handles both development and production environments. + This class is used solely for setting up the development + environment. In production, gunicorn is invoked directly + and migrations are handled in an init container. Usage: manage.py run [option]... Options: - --debug Runs a development server with debug mode enabled. --silent Sets minimal console output. --verbose Sets verbose console output. """ def __init__(self, args: list[str]): - self.debug = "--debug" in args self.silent = "--silent" in args if self.silent: @@ -50,9 +50,8 @@ class SiteManager: else: self.verbosity = 2 if "--verbose" in args else 1 - if self.debug: - os.environ.setdefault("DEBUG", "true") - print("Starting in debug mode.") + os.environ.setdefault("DEBUG", "true") + print("Starting in debug mode.") @staticmethod def create_superuser() -> None: @@ -104,53 +103,31 @@ class SiteManager: call_command("migrate", verbosity=self.verbosity) def prepare_server(self) -> None: - """Preform runserver-specific preparation tasks.""" - if self.debug: - # In Production, collectstatic is ran in the Docker image - print("Collecting static files.") - call_command( - "collectstatic", - interactive=False, - clear=True, - verbosity=self.verbosity - 1 - ) + """Perform debug runserver-specific preparation tasks.""" + print("Collecting static files.") + call_command( + "collectstatic", + interactive=False, + clear=True, + verbosity=self.verbosity - 1 + ) - self.set_dev_site_name() - self.create_superuser() + self.set_dev_site_name() + self.create_superuser() - def run_server(self) -> None: - """Prepare and run the web server.""" + def run_debug(self) -> None: + """Prepare and run the debug web server.""" in_reloader = os.environ.get('RUN_MAIN') == 'true' # Prevent preparing twice when in dev mode due to reloader - if not self.debug or in_reloader: + if in_reloader: self.prepare_environment() self.prepare_server() print("Starting server.") # Run the development server - if self.debug: - call_command("runserver", "0.0.0.0:8000") - return - - # Import gunicorn only if we aren't in debug mode. - import gunicorn.app.wsgiapp - - # Patch the arguments for gunicorn - sys.argv = [ - "gunicorn", - "--preload", - "-b", "0.0.0.0:8000", - "pydis_site.wsgi:application", - "-w", "2", - "--statsd-host", "graphite.default.svc.cluster.local:8125", - "--statsd-prefix", "site", - "--config", "file:gunicorn.conf.py" - ] - - # Run gunicorn for the production server. - gunicorn.app.wsgiapp.run() + call_command("runserver", "0.0.0.0:8000") def run_tests(self) -> None: """Prepare and run the test suite.""" @@ -190,7 +167,7 @@ def main() -> None: if len(sys.argv) > 1 and sys.argv[1] in ("run", "test"): manager = SiteManager(sys.argv) if sys.argv[1] == "run": - manager.run_server() + manager.run_debug() elif sys.argv[1] == "test": manager.run_tests() diff --git a/pyproject.toml b/pyproject.toml index 73f15977..4d647855 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -73,7 +73,7 @@ select = ["ANN", "B", "C4", "D", "DJ", "DTZ", "E", "F", "ISC", "INT", "N", "PGH" "pydis_site/apps/api/models/bot/off_topic_channel_name.py" = ["RUF001"] [tool.taskipy.tasks] -start = "python manage.py run --debug" +start = "python manage.py run" makemigrations = "python manage.py makemigrations" django_shell = "python manage.py shell" test = "coverage run manage.py test" |