feat(monitor): 超级编导自动追加,任务设置里按间隔补新作品
Deploy VitePress site to Pages / build (push) Canceled after 0s
Deploy VitePress site to Pages / Deploy (push) Canceled after 0s

任务编辑里新增「自动从超级编导追加新作品」(抖音 + 作品模式才有):

- 每 N 分钟(默认 2 小时)按**发布时间**查一次数据中心,把这段时间新发布、
  还没在任务里的作品追加进去;已有的不会重复。
- start_time/end_time 实测按发布时间过滤,所以老活动里今天才发的作品也能
  收到 —— 按活动创建时间筛会整批漏掉。
- 查询窗口首尾重叠 1 小时,兜住数据中心的索引延迟;失败时 last_at 不动,
  恢复后这段时间的作品仍在窗口里,只把调度钟按 30 分钟退避重试。
- 同步跑在后台任务里,不阻塞采集调度。

Co-Authored-By: Claude <[email protected]>
This commit is contained in:
2026-10-25 07:58:05 +08:00
co-authored by Claude
parent 809692a957
commit be22b30a83
8 changed files with 470 additions and 4 deletions
+69 -2
View File
@@ -44,14 +44,15 @@ import random
from datetime import datetime
from typing import Optional
from sqlalchemy import select
from sqlalchemy import or_, select
from tools.time_util import get_current_timestamp
from ..services import crawler_manager
from . import app_settings, schedule, upstream
from . import adapters, app_settings, schedule, superdir, upstream
from .db import get_session
from .models import (
MODE_NOTE,
RUN_INTERRUPTED,
RUN_PENDING,
RUN_RUNNING,
@@ -141,6 +142,8 @@ class MonitorScheduler:
# Avoids logging "no cookie" on every single tick. Per platform, because
# warning once for Xiaohongshu must not silence the warning for Douyin.
self._warned_no_cookie: set = set()
# 超级编导自动追加是网络操作,单独一个后台任务,不占调度循环。
self._superdir_task: Optional[asyncio.Task] = None
async def start(self) -> None:
if self._loop_task is not None and not self._loop_task.done():
@@ -186,8 +189,72 @@ class MonitorScheduler:
await self._maybe_send_digest()
except Exception as exc: # pragma: no cover - keep the loop alive
print(f"[monitor.scheduler] daily digest failed: {exc}")
# 超级编导自动追加。和采集、推送都无关,单独一段 try。
try:
await self._maybe_sync_superdir()
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_sync_superdir(self) -> None:
"""到点的任务补一批超级编导新发布的作品。
**放到后台任务里跑**:它要发第三方接口的请求(慢的时候一次好几秒),
堵在调度循环里会推迟其他任务的采集。上一次还没跑完就跳过这一 tick ——
不需要排队,下一次自然还会来。
"""
if self._superdir_task is not None and not self._superdir_task.done():
return
self._superdir_task = asyncio.create_task(self._sync_superdir())
async def _sync_superdir(self) -> None:
try:
now = get_current_timestamp()
async with get_session() as session:
due = list(
await session.scalars(
select(MonitorTask.id).where(
MonitorTask.platform == adapters.PLATFORM_DY,
MonitorTask.mode == MODE_NOTE,
MonitorTask.superdir_auto_enabled.is_(True),
or_(
MonitorTask.superdir_auto_next_at.is_(None),
MonitorTask.superdir_auto_next_at <= now,
),
)
)
)
except Exception as exc: # pragma: no cover - keep the loop alive
print(f"[monitor.scheduler] 读取超级编导自动追加任务失败: {exc}")
return
for task_id in due:
await self._sync_one_superdir(task_id)
async def _sync_one_superdir(self, task_id: int) -> None:
async with get_session() as session:
task = await session.get(MonitorTask, task_id)
if task is None or not task.superdir_auto_enabled:
return
try:
result = await superdir.sync_task(session, task)
except Exception as exc:
# 失败**不推** last_at:接口挂了一小时,恢复后这一小时的作品还在
# 查询窗口里。只把调度钟往后挪一段较短的时间再试。
task.superdir_auto_last_error = str(exc)[:500]
task.superdir_auto_next_at = (
get_current_timestamp() + superdir.SYNC_FAILURE_RETRY_MS
)
print(
f"[monitor.scheduler] 超级编导自动追加失败 task={task_id}: {exc}"
)
return
if result["added"]:
print(
f"[monitor.scheduler] 超级编导自动追加 task={task_id} "
f"新增 {result['added']} 条(共 {result['total']})"
)
async def _maybe_check_upstream(self) -> None:
"""到点就 fetch 一次上游仓库,看它有没有新提交。