docs: A-8 design - configuration-driven processor list
- Analyze three approaches: database table, config file, environment variable
- Recommend: Phase 1 (config file) → Phase 2 (database table)
- Provide implementation plan and acceptance criteria
References:
- A-8 in Momentry_Release_Requirements_20261004.md v1.1
- Quick fix (remove pose): commit d7df19e2
This commit is contained in:
@@ -0,0 +1,228 @@
|
||||
# A-8 设计:配置驱动的 Processor 清单
|
||||
|
||||
**Date**: 2026-10-04
|
||||
**Status**: 设计中
|
||||
**Priority**: P0(阻塞)
|
||||
|
||||
---
|
||||
|
||||
## 背景
|
||||
|
||||
`sync_file_status` 目前使用硬编码的 processor 清单来判断文件状态。这导致:
|
||||
1. 取消 processor 需要修改代码并重新部署
|
||||
2. 新增 processor 需要修改代码并重新部署
|
||||
3. 不同环境可能需要不同的 processor 集合
|
||||
|
||||
---
|
||||
|
||||
## 当前实现(已修复)
|
||||
|
||||
```rust
|
||||
// files.rs:1805-1813
|
||||
let processors = [
|
||||
("asr", "asr.json"),
|
||||
("asrx", "asrx.json"),
|
||||
("ocr", "ocr.json"),
|
||||
("cut", "cut.json"),
|
||||
("face", "face.json"),
|
||||
("face_cluster", "face_cluster.json"),
|
||||
];
|
||||
```
|
||||
|
||||
**问题**:`pose` 已取消但仍列在清单中 → status 永久 `scanning`
|
||||
|
||||
---
|
||||
|
||||
## 设计方案
|
||||
|
||||
### 方案 A:数据库配置表
|
||||
|
||||
**优点**:
|
||||
- 可通过 API 动态调整
|
||||
- 支持多租户(不同用户不同配置)
|
||||
- 有审计记录
|
||||
|
||||
**缺点**:
|
||||
- 需要额外的表结构
|
||||
- 需要迁移脚本
|
||||
- 启动时依赖数据库连接
|
||||
|
||||
**表结构**:
|
||||
```sql
|
||||
CREATE TABLE processor_config (
|
||||
id SERIAL PRIMARY KEY,
|
||||
processor_name VARCHAR(50) NOT NULL UNIQUE,
|
||||
json_file VARCHAR(100) NOT NULL,
|
||||
is_required BOOLEAN DEFAULT true,
|
||||
is_enabled BOOLEAN DEFAULT true,
|
||||
created_at TIMESTAMP DEFAULT NOW(),
|
||||
updated_at TIMESTAMP DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- 初始数据
|
||||
INSERT INTO processor_config (processor_name, json_file, is_required, is_enabled) VALUES
|
||||
('asr', 'asr.json', true, true),
|
||||
('asrx', 'asrx.json', true, true),
|
||||
('ocr', 'ocr.json', true, true),
|
||||
('cut', 'cut.json', true, true),
|
||||
('face', 'face.json', true, true),
|
||||
('face_cluster', 'face_cluster.json', true, true),
|
||||
('pose', 'pose.json', false, false); -- 已取消
|
||||
```
|
||||
|
||||
**实现**:
|
||||
```rust
|
||||
async fn get_required_processors(db: &PostgresDb) -> Result<Vec<(String, String)>> {
|
||||
sqlx::query_as("SELECT processor_name, json_file FROM processor_config WHERE is_required = true AND is_enabled = true")
|
||||
.fetch_all(db.pool())
|
||||
.await
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 方案 B:配置文件
|
||||
|
||||
**优点**:
|
||||
- 简单,无需数据库
|
||||
- 启动时不依赖数据库
|
||||
- 易于版本控制
|
||||
|
||||
**缺点**:
|
||||
- 修改需要重启服务
|
||||
- 不支持动态调整
|
||||
- 多实例部署需要同步配置
|
||||
|
||||
**配置文件** (`config/processors.toml`):
|
||||
```toml
|
||||
[[processors]]
|
||||
name = "asr"
|
||||
json_file = "asr.json"
|
||||
required = true
|
||||
enabled = true
|
||||
|
||||
[[processors]]
|
||||
name = "asrx"
|
||||
json_file = "asrx.json"
|
||||
required = true
|
||||
enabled = true
|
||||
|
||||
[[processors]]
|
||||
name = "pose"
|
||||
json_file = "pose.json"
|
||||
required = false
|
||||
enabled = false
|
||||
```
|
||||
|
||||
**实现**:
|
||||
```rust
|
||||
#[derive(Deserialize)]
|
||||
struct ProcessorConfig {
|
||||
name: String,
|
||||
json_file: String,
|
||||
required: bool,
|
||||
#[serde(default)]
|
||||
enabled: bool,
|
||||
}
|
||||
|
||||
fn load_processor_config() -> Result<Vec<(String, String)>> {
|
||||
let config: Vec<ProcessorConfig> = toml::from_str(&std::fs::read_to_string("config/processors.toml")?)?;
|
||||
Ok(config.into_iter()
|
||||
.filter(|p| p.required && p.enabled)
|
||||
.map(|p| (p.name, p.json_file))
|
||||
.collect())
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 方案 C:环境变量 + 默认值
|
||||
|
||||
**优点**:
|
||||
- 最简单
|
||||
- 无需额外文件或表
|
||||
- 容器化友好
|
||||
|
||||
**缺点**:
|
||||
- 环境变量可能很长
|
||||
- 不够结构化
|
||||
- 难以表达 required/enabled 状态
|
||||
|
||||
**实现**:
|
||||
```rust
|
||||
fn get_required_processors() -> Vec<(String, String)> {
|
||||
let default = vec![
|
||||
("asr", "asr.json"),
|
||||
("asrx", "asrx.json"),
|
||||
("ocr", "ocr.json"),
|
||||
("cut", "cut.json"),
|
||||
("face", "face.json"),
|
||||
("face_cluster", "face_cluster.json"),
|
||||
];
|
||||
|
||||
std::env::var("MOMENTRY_REQUIRED_PROCESSORS")
|
||||
.map(|s| {
|
||||
s.split(',')
|
||||
.filter_map(|p| {
|
||||
let parts: Vec<&str> = p.split(':').collect();
|
||||
if parts.len() == 2 {
|
||||
Some((parts[0].to_string(), parts[1].to_string()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or(default)
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 推荐方案
|
||||
|
||||
**阶段 1(立即)**:方案 B(配置文件)
|
||||
- 快速实现
|
||||
- 无需数据库迁移
|
||||
- 满足当前需求
|
||||
|
||||
**阶段 2(未来)**:方案 A(数据库配置表)
|
||||
- 当需要动态调整时迁移
|
||||
- 支持多租户场景
|
||||
- 有审计记录
|
||||
|
||||
---
|
||||
|
||||
## 实现计划
|
||||
|
||||
### Phase 1:配置文件(本周)
|
||||
|
||||
1. 创建 `config/processors.toml`
|
||||
2. 实现 `load_processor_config()` 函数
|
||||
3. 修改 `sync_file_status` 使用配置文件
|
||||
4. 新增测试:已取消 processor 不阻挡 completed
|
||||
5. 更新文档
|
||||
|
||||
### Phase 2:数据库配置表(待评估)
|
||||
|
||||
1. 设计表结构
|
||||
2. 编写迁移脚本
|
||||
3. 实现 API endpoint
|
||||
4. 迁移现有配置
|
||||
|
||||
---
|
||||
|
||||
## 验收标准
|
||||
|
||||
- ✅ 移除 pose(已完成 commit `d7df19e2`)
|
||||
- ⏳ processor 清单可配置
|
||||
- ⏳ 取消 processor 不阻挡 completed
|
||||
- ⏳ 新增测试案例
|
||||
|
||||
---
|
||||
|
||||
## 相关文档
|
||||
|
||||
- 需求:`Momentry_Release_Requirements_20261004.md` A-8
|
||||
- 诊断:附录 C
|
||||
- 实现:`src/api/files.rs:1805`
|
||||
Reference in New Issue
Block a user