pipeline-parallel.md
2.24 KB
Pipeline 并行化 Plan
当前问题
hermes chat (单进程,300s timeout)
├─ 查询微信消息 → 发现 N 篇文章
├─ check-summaries → 发现 M 篇未缓存
├─ 对第 1 篇: curl POST /summarize → 等 60s
├─ 对第 2 篇: curl POST /summarize → 等 60s
├─ ... (串行!)
├─ 对第 M 篇: curl POST /summarize → 等 60s
└─ 编撰日报
瓶颈: M 篇未缓存文章 → 串行等待 M×60s → 超过 300s timeout → 部分文章丢失或超时
改进方案:两阶段流水线
Phase 1: 并行摘要 (Flask 编排)
hermes chat (collector)
└─ 查询消息 → 返回文章列表
↓
Flask: 对每篇未缓存文章并行 POST /summarize
├─ hermes job: 文章 1 摘要 ─┐
├─ hermes job: 文章 2 摘要 ─┤ 并行!
└─ hermes job: 文章 M 摘要 ─┘
↓ 所有完成
Phase 2: 日报编撰 (单个 hermes)
hermes chat (compiler)
└─ 读取所有缓存摘要 → 编撰日报
改进点:
- M 篇文章摘要并行执行,总耗时 ≈ max(单篇耗时) 而非 sum
- Phase 1 collector 不需要处理摘要,只收集 URL → 快速完成
- Phase 2 compiler 只读缓存 → 不需要网络抓取 → 快速完成
- 每阶段独立追踪(任务监控可见)
实现步骤
Step 1: app.py 新增 /api/reports/generate-v2
- 接收 chatroom_id + date
- Phase 1a: spawn collector job → 返回文章列表
- Phase 1b: 并行 spawn summarize jobs
- Phase 1c: 轮询等待全部完成
- Phase 2: spawn compiler job → 生成日报
Step 2: 前端 reports.html 使用新 API
- "生成日报"按钮改为调用 generate-v2
- 显示流水线进度(X/Y 篇文章摘要完成)
Step 3: 任务监控可见
- 每个子任务独立显示在 /jobs
- 可看到 collector / summarizer×N / compiler 的完整链路
涉及文件
- app.py: 新增 /api/reports/generate-v2 + 并行编排逻辑
- wechat-group-digest skill: 拆分为 collector / compiler 两个子 prompt
- templates/reports.html: 进度展示
- test_jobs_integration.py: 新增流水线测试
风险
- 并行摘要受限于 hermes 并发数(同一用户可同时跑多个 hermes chat?)
- Phase 1c 轮询需要非阻塞实现(用 threading + sleep poll)