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` 四条 —— 超过超时被回收且原因可读、未超时不动、 刚过超时仍在余量内不动、已结束的不碰。
This commit is contained in:
@@ -65,6 +65,57 @@ POLL_INTERVAL_SECONDS = 20
|
||||
# Spread tasks sharing an interval so they do not all come due on the same tick.
|
||||
# Applied to interval mode only -- see the advance step below.
|
||||
JITTER_SECONDS = 60
|
||||
# 超时之后再等这么久才动手回收。采集那边到点后还要收尾写库,别和它抢。
|
||||
REAP_GRACE_SECONDS = 300
|
||||
|
||||
|
||||
async def reap_stale_runs() -> int:
|
||||
"""把「早就该结束却还挂在 running」的 run 收掉,返回收了几个。
|
||||
|
||||
**为什么不能只靠 ``asyncio.wait_for`` 的超时。** 它超时后会去**取消**采集协程,
|
||||
而取消要等协程真正结束。抖音那条路会卡在 Playwright 的调用上
|
||||
(``douyin_api._read_browser`` 里 ``playwright.stop()`` 原先没包超时,driver
|
||||
一卡就永不返回),这种协程不响应取消 —— 于是 ``wait_for`` 自己也永远不返回,
|
||||
``except asyncio.TimeoutError`` 根本轮不到,run 就永远停在「运行中」。
|
||||
实测:7 条这样的幽灵,最久的挂了 48 小时,产物目录全空。
|
||||
|
||||
``recover()`` 只在进程启动时跑一次,进程不重启就没人来收。所以这里按 tick 扫。
|
||||
|
||||
判据用**任务自己配的** ``run_timeout_seconds``(默认 3600),不是写死时长 ——
|
||||
每个任务允许跑多久本来就不一样。
|
||||
"""
|
||||
now = get_current_timestamp()
|
||||
reaped = 0
|
||||
async with get_session() as session:
|
||||
rows = (
|
||||
await session.execute(
|
||||
select(MonitorRun, MonitorTask.run_timeout_seconds)
|
||||
.join(MonitorTask, MonitorTask.id == MonitorRun.task_id)
|
||||
.where(MonitorRun.status == RUN_RUNNING)
|
||||
)
|
||||
).all()
|
||||
for run, timeout_seconds in rows:
|
||||
if run.started_at is None:
|
||||
continue
|
||||
limit_ms = (
|
||||
max(1, int(timeout_seconds or 3600)) + REAP_GRACE_SECONDS
|
||||
) * 1000
|
||||
if now - run.started_at < limit_ms:
|
||||
continue
|
||||
run.status = RUN_INTERRUPTED
|
||||
run.finished_at = now
|
||||
run.error_message = (
|
||||
f"超过 {timeout_seconds} 秒仍未结束,已判定为卡死并回收"
|
||||
"(采集协程没有响应取消,通常卡在浏览器调用上)"
|
||||
)
|
||||
reaped += 1
|
||||
|
||||
if reaped:
|
||||
print(
|
||||
f"[monitor.scheduler] 回收了 {reaped} 个卡死的 run(超过各自超时仍未结束)",
|
||||
flush=True,
|
||||
)
|
||||
return reaped
|
||||
|
||||
|
||||
class MonitorScheduler:
|
||||
@@ -100,6 +151,12 @@ class MonitorScheduler:
|
||||
print(f"[monitor.scheduler] recovery failed: {exc}")
|
||||
|
||||
while not self._stopping.is_set():
|
||||
# 先收尸再调度:卡死的 run 不该继续占着界面的「运行中」,也不该让
|
||||
# 它的任务看起来还在跑。见 reap_stale_runs 的说明。
|
||||
try:
|
||||
await reap_stale_runs()
|
||||
except Exception as exc: # pragma: no cover - keep the loop alive
|
||||
print(f"[monitor.scheduler] stale run reap failed: {exc}")
|
||||
try:
|
||||
await self.tick()
|
||||
except Exception as exc: # pragma: no cover - keep the loop alive
|
||||
|
||||
Reference in New Issue
Block a user