feat(worker): 手动查重 worker task + visual_similarity/match_count 字段 #1661 #1679

Merged
auto-approve-bot merged 2 commits from feat/manual-dup-check-worker-1661 into develop 2026-09-04 01:28:36 +08:00
Owner

背景

Issue #1661:手动查重上传后记录一直 pending —— 上传/重试接口只建了 DuplicationRecord,没有任何 worker 跑计算。本 PR 补全完整处理链路。

改动

1. 新增 Celery task worker.process_duplication_check

apps/worker/worker_app/tasks/duplication_check.py

  1. 从 OSS 下载上传视频到临时目录
  2. 复用 VideoDeduplicator.compute_fingerprint()(动态抽帧+分片指纹)
  3. compute_duplicate_rate(scope="user") 跨项目与用户所有已有成片比对 → duplicate_rate/visual_similarity/match_count
  4. find_duplicate_segments() 分片级滑动窗口时序匹配 → 重复片段定位
  5. 更新 DuplicationRecord:pending → processing → completed/failed

2. 状态与重试

  • 失败重试 3 次、间隔 60s;重试耗尽自动标记 failed 并写错误信息
  • 已完成记录幂等跳过
  • 临时文件在 finally 中始终清理

3. 字段补全

  • DuplicationRecord 实体新增 visual_similarity: float | Nonematch_count: int | None(默认 None,向后兼容)
  • mark_completed() 签名同步加两个参数;reset_for_retry() 同步清空
  • migration 065_dup_record_sim_match:duplication_records 表加两列
  • repository/model 读写映射同步
  • DuplicationRecordResponse / DuplicationDetailResponse 新增两字段,路由透传

4. 片段单位映射

worker 分片 DuplicateSegment(ms + 平均汉明距离)→ 领域 DuplicateSegment(秒 + 0-100 相似度百分比),与前端 SegmentCard 消费格式一致。

5. 路由 enqueue

  • 上传接口 POST /duplication/upload 建记录后 send_task("worker.process_duplication_check")
  • 重试接口 POST /records/{id}/retry 重置后重新 enqueue

测试

  • 新增 11 个单测:成功全链路/已完成跳过/记录不存在/首次失败重试不标记failed/重试耗尽标记failed/临时文件清理/片段ms→s与距离→百分比映射/无分片老视频跳过/schema新字段/domain mark_completed/reset_for_retry
  • 相关 361 个测试全过(dedup/duplication 全量)
  • black + ruff 通过;migration 链 063→064→065 单 head

注意

  • 不碰 dedup.py 算法,与在跑的 PR #1674(#1658)无冲突
  • 所有新字段可选,老数据 None 前端自动隐藏
## 背景 Issue #1661:手动查重上传后记录一直 pending —— 上传/重试接口只建了 DuplicationRecord,没有任何 worker 跑计算。本 PR 补全完整处理链路。 ## 改动 ### 1. 新增 Celery task `worker.process_duplication_check` `apps/worker/worker_app/tasks/duplication_check.py`: 1. 从 OSS 下载上传视频到临时目录 2. 复用 `VideoDeduplicator.compute_fingerprint()`(动态抽帧+分片指纹) 3. `compute_duplicate_rate(scope="user")` 跨项目与用户所有已有成片比对 → duplicate_rate/visual_similarity/match_count 4. `find_duplicate_segments()` 分片级滑动窗口时序匹配 → 重复片段定位 5. 更新 DuplicationRecord:pending → processing → completed/failed ### 2. 状态与重试 - 失败重试 3 次、间隔 60s;重试耗尽自动标记 failed 并写错误信息 - 已完成记录幂等跳过 - 临时文件在 finally 中始终清理 ### 3. 字段补全 - `DuplicationRecord` 实体新增 `visual_similarity: float | None`、`match_count: int | None`(默认 None,向后兼容) - `mark_completed()` 签名同步加两个参数;`reset_for_retry()` 同步清空 - migration `065_dup_record_sim_match`:duplication_records 表加两列 - repository/model 读写映射同步 - `DuplicationRecordResponse` / `DuplicationDetailResponse` 新增两字段,路由透传 ### 4. 片段单位映射 worker 分片 `DuplicateSegment`(ms + 平均汉明距离)→ 领域 `DuplicateSegment`(秒 + 0-100 相似度百分比),与前端 SegmentCard 消费格式一致。 ### 5. 路由 enqueue - 上传接口 `POST /duplication/upload` 建记录后 `send_task("worker.process_duplication_check")` - 重试接口 `POST /records/{id}/retry` 重置后重新 enqueue ## 测试 - 新增 11 个单测:成功全链路/已完成跳过/记录不存在/首次失败重试不标记failed/重试耗尽标记failed/临时文件清理/片段ms→s与距离→百分比映射/无分片老视频跳过/schema新字段/domain mark_completed/reset_for_retry - 相关 361 个测试全过(dedup/duplication 全量) - black + ruff 通过;migration 链 063→064→065 单 head ## 注意 - 不碰 dedup.py 算法,与在跑的 PR #1674(#1658)无冲突 - 所有新字段可选,老数据 None 前端自动隐藏
xiaoxia added 1 commit 2026-09-04 01:17:29 +08:00
feat(worker): 手动查重 worker task + visual_similarity/match_count 字段 #1661
CI/CD Pipeline / Check push changed paths (pull_request) Has been skipped
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 2s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 2s
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 31s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 31s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m22s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 1m45s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 1m55s
CI/CD Pipeline / Validate - Style (pull_request) Has been cancelled
CI/CD Pipeline / Validate - Security (pull_request) Has been cancelled
CI/CD Pipeline / Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / Build Production API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been cancelled
CI/CD Pipeline / Canary Release to Production (pull_request) Has been cancelled
CI/CD Pipeline / CI Gate (pull_request) Has been cancelled
AI Code Review / AI Code Review (pull_request) Has been cancelled
PR Automation / Auto Approve on CI Green (pull_request) Has been cancelled
PR Automation / Auto Merge on CI Green + Approved (pull_request) Has been cancelled
a5e56939a5
- 新增 Celery task worker.process_duplication_check:
  OSS 下载上传视频 → 动态抽帧指纹 → 跨项目与用户所有成片比对
  (compute_duplicate_rate + find_duplicate_segments) → 更新 DuplicationRecord
