Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Gunicorn #79

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/decisions/0002-gunicorn.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- Date: 2024-02-09
- Author(s): [Jonathan Moss][jmoss]
- Status: `Active`
- Status: `Obsolete`

## Decision

Expand Down
36 changes: 36 additions & 0 deletions docs/decisions/0004-no-gunicorn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 0002 Remove Gunicorn as a Process Manager

- Date: 2025-01-015
- Author(s): [Jonathan Moss][jmoss]
- Status: `Active`

## Decision

[Uvicorn][uvicorn] now has built in support for process management, so we no longer need
[Gunicorn][gunicorn] as well.

## Context

An ASGI runtime is needed to support asyncio operations in Django. We currently use
Uvicorn for this. Previously Uvicorn ran as a single process and it was recommended that
Gunicorn was used as a process manager, configured to use Uvicorn purely for the
workers. This is no longer the case, as of release [0.30.0][release] Uvicorn now has
built in process management support.

## Implications

This removes one of the moving part of our stack, reducing the over all number of
packages we need to understand and keep up to date.

Whilst some benchmarking has been performed - it is not exhaustive. The general results
however show a comparable level of throughput to what we had with the previous gunicorn
setup.

<!-- Links -->
[jmoss]: mailto:[email protected]
[uvicorn]: https://www.uvicorn.org/
[gunicorn]: https://gunicorn.org/
[release]: https://www.uvicorn.org/release-notes/#0300-2024-05-28

<!-- Abbreviations -->
*[ASGI]: Asynchronous Server Gateway Interface
3 changes: 0 additions & 3 deletions template/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ RUN apt-get update && apt-get install -y \

COPY --from=builder /venv /venv

# Copy Gunicorn configs
COPY --chown=runtime:runtime src/gunicorn.conf.py /gunicorn.conf.py

# _activate_ the virtual environment
ENV VIRTUAL_ENV=/venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
Expand Down
90 changes: 62 additions & 28 deletions template/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions template/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ django = "^5.1"
django-environ = "^0.11.2"
psycopg = "^3.1.12"
python = "^3.12"
uvicorn = "^0.24.0"
uvicorn = "^0.34.0"
uvloop = { version = "^0.19.0", markers = "sys_platform != 'win32'" }
gunicorn = "^22.0.0"
sentry-sdk = "^2.15.0"
httptools = "^0.6.4"

[tool.poetry.group.dev.dependencies]
bandit = "^1.7.4"
Expand Down
20 changes: 18 additions & 2 deletions template/src/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
#! /bin/bash
set -e

UVICORN_LOG_LEVEL="${UVICORN_LOG_LEVEL:-info}"
UVICORN_WORKERS="${UVICORN_WORKERS:-4}"

serve () {
exec uvicorn \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question the gunicorn config was in a config file, should this stay inline or be in a separate config file?

Copy link
Contributor Author

@a-musing-moose a-musing-moose Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, uvicorn does not support passing config as a file. Which is why I opted to switch back to inline config like this.

--workers="$UVICORN_WORKERS" \
--lifespan=off \
--host=0.0.0.0 \
--port=8000 \
--ws=none \
--no-use-colors \
--access-log \
--log-level="$UVICORN_LOG_LEVEL" \
{{ project_name }}.main.asgi:application
}

if [ -z "$1" ] # nothing specified so we bootstrap the service itself
then
manage migrate --noinput
exec gunicorn -c gunicorn.conf.py {{ project_name }}.main.asgi:application
serve
elif [ "$1" = "run" ] # run the service only
then
exec gunicorn -c gunicorn.conf.py {{ project_name }}.main.asgi:application
serve
elif [ "$1" = "migrate" ] # run database migrations
then
exec manage migrate --noinput "${@:2}"
Expand Down
13 changes: 0 additions & 13 deletions template/src/gunicorn.conf.py

This file was deleted.

12 changes: 9 additions & 3 deletions template/src/project_name/main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,23 @@
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"standard": {
"format": "{levelname:<9s} {name} {asctime} {message}",
"style": "{",
},
},
"handlers": {
"console": {
"level": LOG_LEVEL,
"class": "logging.StreamHandler",
"formatter": "standard",
},
},
"loggers": {
"django": {
"{{ project_name }}": {
"handlers": ["console"],
"level": LOG_LEVEL,
"propagate": True,
"propagate": False,
},
},
}
Expand Down
Loading