Skip to content

Commit

Permalink
misc: Add support for WEB_CONCURRENCY environment variable
Browse files Browse the repository at this point in the history
The `WEB_CONCURRENCY` environment variable is a more common way to
configure the number of workers for Gunicorn [1] or other web servers.

This change maintains `GUNICORN_WORKERS` compatibility, while notifying
users that it is deprecated and should be replaced with
`WEB_CONCURRENCY`.

It would also allow us to replace Gunicorn with another web server in
the future without changing the variable name.

[1] https://docs.gunicorn.org/en/stable/settings.html#workers
  • Loading branch information
adamantike committed Feb 19, 2025
1 parent d24f981 commit 37db031
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 0 additions & 1 deletion backend/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def str_to_bool(value: str) -> bool:
DEV_MODE: Final = str_to_bool(os.environ.get("DEV_MODE", "false"))
DEV_HOST: Final = os.environ.get("DEV_HOST", "127.0.0.1")
DEV_PORT: Final = int(os.environ.get("DEV_PORT", "5000"))
GUNICORN_WORKERS: Final = int(os.environ.get("GUNICORN_WORKERS", 2))

# PATHS
ROMM_BASE_PATH: Final = os.environ.get("ROMM_BASE_PATH", "/romm")
Expand Down
15 changes: 12 additions & 3 deletions docker/init_scripts/init
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ debug_log() {
fi
}

# print debug log output if enabled
info_log() {
echo "INFO: [init][$(date +"%Y-%m-%d %T")]" "${@}" || true
}

# print error log output if enabled
warn_log() {
echo "WARNING: [init][$(date +"%Y-%m-%d %T")]" "${@}" || true
}

error_log() {
echo "ERROR: [init][$(date +"%Y-%m-%d %T")]" "${@}" || true
exit 1
Expand All @@ -45,6 +47,13 @@ start_bin_gunicorn() {

# commands to start our main application and store its PID to check for crashes
info_log "starting gunicorn"

# TODO: Remove support for GUNICORN_WORKERS in future version.
if [[ -n ${GUNICORN_WORKERS-} ]]; then
warn_log "GUNICORN_WORKERS variable is deprecated, use WEB_CONCURRENCY instead"
: ${WEB_CONCURRENCY:=${GUNICORN_WORKERS}}

Check notice on line 54 in docker/init_scripts/init

View check run for this annotation

Trunk.io / Trunk Check

shellcheck(SC2223)

[new] This default assignment may cause DoS due to globbing. Quote it.

Check notice on line 54 in docker/init_scripts/init

View workflow job for this annotation

GitHub Actions / Trunk Check

shellcheck(SC2223)

[new] This default assignment may cause DoS due to globbing. Quote it.
fi

gunicorn \
--access-logfile - \
--error-logfile - \
Expand All @@ -53,7 +62,7 @@ start_bin_gunicorn() {
--bind=unix:/tmp/gunicorn.sock \
--pid=/tmp/gunicorn.pid \
--forwarded-allow-ips="*" \
--workers "${GUNICORN_WORKERS:=2}" \
--workers "${WEB_CONCURRENCY:-2}" \
main:app &
}

Expand Down
2 changes: 1 addition & 1 deletion env.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ KIOSK_MODE=false

# Gunicorn (optional)
# Workers -> (2 × CPU cores) + 1
GUNICORN_WORKERS=4
WEB_CONCURRENCY=4

# IGDB credentials
IGDB_CLIENT_ID=
Expand Down

0 comments on commit 37db031

Please sign in to comment.