- 状态流转 pending → processing → completed/failed,失败重试 3 次间隔 60s,
  重试耗尽标记 failed,临时文件 finally 清理
- 片段映射:worker 分片 DuplicateSegment(ms/汉明距离) → 领域 DuplicateSegment(秒/0-100 相似度)
- DuplicationRecord 实体新增 visual_similarity/match_count(可选 None,向后兼容),
  mark_completed()/reset_for_retry() 同步
- migration 065: duplication_records 表加两列
- repository/model/schemas/routes 读写映射与响应透传
- 上传接口与重试接口 enqueue task(此前记录一直 pending 的根因)
- 11 个新单测:成功/跳过/未找到/重试/失败标记/临时文件清理/片段映射/schema

🚀 预览环境已部署

项目 详情
PR号 #1679
预览链接 https://pr-1679.preview.xiaoxiajianji.com
API环境 staging

💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。

🔄 每次提交新代码后预览环境会自动更新。

🗑️ PR 关闭或合并后,预览环境会自动清理。

🚀 **预览环境已部署** | 项目 | 详情 | |------|------| | PR号 | #1679 | | 预览链接 | [https://pr-1679.preview.xiaoxiajianji.com](https://pr-1679.preview.xiaoxiajianji.com) | | API环境 | staging | > 💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。 > > 🔄 每次提交新代码后预览环境会自动更新。 > > 🗑️ PR 关闭或合并后,预览环境会自动清理。
auto-approve-bot added 1 commit 2026-09-04 01:20:04 +08:00
style: auto-format with black + isort + prettier [skip ci-format-check]
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 4s
CI/CD Pipeline / Check push changed paths (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 3s
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 33s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 2m0s
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 2m31s
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 29s
CI/CD Pipeline / Validate - Style (pull_request) Successful in 3m0s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 1m33s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m11s
AI Code Review / AI Code Review (pull_request) Failing after 3m54s
CI/CD Pipeline / Validate - Security (pull_request) Successful in 5m22s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 7m25s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / CI Gate (pull_request) Successful in 1s
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 5m11s
ACR Cleanup / ACR Image Cleanup (pull_request_target) Successful in 5s
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 46s
77267ac101
Collaborator

【阻塞级判定】

  • 是否存在阻塞级问题:是
  • 阻塞级问题数量:1 个

📊 审查概览

  • 整体评价:需修改
  • 建议级问题数量:2 个

🔴 阻塞级问题(必须修复)

  1. [apps/worker/worker_app/tasks/duplication_check.py: 188] 异常处理逻辑导致无限重试
    • 问题类型:逻辑bug
    • 问题描述:在 process_duplication_check 函数的 except Exception 块中,raise self.retry(...) 语句位于 if self.request.retries >= self.max_retries 判断块之外。这意味着即使任务已经达到最大重试次数(max_retries=3)并将记录标记为 "failed",代码仍然会执行最后的 raise self.retry,导致 Celery 继续重试该任务,造成无限循环或超出预期的重试行为。
    • 修改建议:将 raise self.retry(...) 移入 else 分支,或者仅在未达到最大重试次数时执行重试。当 retries >= max_retries 时,应直接返回或抛出非 Retry 异常以结束任务。

💡 改进建议(不阻塞合并)

  1. [apps/worker/worker_app/tasks/duplication_check.py: 54] 访问受保护成员

    • 具体内容:代码中调用了 deduplicator._get_existing_chunks(...),访问了类的 protected/private 成员(以下划线开头)。这违反了封装原则,如果库内部实现变更,会导致代码运行时错误。建议确认是否有公开 API 可用,或与库维护者沟通。
  2. [apps/worker/worker_app/tasks/duplication_check.py: 66] 魔法数字硬编码

    • 具体内容:计算相似度时使用了 avg_sim = 1.0 - raw.avg_distance / 64.0。这里的 64.0 是一个魔法数字,可能代表距离的最大值或归一化因子。建议将其定义为常量(例如 MAX_DISTANCE = 64.0)或从配置/类属性中获取,以提高代码可读性和可维护性。

良好实践

  • 在 API 层使用 getattr 兼容新旧字段,确保在数据库迁移未执行时旧代码仍能运行。
  • Worker 任务中正确处理了临时文件(tempfile.mkdtemp)的清理,使用了 finally 块确保资源释放。
  • 测试用例覆盖了成功、重试、清理临时文件等场景,覆盖度较好。

格式检查通过 | 逻辑审查需修改 | ⚠️ 建议关注性能


🤖 由 AI 代码审查机器人自动生成 | 2026-09-03 17:24:06 | 模型:

### 【阻塞级判定】 - 是否存在阻塞级问题:是 - 阻塞级问题数量:1 个 ### 📊 审查概览 - 整体评价:需修改 - 建议级问题数量:2 个 ### 🔴 阻塞级问题(必须修复) 1. **[apps/worker/worker_app/tasks/duplication_check.py: 188] 异常处理逻辑导致无限重试** - 问题类型:逻辑bug - 问题描述:在 `process_duplication_check` 函数的 `except Exception` 块中,`raise self.retry(...)` 语句位于 `if self.request.retries >= self.max_retries` 判断块之外。这意味着即使任务已经达到最大重试次数(max_retries=3)并将记录标记为 "failed",代码仍然会执行最后的 `raise self.retry`,导致 Celery 继续重试该任务,造成无限循环或超出预期的重试行为。 - 修改建议:将 `raise self.retry(...)` 移入 `else` 分支,或者仅在未达到最大重试次数时执行重试。当 `retries >= max_retries` 时,应直接返回或抛出非 Retry 异常以结束任务。 ### 💡 改进建议(不阻塞合并) 1. **[apps/worker/worker_app/tasks/duplication_check.py: 54] 访问受保护成员** - 具体内容:代码中调用了 `deduplicator._get_existing_chunks(...)`,访问了类的 protected/private 成员(以下划线开头)。这违反了封装原则,如果库内部实现变更,会导致代码运行时错误。建议确认是否有公开 API 可用,或与库维护者沟通。 2. **[apps/worker/worker_app/tasks/duplication_check.py: 66] 魔法数字硬编码** - 具体内容:计算相似度时使用了 `avg_sim = 1.0 - raw.avg_distance / 64.0`。这里的 `64.0` 是一个魔法数字,可能代表距离的最大值或归一化因子。建议将其定义为常量(例如 `MAX_DISTANCE = 64.0`)或从配置/类属性中获取,以提高代码可读性和可维护性。 ### ✅ 良好实践 - 在 API 层使用 `getattr` 兼容新旧字段,确保在数据库迁移未执行时旧代码仍能运行。 - Worker 任务中正确处理了临时文件(`tempfile.mkdtemp`)的清理,使用了 `finally` 块确保资源释放。 - 测试用例覆盖了成功、重试、清理临时文件等场景,覆盖度较好。 --- ✅ 格式检查通过 | ❌ 逻辑审查需修改 | ⚠️ 建议关注性能 --- <sub>🤖 由 AI 代码审查机器人自动生成 | 2026-09-03 17:24:06 | 模型: </sub> <!-- AI_CODE_REVIEW_AUTO_COMMENT -->
auto-approve-bot merged commit 2205adb8fb into develop 2026-09-04 01:28:36 +08:00
auto-approve-bot deleted branch feat/manual-dup-check-worker-1661 2026-09-04 01:28:36 +08:00

🗑️ 预览环境已清理

PR #1679 已关闭或合并,对应的预览环境已被清理。

如有需要,可以重新打开 PR 来重新生成预览环境。

🗑️ **预览环境已清理** PR #1679 已关闭或合并,对应的预览环境已被清理。 > 如有需要,可以重新打开 PR 来重新生成预览环境。
Sign in to join this conversation.