feat: implement Re-scan API (partial)
實現內容: 1. 新增 ReScanRequest 和 ReScanResponse 結構體 2. 實現 re_scan API 函數 3. 添加路由 POST /api/v1/re-scan 4. 修改 process_register_command 添加 auto_pipeline_override 支持 測試結果: - ✅ Re-scan API 可用 - ✅ Command 創建成功 - ✅ Force unregister 正確執行 - ❌ Monitor Job 未自動創建(需要進一步修復) 已知問題: - register_single_file 在 force unregister 後仍返回 already_exists=true - 導致 auto_pipeline 條件判斷失敗 下一步: - 修改 register_single_file 修復 already_exists 標志 - 或在 process_register_command 中添加 force_unregister_executed 標志
This commit is contained in:
@@ -1053,6 +1053,29 @@ struct UnregisterRequest {
|
||||
reason: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct ReScanRequest {
|
||||
file_uuid: String,
|
||||
#[serde(default)]
|
||||
processors: Option<Vec<String>>,
|
||||
#[serde(default)]
|
||||
face_settings: Option<FaceSettings>,
|
||||
#[serde(default)]
|
||||
trigger_source: Option<String>,
|
||||
#[serde(default)]
|
||||
reason: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct ReScanResponse {
|
||||
success: bool,
|
||||
message: String,
|
||||
file_uuid: String,
|
||||
unregister_command_id: Option<String>,
|
||||
register_command_id: Option<String>,
|
||||
process_command_id: Option<String>,
|
||||
}
|
||||
|
||||
fn delete_output_files(uuid: &str) -> u64 {
|
||||
let mut deleted_count = 0u64;
|
||||
let output_dirs = [
|
||||
@@ -1174,6 +1197,94 @@ async fn unregister(
|
||||
}))
|
||||
}
|
||||
|
||||
async fn re_scan(
|
||||
State(state): State<AppState>,
|
||||
Json(req): Json<ReScanRequest>,
|
||||
) -> Result<Json<ReScanResponse>, StatusCode> {
|
||||
let file_uuid = req.file_uuid.clone();
|
||||
let source = req.trigger_source.as_deref().unwrap_or("api_re_scan");
|
||||
let reason = req.reason.as_deref().unwrap_or("re_scan");
|
||||
|
||||
tracing::info!(
|
||||
"[RE-SCAN] Starting re-scan for {} (source={}, reason={})",
|
||||
file_uuid,
|
||||
source,
|
||||
reason
|
||||
);
|
||||
|
||||
// Step 1: Get file path from database
|
||||
let videos_table = schema::table_name("videos");
|
||||
let file_path: Option<String> = sqlx::query_scalar(&format!(
|
||||
"SELECT file_path FROM {} WHERE file_uuid = $1",
|
||||
videos_table
|
||||
))
|
||||
.bind(&file_uuid)
|
||||
.fetch_optional(state.db.pool())
|
||||
.await
|
||||
.map_err(|e| {
|
||||
tracing::error!("[RE-SCAN] Failed to query file path for {}: {:?}", file_uuid, e);
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
})?;
|
||||
|
||||
let file_path = match file_path {
|
||||
Some(p) => p,
|
||||
None => {
|
||||
tracing::error!("[RE-SCAN] File not found: {}", file_uuid);
|
||||
return Err(StatusCode::NOT_FOUND);
|
||||
}
|
||||
};
|
||||
|
||||
// Step 2: Create register command with force=true
|
||||
// Note: force=true will automatically unregister existing file if needed
|
||||
let register_payload = serde_json::json!({
|
||||
"force": true,
|
||||
"auto_pipeline": true, // Flag to force auto-pipeline
|
||||
"content_hash": null,
|
||||
"face_settings": req.face_settings.as_ref().map(|fs| {
|
||||
serde_json::json!({
|
||||
"min_confidence": fs.min_confidence,
|
||||
"min_size": fs.min_size
|
||||
})
|
||||
}),
|
||||
});
|
||||
|
||||
let register_cmd = state
|
||||
.db
|
||||
.create_command("register", Some(&file_path), None, None, ®ister_payload, 1)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
tracing::error!(
|
||||
"[RE-SCAN] Failed to enqueue register command for {}: {:?}",
|
||||
file_path,
|
||||
e
|
||||
);
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
})?;
|
||||
|
||||
tracing::info!(
|
||||
"[RE-SCAN] Register command {} enqueued for {} (with force=true)",
|
||||
register_cmd.command_id,
|
||||
file_path
|
||||
);
|
||||
|
||||
// Note: Register command with force=true will:
|
||||
// 1. Unregister existing file if needed
|
||||
// 2. Register the file
|
||||
// 3. Trigger auto-pipeline if auto_pipeline flag is set
|
||||
|
||||
Ok(Json(ReScanResponse {
|
||||
success: true,
|
||||
message: format!(
|
||||
"Re-scan queued: register={}",
|
||||
register_cmd.command_id
|
||||
),
|
||||
file_uuid,
|
||||
unregister_command_id: None,
|
||||
register_command_id: Some(register_cmd.command_id),
|
||||
process_command_id: None,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Internal unregister function - can be called from both API and register
|
||||
pub(crate) async fn unregister_internal(db: &PostgresDb, uuid: &str) -> Result<(), StatusCode> {
|
||||
let videos_table = schema::table_name("videos");
|
||||
@@ -1487,6 +1598,7 @@ pub fn file_routes() -> Router<AppState> {
|
||||
.route("/api/v1/files/register", post(register_file))
|
||||
.route("/api/v1/files/lookup", get(lookup_file_by_name))
|
||||
.route("/api/v1/unregister", post(unregister))
|
||||
.route("/api/v1/re-scan", post(re_scan))
|
||||
.route("/api/v1/command/:command_id", get(get_command_status))
|
||||
.route("/api/v1/file/:file_uuid/probe", get(probe_by_uuid))
|
||||
.route(
|
||||
|
||||
@@ -530,11 +530,16 @@ impl JobWorker {
|
||||
.get("force")
|
||||
.and_then(|v| v.as_bool())
|
||||
.unwrap_or(false);
|
||||
let auto_pipeline_override = cmd
|
||||
.payload
|
||||
.get("auto_pipeline")
|
||||
.and_then(|v| v.as_bool())
|
||||
.unwrap_or(false);
|
||||
let face_settings = cmd.payload.get("face_settings").cloned();
|
||||
|
||||
info!(
|
||||
"[COMMAND_QUEUE] Registering file: {} (user_id={:?}, force={})",
|
||||
file_path, user_id, force
|
||||
"[COMMAND_QUEUE] Registering file: {} (user_id={:?}, force={}, auto_pipeline_override={})",
|
||||
file_path, user_id, force, auto_pipeline_override
|
||||
);
|
||||
|
||||
let resp = register_single_file(&self.db, file_path, user_id, content_hash, force).await;
|
||||
@@ -543,8 +548,11 @@ impl JobWorker {
|
||||
anyhow::bail!("Register failed: {}", resp.message);
|
||||
}
|
||||
|
||||
if resp.success
|
||||
&& !resp.already_exists
|
||||
// Trigger auto-pipeline if:
|
||||
// 1. auto_pipeline_override flag is set, OR
|
||||
// 2. (force || !already_exists) AND file is video AND auto_pipeline is enabled globally
|
||||
if (auto_pipeline_override || (force || !resp.already_exists))
|
||||
&& resp.success
|
||||
&& resp.file_type.as_deref() == Some("video")
|
||||
&& crate::core::config::get_auto_pipeline_enabled()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user