Files
MediaCrawler/api/monitor
butubb 24b7a221f3
Deploy VitePress site to Pages / build (push) Canceled after 0s
Deploy VitePress site to Pages / Deploy (push) Canceled after 0s
fix(monitor): 卡死的 run 会永远停在「运行中」—— 加周期回收,并让 Playwright 卡住时能放手
线上现象:抖音任务 7 有 7 条 run 卡在 running,最久的 48 小时,`notes_fetched=0`、
无退出码、无错误,产物目录全空(说明在开始抓之前就挂住了),界面上就是「好多在运行中」。

根因两段,缺一不可:

1. `douyin_api._read_browser` 的 finally 里是裸的 `await playwright.stop()`,**没有
   超时**。Playwright 的 driver 一卡住(容器里能查到两个跑了两天的 run-driver 进程),
   `stop()` 永不返回 → `_read_browser` 永不返回 → `collect` 永不返回。
2. `runner` 用 `asyncio.wait_for(collect(...), timeout=3600)` 兜底,但 **wait_for 超时
   后会先取消、再等取消完成**;协程卡在不响应取消的调用上时,它自己也永远不返回,
   `except asyncio.TimeoutError` 根本轮不到。

于是 run 永远停在 running。而 `recover()` 只在进程启动时跑一次,不重启就没人收。

改动:

* `douyin_api`:`playwright.stop()` 改用新的 `_abandon_after()` —— 它用
  `asyncio.wait`(到点就返回)而不是 `wait_for`(会等取消完成),到点就走;
  `async_playwright().start()` 同样加 20 秒上限,起不来就回退到库里的 cookie。
* `scheduler`:新增 `reap_stale_runs()`,每个 tick(20 秒)扫一遍,把 running 超过
  **该任务自己配的** `run_timeout_seconds` + 300 秒余量的 run 标成 interrupted,
  并写上原因。不再依赖重启。

测试:`TestReapStaleRuns` 四条 —— 超过超时被回收且原因可读、未超时不动、
刚过超时仍在余量内不动、已结束的不碰。
2026-10-18 16:48:28 +08:00
..