allow gunicorn worker to be disabled (#4774)

* allow gunicorn worker to be disabled

* allow gunicorn worker to be disabled

* rewrite the command

---------

Co-authored-by: Khaleel Al-Adhami <khaleel.aladhami@gmail.com>
This commit is contained in:
Simon Young 2025-02-07 17:20:35 -08:00 committed by GitHub
parent ee731a908d
commit 3de04156e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 17 deletions

View File

@ -679,7 +679,7 @@ class Config(Base):
# Number of gunicorn workers from user # Number of gunicorn workers from user
gunicorn_workers: Optional[int] = None gunicorn_workers: Optional[int] = None
# Number of requests before a worker is restarted # Number of requests before a worker is restarted; set to 0 to disable
gunicorn_max_requests: int = 100 gunicorn_max_requests: int = 100
# Variance limit for max requests; gunicorn only # Variance limit for max requests; gunicorn only

View File

@ -368,34 +368,49 @@ def run_uvicorn_backend_prod(host: str, port: int, loglevel: LogLevel):
app_module = get_app_module() app_module = get_app_module()
run_backend_prod = f"gunicorn --worker-class {config.gunicorn_worker_class} --max-requests {config.gunicorn_max_requests} --max-requests-jitter {config.gunicorn_max_requests_jitter} --preload --timeout {config.timeout} --log-level critical".split()
run_backend_prod_windows = f"uvicorn --limit-max-requests {config.gunicorn_max_requests} --timeout-keep-alive {config.timeout}".split()
command = ( command = (
[ [
*run_backend_prod_windows, "uvicorn",
"--host", *(
host, [
"--port", "--limit-max-requests",
str(port), str(config.gunicorn_max_requests),
]
if config.gunicorn_max_requests > 0
else []
),
*("--timeout-keep-alive", str(config.timeout)),
*("--host", host),
*("--port", str(port)),
*("--workers", str(_get_backend_workers())),
app_module, app_module,
] ]
if constants.IS_WINDOWS if constants.IS_WINDOWS
else [ else [
*run_backend_prod, "gunicorn",
"--bind", *("--worker-class", config.gunicorn_worker_class),
f"{host}:{port}", *(
"--threads", [
str(_get_backend_workers()), "--max-requests",
str(config.gunicorn_max_requests),
"--max-requests-jitter",
str(config.gunicorn_max_requests_jitter),
]
if config.gunicorn_max_requests > 0
else []
),
"--preload",
*("--timeout", str(config.timeout)),
*("--bind", f"{host}:{port}"),
*("--threads", str(_get_backend_workers())),
f"{app_module}()", f"{app_module}()",
] ]
) )
command += [ command += [
"--log-level", *("--log-level", loglevel.value),
loglevel.value,
"--workers",
str(_get_backend_workers()),
] ]
processes.new_process( processes.new_process(
command, command,
run=True, run=True,