預熱活動看板 worker 快取
All checks were successful
CD Pipeline / deploy (push) Successful in 1m2s

This commit is contained in:
OoO
2026-05-19 13:34:06 +08:00
parent 48dd9c5094
commit 1205ce87fb
2 changed files with 30 additions and 1 deletions

View File

@@ -102,8 +102,10 @@ def post_worker_init(worker):
try: try:
from routes.dashboard_routes import warm_full_dashboard_cache from routes.dashboard_routes import warm_full_dashboard_cache
warm_full_dashboard_cache(reason=f"gunicorn-worker-{worker.pid}") warm_full_dashboard_cache(reason=f"gunicorn-worker-{worker.pid}")
from routes.edm_routes import warm_promo_dashboard_cache
warm_promo_dashboard_cache(reason=f"gunicorn-worker-{worker.pid}")
except Exception as exc: except Exception as exc:
worker.log.warning("Dashboard cache prewarm failed in worker %s: %s", worker.pid, exc) worker.log.warning("Dashboard/promo cache prewarm failed in worker %s: %s", worker.pid, exc)
thread = threading.Thread( thread = threading.Thread(
target=_warm_dashboard_cache, target=_warm_dashboard_cache,

View File

@@ -432,6 +432,33 @@ def _build_promo_dashboard_data(session, page_type, page_name, sort_by, order, r
return data return data
def warm_promo_dashboard_cache(reason='manual'):
"""預熱常用活動看板 shared cache避免部署後第一個使用者承擔冷 worker。"""
db = DatabaseManager()
session = db.get_session()
page_configs = [
('edm', '限時搶購'),
('festival', '1.1狂歡購物節'),
('mothers_day', '母親節超值限時購'),
('valentine_520', '520情人節限定購物'),
('labor_day', '勞動節購物優惠'),
]
started = datetime.now(TAIPEI_TZ)
warmed = 0
try:
for page_type, page_name in page_configs:
data = _build_promo_dashboard_data(session, page_type, page_name, 'default', 'desc', None)
if data:
warmed += 1
elapsed_ms = (datetime.now(TAIPEI_TZ) - started).total_seconds() * 1000
sys_log.info(
f"[EDM] [Cache] ✅ 活動看板預熱完成 | reason={reason} | pages={warmed} | 耗時={elapsed_ms:.0f}ms"
)
return warmed
finally:
session.close()
# ========================================== # ==========================================
# EDM 儀表板路由 # EDM 儀表板路由
# ========================================== # ==========================================