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
+144
View File
@@ -420,3 +420,147 @@ class TestCreateTask:
async def test_append_to_a_missing_task_is_rejected(self, session):
with pytest.raises(ValueError):
await superdir.append_task(session, 99999, [AWEME_A])
class TestAutoAppend:
"""每隔 N 分钟自动把新发布的作品补进同一个任务。"""
async def _task(self, session):
task, _ = await superdir.create_task(session, [AWEME_A], "自动追加")
return task
@pytest.fixture(autouse=True)
def _token(self, monkeypatch):
async def fake_token(session, platform=superdir.adapters.PLATFORM_DY):
return "token"
monkeypatch.setattr(superdir, "get_token", fake_token)
@pytest.mark.asyncio
async def test_sync_appends_only_new_public_videos(self, session, monkeypatch):
task = await self._task(session)
task.superdir_auto_interval_minutes = 120
async def fake_fetch(token, client, start_ts, end_ts):
return [
_video(AWEME_A), # 已经在任务里
_video(AWEME_B), # 新的
_video(AWEME_C, video_status=2), # 私密,不收
]
monkeypatch.setattr(superdir, "_fetch_published", fake_fetch)
result = await superdir.sync_task(session, task)
assert result == {"fetched": 2, "added": 1, "total": 2}
assert task.superdir_auto_last_added == 1
assert task.superdir_auto_last_error == ""
assert task.superdir_auto_last_at is not None
assert task.superdir_auto_next_at is not None
targets = list(
await session.scalars(
select(MonitorTarget).where(MonitorTarget.task_id == task.id)
)
)
assert sorted(t.external_id for t in targets) == sorted([AWEME_A, AWEME_B])
@pytest.mark.asyncio
async def test_the_same_window_twice_adds_nothing_the_second_time(
self, session, monkeypatch
):
task = await self._task(session)
async def fake_fetch(token, client, start_ts, end_ts):
return [_video(AWEME_A), _video(AWEME_B)]
monkeypatch.setattr(superdir, "_fetch_published", fake_fetch)
await superdir.sync_task(session, task)
# 窗口重叠一小时是故意的(接口有索引延迟),重复的靠去重消化。
result = await superdir.sync_task(session, task)
assert result["added"] == 0
assert result["total"] == 2
assert task.superdir_auto_last_added == 0
assert task.superdir_auto_last_error == ""
@pytest.mark.asyncio
async def test_nothing_new_still_advances_the_clock(self, session, monkeypatch):
task = await self._task(session)
async def fake_fetch(token, client, start_ts, end_ts):
return []
monkeypatch.setattr(superdir, "_fetch_published", fake_fetch)
result = await superdir.sync_task(session, task)
assert result["added"] == 0
assert result["total"] == 1
assert task.superdir_auto_last_error == ""
assert task.superdir_auto_last_at is not None
# 下一次在「这次成功」之后一个间隔,而不是更早。
assert (
task.superdir_auto_next_at
== task.superdir_auto_last_at + 120 * 60_000
)
@pytest.mark.asyncio
async def test_a_failure_does_not_advance_the_watermark(self, session, monkeypatch):
"""接口挂了的时候 last_at 必须停住,否则恢复后那段时间的作品就漏了。"""
task = await self._task(session)
async def boom(token, client, start_ts, end_ts):
raise superdir.SuperDirError(500, "接口挂了")
monkeypatch.setattr(superdir, "_fetch_published", boom)
with pytest.raises(superdir.SuperDirError):
await superdir.sync_task(session, task)
assert task.superdir_auto_last_at is None
assert task.superdir_auto_last_added is None
class TestAutoAppendScheduling:
@pytest.mark.asyncio
async def test_enabling_schedules_the_first_check_immediately(self, session):
task, _ = await superdir.create_task(session, [AWEME_A], "自动")
await superdir.service.update_task(
session, task.id, {"superdir_auto_enabled": True}
)
assert task.superdir_auto_enabled is True
assert task.superdir_auto_next_at is not None
@pytest.mark.asyncio
async def test_disabling_clears_the_clock(self, session):
task, _ = await superdir.create_task(session, [AWEME_A], "自动")
await superdir.service.update_task(
session, task.id, {"superdir_auto_enabled": True}
)
await superdir.service.update_task(
session, task.id, {"superdir_auto_enabled": False}
)
assert task.superdir_auto_enabled is False
assert task.superdir_auto_next_at is None
@pytest.mark.asyncio
async def test_a_creator_task_cannot_enable_it(self, session):
task = await superdir.service.create_task(
session,
{
"name": "博主任务",
"platform": "dy",
"mode": "creator",
"targets": ["MS4wLjABAAAAsomebody"],
},
)
with pytest.raises(ValueError):
await superdir.service.update_task(
session, task.id, {"superdir_auto_enabled": True}
)