fix(notify): harden ledger/flush after second review
- flush() no longer re-enters the 60s PATCH after a mid-loop failure (once it failed at batch>=10 every remaining record retried it, which could stall the tick until HEALTHCHECK killed it); it retries exactly once at the end. - silenced rows (ignored seller / keyword / watch notify=off) are no longer written to the ledger, so a failed PATCH re-runs the same silent branch next tick instead of healing to notified without skip_notify. Ledger now means 'actually delivered to Telegram' and nothing else. - success path order: ledger (memory) -> batch -> snapshot, and the finally block isolates save_ledger from flush so an ENOSPC cannot skip the DB write. - _tg_call accepts str bodies (Content-Length is bytes; str would have been latin-1 encoded and misread as ASSUME_SENT).
This commit is contained in:
1 parent
98d38953a4
commit
869b75d48e
1 file changed
+22
-13
+22
-13
@@ -279,6 +279,8 @@ def _tg_call(method, body, content_type, read_timeout=None):
|
||||
method 是 Telegram 的方法名(sendMessage / sendPhoto),不是 HTTP 动词。
|
||||
返回 (state, detail)。
|
||||
"""
|
||||
if isinstance(body, str):
|
||||
body = body.encode("utf-8") # Content-Length 必须是字节数;http.client 对 str 走 latin-1
|
||||
path = f"/bot{TELEGRAM_BOT_TOKEN}/{method}"
|
||||
read_timeout = read_timeout or TG_READ_TIMEOUT
|
||||
for attempt in range(3):
|
||||
@@ -934,8 +936,11 @@ def send_pending_notifications(listings_tid, settings_tid, ignored_sellers_tid,
|
||||
|
||||
batch = []
|
||||
|
||||
def flush():
|
||||
if not batch:
|
||||
def flush(force=False):
|
||||
"""写回标记。失败后本轮不再自动重试 —— 否则第 10 条失败后,之后每一条都会
|
||||
再触发一次 60s 超时的 PATCH,一轮几十次能把容器拖到被 HEALTHCHECK 杀掉。
|
||||
轮末用 force=True 再赌一次(只此一次)。"""
|
||||
if not batch or (not stats["patch_ok"] and not force):
|
||||
return
|
||||
if patch_listings(listings_tid, batch):
|
||||
del batch[:]
|
||||
@@ -961,18 +966,14 @@ def send_pending_notifications(listings_tid, settings_tid, ignored_sellers_tid,
|
||||
search_url = (rec.get("search_url") or "").strip()
|
||||
kw_for_watch = ignored_kw_by_url.get(search_url, set())
|
||||
if seller in ignored or title_matches_keyword(title, kw_for_watch):
|
||||
now_ts = time.time()
|
||||
for k in keys:
|
||||
ledger[k] = now_ts
|
||||
# 不入账本:账本只记录「真的发到 Telegram 的」。批写失败时下一轮
|
||||
# 会重新走这条分支(依旧不发),顺带把 skip_notify 一起补上。
|
||||
batch.append({"Id": rid, "skip_notify": True, "notified": True})
|
||||
stats["silenced"] += 1
|
||||
continue
|
||||
|
||||
# 3) 该 watch 关了 notify:同样静默标记
|
||||
if not notify_by_title.get(rec.get("search_title") or "", True):
|
||||
now_ts = time.time()
|
||||
for k in keys:
|
||||
ledger[k] = now_ts
|
||||
batch.append({"Id": rid, "notified": True})
|
||||
stats["silenced"] += 1
|
||||
continue
|
||||
@@ -983,20 +984,28 @@ def send_pending_notifications(listings_tid, settings_tid, ignored_sellers_tid,
|
||||
continue
|
||||
|
||||
if send_listing_from_record(rec):
|
||||
# 顺序:先记账本(内存)→ 再排入 batch → 最后落快照。这样快照失败
|
||||
# 也不会丢掉「这条已经发出去了」这个事实。
|
||||
now_ts = time.time()
|
||||
for k in keys:
|
||||
ledger[k] = now_ts
|
||||
# 先落一份无 fsync 快照(毫秒级),再谈 NocoDB 写回
|
||||
save_ledger(ledger, protected)
|
||||
batch.append({"Id": rid, "notified": True})
|
||||
stats["sent"] += 1
|
||||
try:
|
||||
save_ledger(ledger, protected) # 无 fsync 快照,毫秒级
|
||||
except Exception as e:
|
||||
sys.stderr.write(f"ledger snapshot failed: {e}\n")
|
||||
if len(batch) >= 10:
|
||||
flush()
|
||||
time.sleep(SEND_INTERVAL_SECONDS)
|
||||
finally:
|
||||
# 轮末:账本带 fsync + .bak 落盘,剩余标记一次批量 PATCH
|
||||
save_ledger(ledger, protected, durable=True)
|
||||
flush()
|
||||
# 轮末:账本带 fsync + .bak 落盘,剩余标记一次批量 PATCH。
|
||||
# 两者互相隔离:账本写失败也绝不能跳过 DB 写回。
|
||||
try:
|
||||
save_ledger(ledger, protected, durable=True)
|
||||
except Exception as e:
|
||||
sys.stderr.write(f"ledger durable save failed: {e}\n")
|
||||
flush(force=True)
|
||||
|
||||
return stats
|
||||
|
||||
|
||||
Reference in new issue
Block a user