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:
@@ -338,3 +338,77 @@ class TestRecovery:
|
||||
async with monitor_db.get_session() as session:
|
||||
run = await session.scalar(select(MonitorRun))
|
||||
assert run.status == RUN_SUCCESS
|
||||
|
||||
|
||||
async def _make_run(
|
||||
task_id: int, *, started_minutes_ago: int, status: str = RUN_RUNNING
|
||||
) -> int:
|
||||
now = get_current_timestamp()
|
||||
async with monitor_db.get_session() as session:
|
||||
run = MonitorRun(
|
||||
task_id=task_id,
|
||||
trigger="scheduled",
|
||||
status=status,
|
||||
phase=MODE_CREATOR,
|
||||
save_data_path="",
|
||||
queued_at=now,
|
||||
not_before=0,
|
||||
started_at=now - started_minutes_ago * MS_PER_MINUTE,
|
||||
max_comments_count=50,
|
||||
)
|
||||
session.add(run)
|
||||
await session.flush()
|
||||
return run.id
|
||||
|
||||
|
||||
async def _get_run(run_id: int) -> MonitorRun:
|
||||
async with monitor_db.get_session() as session:
|
||||
return await session.get(MonitorRun, run_id)
|
||||
|
||||
|
||||
class TestReapStaleRuns:
|
||||
"""卡在「运行中」的 run 必须能被周期性地收掉,而不是只能靠重启。
|
||||
|
||||
``runner`` 那边用 ``asyncio.wait_for(collect(...), timeout=...)`` 兜底,但超时后
|
||||
它会去**取消**采集协程、并且**等取消完成**。抖音那条路卡在 Playwright 调用上时
|
||||
协程不响应取消,于是 ``except asyncio.TimeoutError`` 根本轮不到 —— 线上真的因此
|
||||
挂过 7 条幽灵 run,最久的 48 小时,产物目录全空,只有重启才清掉。
|
||||
"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_past_its_own_timeout_is_reaped(self, db):
|
||||
task_id = await _make_task(get_current_timestamp()) # run_timeout_seconds=3600
|
||||
run_id = await _make_run(task_id, started_minutes_ago=120)
|
||||
|
||||
assert await scheduler_module.reap_stale_runs() == 1
|
||||
|
||||
run = await _get_run(run_id)
|
||||
assert run.status == RUN_INTERRUPTED
|
||||
assert run.finished_at is not None
|
||||
# 留下来的必须是**说得清**的原因,不能是空字符串
|
||||
assert "卡死" in (run.error_message or "")
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_still_inside_its_timeout_is_untouched(self, db):
|
||||
task_id = await _make_task(get_current_timestamp())
|
||||
run_id = await _make_run(task_id, started_minutes_ago=30)
|
||||
|
||||
assert await scheduler_module.reap_stale_runs() == 0
|
||||
assert (await _get_run(run_id)).status == RUN_RUNNING
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_grace_period_leaves_the_runner_time_to_finish(self, db):
|
||||
"""刚过超时那一小段不动手 —— 采集那边到点后还要收尾写库。"""
|
||||
task_id = await _make_task(get_current_timestamp())
|
||||
run_id = await _make_run(task_id, started_minutes_ago=61) # 3600s 刚过
|
||||
|
||||
assert await scheduler_module.reap_stale_runs() == 0
|
||||
assert (await _get_run(run_id)).status == RUN_RUNNING
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_finished_runs_are_left_alone(self, db):
|
||||
task_id = await _make_task(get_current_timestamp())
|
||||
run_id = await _make_run(task_id, started_minutes_ago=1000, status=RUN_SUCCESS)
|
||||
|
||||
assert await scheduler_module.reap_stale_runs() == 0
|
||||
assert (await _get_run(run_id)).status == RUN_SUCCESS
|
||||
|
||||
Reference in New Issue
Block a user