use class for conf (#607)

Co-authored-by: hay-kot <hay-kot@pm.me>
This commit is contained in:
Hayden 2021-07-01 15:58:50 -08:00 committed by GitHub
parent 7d3983d2ec
commit 8a2ed0bd78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 46 deletions

View File

@ -18,10 +18,8 @@ services:
POSTGRES_SERVER: postgres POSTGRES_SERVER: postgres
POSTGRES_PORT: 5432 POSTGRES_PORT: 5432
POSTGRES_DB: mealie POSTGRES_DB: mealie
# WORKERS_PER_CORE: 0.5 # WORKERS_PER_CORE: 0.5
# MAX_WORKERS: 8 # MAX_WORKERS: 8
WEB_CONCURRENCY: 2 WEB_CONCURRENCY: 2
postgres: postgres:
container_name: postgres container_name: postgres

View File

@ -4,49 +4,55 @@ import json
import multiprocessing import multiprocessing
import os import os
workers_per_core_str = os.getenv("WORKERS_PER_CORE", "1")
max_workers_str = os.getenv("MAX_WORKERS")
use_max_workers = None
if max_workers_str:
use_max_workers = int(max_workers_str)
web_concurrency_str = os.getenv("WEB_CONCURRENCY", None)
host = os.getenv("HOST", "127.0.0.1") # 0.0.0.0 class GunicornConfig:
port = os.getenv("PORT", "80") """Configuration to generate the properties for Gunicorn"""
bind_env = os.getenv("BIND", None)
use_loglevel = os.getenv("LOG_LEVEL", "info")
if bind_env:
use_bind = bind_env
else:
use_bind = f"{host}:{port}"
cores = multiprocessing.cpu_count() def __init__(self):
workers_per_core = float(workers_per_core_str)
default_web_concurrency = workers_per_core * cores # Env Variables
if web_concurrency_str: self.host = os.getenv("HOST", "127.0.0.1")
web_concurrency = int(web_concurrency_str) self.port = os.getenv("PORT", "80")
assert web_concurrency > 0 self.log_level: str = os.getenv("LOG_LEVEL", "info")
else: self.bind: str = os.getenv("BIND", None)
web_concurrency = max(int(default_web_concurrency), 2) self.errorlog: str = os.getenv("ERROR_LOG", "-") or None
if use_max_workers: self.accesslog: str = os.getenv("ACCESS_LOG", "-") or None
web_concurrency = min(web_concurrency, use_max_workers) self.graceful_timeout: int = int(os.getenv("GRACEFUL_TIMEOUT", "120"))
accesslog_var = os.getenv("ACCESS_LOG", "-") self.timeout = int(os.getenv("TIMEOUT", "120"))
use_accesslog = accesslog_var or None self.keepalive = int(os.getenv("KEEP_ALIVE", "5"))
errorlog_var = os.getenv("ERROR_LOG", "-") self.workers_per_core = float(os.getenv("WORKERS_PER_CORE", "1"))
use_errorlog = errorlog_var or None self.web_concurrency_str: str = os.getenv("WEB_CONCURRENCY", None)
graceful_timeout_str = os.getenv("GRACEFUL_TIMEOUT", "120") self.max_workers_str: str = os.getenv("MAX_WORKERS", None)
timeout_str = os.getenv("TIMEOUT", "120")
keepalive_str = os.getenv("KEEP_ALIVE", "5") # Computed Variables
self.cores = multiprocessing.cpu_count()
self.default_bind = f"{self.host}:{self.port}"
self.default_web_concorrency = self.workers_per_core * self.cores
self.workers = self.get_workers()
def get_workers(self) -> int:
if self.web_concurrency_str:
web_concurrency = int(self.web_concurrency_str)
assert web_concurrency > 0
else:
web_concurrency = max(int(self.default_web_concorrency), 2)
if self.max_workers_str:
web_concurrency = min(web_concurrency, int(self.max_workers_str))
return web_concurrency
gunicorn_conf = GunicornConfig()
# Gunicorn config variables # Gunicorn config variables
loglevel = use_loglevel loglevel = gunicorn_conf.log_level
workers = web_concurrency workers = gunicorn_conf.workers
bind = use_bind bind = gunicorn_conf.bind or gunicorn_conf.default_bind
errorlog = use_errorlog errorlog = gunicorn_conf.errorlog
accesslog = use_accesslog accesslog = gunicorn_conf.accesslog
graceful_timeout = int(graceful_timeout_str) graceful_timeout = gunicorn_conf.graceful_timeout
timeout = int(timeout_str) timeout = gunicorn_conf.timeout
keepalive = int(keepalive_str) keepalive = gunicorn_conf.keepalive
# For debugging and testing # For debugging and testing
@ -60,9 +66,10 @@ log_data = {
"errorlog": errorlog, "errorlog": errorlog,
"accesslog": accesslog, "accesslog": accesslog,
# Additional, non-gunicorn variables # Additional, non-gunicorn variables
"workers_per_core": workers_per_core, "workers_per_core": gunicorn_conf.workers_per_core,
"use_max_workers": use_max_workers, "use_max_workers": gunicorn_conf.max_workers_str,
"host": host, "host": gunicorn_conf.host,
"port": port, "port": gunicorn_conf.port,
} }
print(json.dumps(log_data))
print("---- Gunicorn Configuration ----", json.dumps(log_data, indent=4))