This commit is contained in:
parent
52f959fe9e
commit
1e920b29d0
46
gunicorn/gunicorn_conf.py
Normal file
46
gunicorn/gunicorn_conf.py
Normal file
@ -0,0 +1,46 @@
|
||||
# based on https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker
|
||||
|
||||
import multiprocessing
|
||||
import os
|
||||
|
||||
host = os.getenv("HOST", "0.0.0.0")
|
||||
port = os.getenv("PORT", "8012")
|
||||
bind_env = os.getenv("BIND", None)
|
||||
|
||||
use_bind = bind_env if bind_env else f"{host}:{port}"
|
||||
|
||||
workers_per_core_str = os.getenv("WORKERS_PER_CORE", "1")
|
||||
max_workers_str = os.getenv("MAX_WORKERS")
|
||||
web_concurrency_str = os.getenv("WEB_CONCURRENCY", None)
|
||||
|
||||
cores = multiprocessing.cpu_count()
|
||||
workers_per_core = int(workers_per_core_str)
|
||||
default_web_concurrency = workers_per_core * cores + 1
|
||||
|
||||
if web_concurrency_str:
|
||||
web_concurrency = int(web_concurrency_str)
|
||||
assert web_concurrency > 0
|
||||
else:
|
||||
web_concurrency = max(int(default_web_concurrency), 2)
|
||||
if max_workers_str:
|
||||
use_max_workers = int(max_workers_str)
|
||||
web_concurrency = min(web_concurrency, use_max_workers)
|
||||
|
||||
graceful_timeout_str = os.getenv("GRACEFUL_TIMEOUT", "120")
|
||||
timeout_str = os.getenv("TIMEOUT", "120")
|
||||
keepalive_str = os.getenv("KEEP_ALIVE", "5")
|
||||
use_loglevel = os.getenv("LOG_LEVEL", "info")
|
||||
|
||||
# Gunicorn config variables
|
||||
# 错误日志级别
|
||||
errorlog = "-"
|
||||
# 访问日志级别
|
||||
accesslog = "-"
|
||||
loglevel = use_loglevel
|
||||
workers = web_concurrency
|
||||
bind = use_bind
|
||||
worker_tmp_dir = "/dev/shm"
|
||||
graceful_timeout = int(graceful_timeout_str)
|
||||
timeout = int(timeout_str)
|
||||
keepalive = int(keepalive_str)
|
||||
logconfig = os.getenv("LOG_CONFIG", "logging_production.ini")
|
5
scripts/format
Normal file
5
scripts/format
Normal file
@ -0,0 +1,5 @@
|
||||
#!/bin/sh -e
|
||||
set -x
|
||||
|
||||
ruff --fix src
|
||||
ruff format src
|
18
scripts/start-dev.sh
Normal file
18
scripts/start-dev.sh
Normal file
@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -e
|
||||
|
||||
DEFAULT_MODULE_NAME=src.main
|
||||
|
||||
MODULE_NAME=${MODULE_NAME:-$DEFAULT_MODULE_NAME}
|
||||
VARIABLE_NAME=${VARIABLE_NAME:-app}
|
||||
export APP_MODULE=${APP_MODULE:-"$MODULE_NAME:$VARIABLE_NAME"}
|
||||
|
||||
HOST=${HOST:-0.0.0.0}
|
||||
PORT=${PORT:-8000}
|
||||
LOG_LEVEL=${LOG_LEVEL:-info}
|
||||
#LOG_CONFIG=${LOG_CONFIG:-/src/logging.ini}
|
||||
LOG_CONFIG=${LOG_CONFIG:-./logging.ini}
|
||||
|
||||
# Start Uvicorn with live reload
|
||||
exec uvicorn --reload --proxy-headers --host $HOST --port $PORT --log-config $LOG_CONFIG "$APP_MODULE"
|
16
scripts/start-prod.sh
Normal file
16
scripts/start-prod.sh
Normal file
@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -e
|
||||
|
||||
DEFAULT_MODULE_NAME=src.main
|
||||
|
||||
MODULE_NAME=${MODULE_NAME:-$DEFAULT_MODULE_NAME}
|
||||
VARIABLE_NAME=${VARIABLE_NAME:-app}
|
||||
export APP_MODULE=${APP_MODULE:-"$MODULE_NAME:$VARIABLE_NAME"}
|
||||
|
||||
DEFAULT_GUNICORN_CONF=gunicorn/gunicorn_conf.py
|
||||
export GUNICORN_CONF=${GUNICORN_CONF:-$DEFAULT_GUNICORN_CONF}
|
||||
export WORKER_CLASS=${WORKER_CLASS:-"uvicorn.workers.UvicornWorker"}
|
||||
|
||||
# Start Gunicorn
|
||||
gunicorn --forwarded-allow-ips "*" -k "$WORKER_CLASS" -c "$GUNICORN_CONF" "$APP_MODULE"
|
Loading…
Reference in New Issue
Block a user