fix(daily_sales): 啟用 bp 版改進邏輯 + import 後跨 worker 清 cache,根除 #24 隱形 bug
All checks were successful
CD Pipeline / deploy (push) Successful in 1m6s
All checks were successful
CD Pipeline / deploy (push) Successful in 1m6s
- 從 app.py 刪除 396 行的 /daily_sales、/daily_sales/export、/daily_sales/export_marketing 三條 @app.route(行 5911-6306),讓 routes/daily_sales_routes.py 的 daily_sales_bp 生效(first-registered wins,原 app.py 版本 shadow 了 bp)。 - bp 版改進點:_is_cache_valid() 帶 5 分鐘 TTL、/api/daily_sales/clear_cache 端點、 完整模板參數(datetime_now / active_page)。 - services/import_service.py process_daily_sales_import return True 前, 新增跨 gunicorn worker 清 daily_sales cache 邏輯:依 GUNICORN_WORKERS 次數呼叫 internal /api/daily_sales/clear_cache,避免 4 worker 各持 5 分鐘舊快取 導致「匯入 15323 筆但當日業績看不到」隱形 bug。 [P7-COMPLETION] - 方案正確: 雙重佐證(refactor-specialist + web-researcher)確認 Flask first-registered wins,刪 app.py 內 route 即可讓 bp 接管;helper 函式(preprocess_daily_sales_data 等) 為 dead code 但保守保留不影響執行。 - 影響完整: 全 repo grep 確認 _SALES_PROCESSED_CACHE 在 app.py 仍有 30+ 處使用 (sales_analysis 等其他路由),未動到;helper 函式無外部 caller。 - Regression 風險: 低,bp 版簽名與行為相容;新 cache 清除走 internal HTTP 帶 try/except 不影響主流程;若 GUNICORN_WORKERS 未設則默認 4 與生產一致。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -637,6 +637,22 @@ class ImportService:
|
||||
session.close()
|
||||
|
||||
logger.info(f"任務 {job_id} 匯入成功: {total_rows} 筆")
|
||||
|
||||
# 跨 worker 清 daily_sales cache(gunicorn N worker 各持獨立 in-memory cache,
|
||||
# 需打 N 次 internal endpoint 確保每個 worker 的快取都被清掉)
|
||||
try:
|
||||
import requests
|
||||
base = os.getenv('INTERNAL_BASE_URL', 'http://127.0.0.1:5000')
|
||||
n = int(os.getenv('GUNICORN_WORKERS', '4'))
|
||||
for _ in range(n):
|
||||
try:
|
||||
requests.post(f"{base}/api/daily_sales/clear_cache", timeout=2)
|
||||
except Exception:
|
||||
break
|
||||
logger.info(f"任務 {job_id} 已清 daily_sales cache({n} workers)")
|
||||
except Exception as e:
|
||||
logger.warning(f"任務 {job_id} 清 cache 失敗(不影響主流程): {e}")
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user