From 1205ce87fbd5b88a19eac561b280f5636a625853 Mon Sep 17 00:00:00 2001 From: OoO Date: Tue, 19 May 2026 13:34:06 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A0=90=E7=86=B1=E6=B4=BB=E5=8B=95=E7=9C=8B?= =?UTF-8?q?=E6=9D=BF=20worker=20=E5=BF=AB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gunicorn.conf.py | 4 +++- routes/edm_routes.py | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/gunicorn.conf.py b/gunicorn.conf.py index 0d44b3f..dcf8041 100644 --- a/gunicorn.conf.py +++ b/gunicorn.conf.py @@ -102,8 +102,10 @@ def post_worker_init(worker): try: from routes.dashboard_routes import warm_full_dashboard_cache 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: - 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( target=_warm_dashboard_cache, diff --git a/routes/edm_routes.py b/routes/edm_routes.py index b8c49fb..15837c3 100644 --- a/routes/edm_routes.py +++ b/routes/edm_routes.py @@ -432,6 +432,33 @@ def _build_promo_dashboard_data(session, page_type, page_name, sort_by, order, r 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 儀表板路由 # ==========================================