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_PORT: 5432
POSTGRES_DB: mealie
# WORKERS_PER_CORE: 0.5
# MAX_WORKERS: 8
WEB_CONCURRENCY: 2
postgres:
container_name: postgres

View file

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