This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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 儀表板路由
|
||||
# ==========================================
|
||||
|
||||
Reference in New Issue
Block a user