預熱每個首頁 worker 快取
All checks were successful
CD Pipeline / deploy (push) Successful in 1m3s

This commit is contained in:
OoO
2026-05-19 13:27:32 +08:00
parent 1d3da03eee
commit f288ef7031

View File

@@ -7,7 +7,6 @@ restart workers but keep the preloaded app object from the old master process.
import os
import sys
import fcntl
import threading
from sqlalchemy.engine import Engine
@@ -94,23 +93,15 @@ def post_fork(server, worker):
def post_worker_init(worker):
"""Warm the expensive dashboard cache once per container start."""
"""Load the shared dashboard cache in every worker before user traffic."""
enabled = os.getenv("DASHBOARD_PREWARM_ON_WORKER_INIT", "1").lower()
if enabled in {"0", "false", "no"}:
return
def _warm_dashboard_cache():
lock_path = os.getenv("DASHBOARD_PREWARM_LOCK_PATH", "/tmp/momo-dashboard-prewarm.lock")
try:
with open(lock_path, "w", encoding="utf-8") as lock_file:
try:
fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError:
worker.log.info("Dashboard cache prewarm already running; worker %s skips", worker.pid)
return
from routes.dashboard_routes import warm_full_dashboard_cache
warm_full_dashboard_cache(reason=f"gunicorn-worker-{worker.pid}")
from routes.dashboard_routes import warm_full_dashboard_cache
warm_full_dashboard_cache(reason=f"gunicorn-worker-{worker.pid}")
except Exception as exc:
worker.log.warning("Dashboard cache prewarm failed in worker %s: %s", worker.pid, exc)