美团券 ETL:prune 防误删护栏 + 每小时定时部署 + meituan_coupon 表文档

- prune 仅在本轮有入库(total>0)时执行,修上游故障时按 last_seen 清空全表的隐患
- 新增 deploy/meituan-etl.{service,timer}:systemd timer 每小时全量灌库(服务器用)
- 新增 docs/database/meituan_coupon.md(字段核对/索引评估/清理策略)并登记索引
This commit is contained in:
chenshuobo
2026-06-10 14:44:40 +08:00
parent db9742f132
commit e92a429ee7
5 changed files with 118 additions and 4 deletions
+6 -2
View File
@@ -288,8 +288,10 @@ def run_once(prune_hours: int = 24) -> None:
total += up
print(f" {src['label']:18}{len(items):5} 解析{len(parsed):5} "
f"入库{up:5} (本源去重{dup:3}) {time.time() - ts:4.0f}s")
# 清理长期未再出现的陈旧券(美团 sign 轮换 / 券下架后的残留),默认 24h 宽限
if prune_hours and prune_hours > 0:
# 清理长期未再出现的陈旧券(美团 sign 轮换 / 券下架后的残留),默认 24h 宽限
# 护栏:仅在本轮确有入库(total>0)时才清理。美团整体故障时本轮可能 0 入库
# (脚本不抛异常、只是抓回空),若仍照常 prune,连续故障会按 last_seen 把全表删空。
if prune_hours and prune_hours > 0 and total > 0:
cutoff = now - timedelta(hours=prune_hours)
pruned = db.execute(
delete(MeituanCoupon).where(MeituanCoupon.last_seen < cutoff)
@@ -297,6 +299,8 @@ def run_once(prune_hours: int = 24) -> None:
db.commit()
if pruned:
print(f" 清理陈旧券(>{prune_hours}h 未再出现): {pruned}")
elif prune_hours and prune_hours > 0 and total == 0:
print(" 本轮 0 入库(疑似上游故障),跳过清理以防误删全表")
cnt = db.execute(select(func.count()).select_from(MeituanCoupon)).scalar()
print(f"[{datetime.now():%H:%M:%S}] 本轮完成: 入库 {total} 条, 表总计 {cnt} 行, "
f"用时 {time.time() - t0:.0f}s")