fix: 確保每個 file_uuid 只有一個 monitor_job
Core Test / lint-and-test (push) Has been cancelled
Cross-Project Integration / integration (push) Has been cancelled

檢查結果:目前 DB 無重複 job,但 uuid 索引非 UNIQUE,且兩個建立路徑
(create_monitor_job 的 check-then-insert、REPAIR 的 INSERT WHERE NOT EXISTS)
都有 race,並發時可能產生重複 job。

三層防護:
- DB: CREATE UNIQUE INDEX uq_monitor_jobs_uuid ON monitor_jobs(uuid)
- create_monitor_job INSERT 改 ON CONFLICT (uuid) DO UPDATE
- REPAIR INSERT 加 ON CONFLICT (uuid) DO NOTHING

驗證:並發 5 個 process 請求 → 仍只有 1 個 job
This commit is contained in:
Accusys
2026-11-19 16:49:13 +08:00
parent 9b1618f85b
commit d0dae475f9
2 changed files with 6 additions and 1 deletions
+4
View File
@@ -2395,6 +2395,10 @@ impl PostgresDb {
r#"
INSERT INTO {} (uuid, video_path, status, video_id, processors, face_settings)
VALUES ($1, $2, 'pending', $3, {}::text[], $4::jsonb)
ON CONFLICT (uuid) DO UPDATE SET
status = 'pending', video_path = EXCLUDED.video_path,
video_id = EXCLUDED.video_id, processors = EXCLUDED.processors,
face_settings = EXCLUDED.face_settings, updated_at = NOW()
RETURNING id, uuid, video_path, status, current_processor, progress_total, progress_current, error_count, last_error, started_at::TEXT, updated_at::TEXT, created_at::TEXT, processors, completed_processors, failed_processors, video_id, face_settings::text
"#,
jobs_table, processors_array
+2 -1
View File
@@ -346,7 +346,8 @@ impl JobWorker {
FROM {v_t} v
WHERE v.status = 'pending'
AND v.file_type IN ('video', 'mp4', 'mov', 'avi', 'mkv')
AND NOT EXISTS (SELECT 1 FROM {mj_t} mj WHERE mj.uuid = v.file_uuid)",
AND NOT EXISTS (SELECT 1 FROM {mj_t} mj WHERE mj.uuid = v.file_uuid)
ON CONFLICT (uuid) DO NOTHING",
mj_t = monitor_jobs_table,
v_t = videos_table
))