fix(monitor): 卡死的一轮不再堵住调度循环;矩阵补总获赞和首条视频日期
「抓不到最新的」有两个叠加的原因: 1. 采集卡死时 `_run_due_task` 是内联 await,execute_task 里的 wait_for 又在等 一个不响应取消的协程 —— 整个调度循环(连同自动追加、每日推送、其他任务) 一起停 65 分钟。现在采集丢到后台任务里跑,循环照常 tick,reap_stale_runs 也能按时回收。实测任务 25 最近 8 次运行有 4 次这样。 2. 超级编导自动追加在 10-25 因密码错误被停到 11-24(防锁号),新作品进不来。 已把 25 号的调度钟拨回现在,补进了 10-31 那条。 标签矩阵:博主名字左边在粉丝数之后加「主页总获赞」和「第一条视频发布日期」 (查全库最早的作品时间,不受 7/14/30 天窗口限制)。
This commit is contained in:
@@ -144,6 +144,9 @@ class MonitorScheduler:
|
||||
self._warned_no_cookie: set = set()
|
||||
# 超级编导自动追加是网络操作,单独一个后台任务,不占调度循环。
|
||||
self._superdir_task: Optional[asyncio.Task] = None
|
||||
# 正在跑的采集(task_id -> asyncio.Task)。**必须留强引用**,否则任务可能被
|
||||
# GC 回收;顺带让测试和停机可以等它们结束。
|
||||
self._running_tasks: dict[int, asyncio.Task] = {}
|
||||
|
||||
async def start(self) -> None:
|
||||
if self._loop_task is not None and not self._loop_task.done():
|
||||
@@ -469,11 +472,47 @@ class MonitorScheduler:
|
||||
|
||||
task_id = task.id
|
||||
|
||||
try:
|
||||
await execute_task(task_id, trigger="scheduled")
|
||||
except Exception as exc:
|
||||
self._start_execution(task_id)
|
||||
|
||||
def _start_execution(self, task_id: int) -> asyncio.Task:
|
||||
"""把采集丢到后台跑,**不让它堵住调度循环**。
|
||||
|
||||
``execute_task`` 里的超时用的是 ``asyncio.wait_for``,它到点会取消采集协程、
|
||||
然后**等取消完成**。抖音那条路偶尔卡在不响应取消的浏览器调用上 —— 内联
|
||||
``await`` 会把整个循环一起钉死 65 分钟:其他任务不触发、自动追加不跑、每日
|
||||
推送也错过。实测任务 25 的 8 次运行里有 4 次就是这样。
|
||||
|
||||
放到后台之后,``reap_stale_runs`` 仍会把卡死的 run 标记回收,循环照常 tick。
|
||||
"""
|
||||
running = self._running_tasks.get(task_id)
|
||||
if running is not None and not running.done():
|
||||
# 同一个任务已经有采集在跑(比如手动触发的还没结束),不要再起一个。
|
||||
return running
|
||||
|
||||
task = asyncio.create_task(execute_task(task_id, trigger="scheduled"))
|
||||
self._running_tasks[task_id] = task
|
||||
task.add_done_callback(lambda done: self._execution_done(task_id, done))
|
||||
return task
|
||||
|
||||
def _execution_done(self, task_id: int, task: asyncio.Task) -> None:
|
||||
if self._running_tasks.get(task_id) is task:
|
||||
self._running_tasks.pop(task_id, None)
|
||||
if task.cancelled():
|
||||
return
|
||||
exc = task.exception()
|
||||
if exc is not None:
|
||||
print(f"[monitor.scheduler] task {task_id} failed: {exc}")
|
||||
|
||||
async def drain(self) -> None:
|
||||
"""等后台采集结束。测试与优雅停机用。"""
|
||||
while True:
|
||||
pending = [
|
||||
task for task in self._running_tasks.values() if not task.done()
|
||||
]
|
||||
if not pending:
|
||||
return
|
||||
await asyncio.gather(*pending, return_exceptions=True)
|
||||
|
||||
|
||||
# Global singleton, mirroring the crawler_manager pattern.
|
||||
monitor_scheduler = MonitorScheduler()
|
||||
|
||||
Reference in New Issue
Block a user