feat(matrix): 「第一条视频」改成主页最早一条,后台翻页扫描 + 缓存
要的是这个博主**最早发的**那条(可能根本不在监控目标里),不是主页第一屏。 按游标翻到底才知道,所以: - 新增 monitor_creator_first_video 缓存表,按 sec_uid 存最早发布时间, 30 天内不再扫; - 调度器后台任务慢慢扫(每页 20 条、页间 0.5 秒、单次最多 50 页 / 3 分钟), 不占调度循环; - 矩阵只读缓存,读不到退回库里最早的一条作品; - 失败只锁 6 小时再试,不会把没扫到的号锁 30 天。 同时撤掉上一版「每轮采集多问一次主页最新视频」的实现(方向反了)。
This commit is contained in:
@@ -49,7 +49,7 @@ from sqlalchemy import or_, select
|
||||
from tools.time_util import get_current_timestamp
|
||||
|
||||
from ..services import crawler_manager
|
||||
from . import adapters, app_settings, schedule, superdir, upstream
|
||||
from . import adapters, app_settings, creator_scan, schedule, superdir, upstream
|
||||
from .db import get_session
|
||||
from .models import (
|
||||
MODE_NOTE,
|
||||
@@ -147,6 +147,8 @@ class MonitorScheduler:
|
||||
# 正在跑的采集(task_id -> asyncio.Task)。**必须留强引用**,否则任务可能被
|
||||
# GC 回收;顺带让测试和停机可以等它们结束。
|
||||
self._running_tasks: dict[int, asyncio.Task] = {}
|
||||
# 扫「最早一条视频」的后台任务。慢、且不急,单飞即可。
|
||||
self._first_video_task: Optional[asyncio.Task] = None
|
||||
|
||||
async def start(self) -> None:
|
||||
if self._loop_task is not None and not self._loop_task.done():
|
||||
@@ -197,8 +199,30 @@ class MonitorScheduler:
|
||||
await self._maybe_sync_superdir()
|
||||
except Exception as exc: # pragma: no cover - keep the loop alive
|
||||
print(f"[monitor.scheduler] 超级编导自动追加失败: {exc}")
|
||||
# 扫博主主页「最早一条视频」。慢且不紧急,后台慢慢来。
|
||||
try:
|
||||
await self._maybe_scan_first_videos()
|
||||
except Exception as exc: # pragma: no cover - keep the loop alive
|
||||
print(f"[monitor.scheduler] 扫描最早视频失败: {exc}")
|
||||
await asyncio.sleep(POLL_INTERVAL_SECONDS)
|
||||
|
||||
async def _maybe_scan_first_videos(self) -> None:
|
||||
if self._first_video_task is not None and not self._first_video_task.done():
|
||||
return
|
||||
self._first_video_task = asyncio.create_task(self._scan_first_videos())
|
||||
|
||||
async def _scan_first_videos(self) -> None:
|
||||
try:
|
||||
result = await creator_scan.refresh_batch(adapters.PLATFORM_DY)
|
||||
except Exception as exc: # pragma: no cover - keep the loop alive
|
||||
print(f"[monitor.scheduler] 扫描最早视频失败: {exc}")
|
||||
return
|
||||
if result.get("scanned") or result.get("failed"):
|
||||
print(
|
||||
f"[monitor.scheduler] 最早视频扫描:"
|
||||
f"到期 {result['due']},成功 {result['scanned']},失败 {result['failed']}"
|
||||
)
|
||||
|
||||
async def _maybe_sync_superdir(self) -> None:
|
||||
"""到点的任务补一批超级编导新发布的作品。
|
||||
|
||||
|
||||
Reference in New Issue
Block a user