Files
DashBoard/gunicorn.conf.py
beabigegg fcfa4fb919 feat: Phase 2 連線池化與優雅關閉改進
- NullPool 改為 QueuePool 連線池
  - pool_size=5, max_overflow=10, pool_recycle=1800
  - pool_pre_ping 確保連線有效性
- 新增連線池監控事件 (checkout/checkin/invalidate/connect)
- 新增 keep-alive 機制 (每 5 分鐘 ping)
- 新增 dispose_engine() 清理函數
- Gunicorn worker_exit hook 自動清理連線
- 降低 graceful_timeout 至 10 秒加速重啟
- 降低 tcp_connect_timeout 至 10 秒減少等待

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-27 18:34:31 +08:00

25 lines
908 B
Python

import os
bind = os.getenv("GUNICORN_BIND", "0.0.0.0:8080")
workers = int(os.getenv("GUNICORN_WORKERS", "2")) # 2 workers for redundancy
threads = int(os.getenv("GUNICORN_THREADS", "4"))
worker_class = "gthread"
# Timeout settings - critical for dashboard stability
timeout = 60 # Worker timeout: 60 seconds max per request
graceful_timeout = 10 # Graceful shutdown timeout (reduced for faster restart)
keepalive = 5 # Keep-alive connections timeout
# ============================================================
# Worker Lifecycle Hooks
# ============================================================
def worker_exit(server, worker):
"""Clean up database connections when worker exits."""
try:
from mes_dashboard.core.database import dispose_engine
dispose_engine()
except Exception as e:
server.log.warning(f"Error disposing database engine: {e}")