fix: integrate VLM into worker poll loop for auto-resume

- Move VLM trigger from TKG spawn to worker poll loop
- Each poll checks if VLM is incomplete and key_frames are ready
- If VLM was interrupted (server restart), next poll will restart it
- Removed duplicate VLM spawn from TKG block
This commit is contained in:
Accusys
2026-08-24 21:34:31 +08:00
parent b2378a219b
commit 2bbbf1950d
+63 -55
View File
@@ -1988,6 +1988,67 @@ impl JobWorker {
let mut created_deferred = false;
// Check VLM summary progress — if incomplete, trigger/restart
let tp_table = schema::table_name("trace_profiles");
let vlm_total: i64 = sqlx::query_scalar(&format!(
"SELECT COUNT(*) FROM {tp_table} WHERE file_uuid = $1"
))
.bind(uuid)
.fetch_one(self.db.pool())
.await
.unwrap_or(0);
let vlm_done: i64 = sqlx::query_scalar(&format!(
"SELECT COUNT(*) FROM {tp_table} WHERE file_uuid = $1 AND vlm_description IS NOT NULL AND vlm_description != ''"
))
.bind(uuid)
.fetch_one(self.db.pool())
.await
.unwrap_or(0);
if vlm_total > 0 && vlm_done < vlm_total {
// VLM incomplete — check if we should trigger it
let face_cluster_done = job_processors.iter().any(|p| p == "face_cluster")
&& results.iter().any(|r| {
r.processor_type.as_str() == "face_cluster"
&& matches!(r.status, ProcessorJobStatus::Completed)
});
if face_cluster_done {
// Check if key_frame.jpg files are ready
let output_dir = crate::core::config::OUTPUT_DIR.as_str();
let trace_dir = std::path::PathBuf::from(output_dir).join(uuid);
let key_frame_count = if trace_dir.exists() {
std::fs::read_dir(&trace_dir)
.ok()
.map(|entries| {
entries.filter_map(|e| e.ok()).filter(|e| {
e.path().join("key_frame.jpg").exists()
}).count() as i64
})
.unwrap_or(0)
} else {
0
};
if key_frame_count >= vlm_total {
info!("[VLM-poll] {}/{} done, {}/{} key_frames ready — triggering VLM", vlm_done, vlm_total, key_frame_count, vlm_total);
let db_clone = self.db.clone();
let redis_clone = self.redis.clone();
let uuid_clone = uuid.to_string();
let output_dir_clone = crate::core::config::OUTPUT_DIR.clone();
tokio::spawn(async move {
crate::core::processor::vlm_summary::generate_vlm_summaries(
&db_clone, &uuid_clone, &output_dir_clone, Some(redis_clone),
).await;
});
} else {
info!("[VLM-poll] Waiting for key_frame.jpg: {}/{} ready", key_frame_count, vlm_total);
}
}
}
let mut created_deferred = false;
// Find processors in job_processors that are not in results yet
for processor_name in job_processors {
let processor_type = match crate::core::db::ProcessorType::from_db_str(processor_name) {
@@ -2677,7 +2738,8 @@ impl JobWorker {
}
}
// 🚀 P4 Trigger: TKG Build (Face + ASRX) → then Rule2 ingestion
// 🚀 P4 Trigger: TKG Build (Face + ASRX) → then Rule2 ingestion
// VLM is now handled in poll loop (auto-resume on interrupt)
if has_face && has_asrx {
// Check face.json status first to determine TKG type
let face_json_path = format!(
@@ -2912,60 +2974,6 @@ impl JobWorker {
)
.await;
// P5: VLM Summary — wait for ALL key_frame.jpg files to be ready
let face_count = r.face_track_nodes;
if face_count > 0 {
let output_dir_wait = output_dir_clone.clone();
let uuid_wait = uuid_clone.clone();
let db_wait = db_clone.clone();
let redis_wait = redis_clone.clone();
tokio::spawn(async move {
// Poll until ALL key_frame.jpg files exist (max 10 min)
let max_attempts = 600; // 10 min / 1s
let mut ready_count = 0i64;
for attempt in 0..max_attempts {
// Count traces with key_frame.jpg on disk
let output_dir = output_dir_wait.as_str();
let trace_dir = std::path::PathBuf::from(output_dir)
.join(&uuid_wait);
ready_count = if trace_dir.exists() {
std::fs::read_dir(&trace_dir)
.ok()
.map(|entries| {
entries.filter_map(|e| e.ok()).filter(|e| {
e.path().join("key_frame.jpg").exists()
}).count() as i64
})
.unwrap_or(0)
} else {
0
};
if ready_count >= face_count as i64 && ready_count > 0 {
info!("[VLM-wait] All {}/{} key_frame.jpg ready for {}", ready_count, face_count, uuid_wait);
break;
}
if attempt % 30 == 0 {
info!("[VLM-wait] {}/{} key_frame.jpg ready for {} (waiting...)", ready_count, face_count, uuid_wait);
}
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
if ready_count >= face_count as i64 {
info!("[VLM] Starting for {}: {} traces verified", uuid_wait, ready_count);
let db_p5 = db_wait.clone();
let redis_p5 = redis_wait.clone();
let uuid_p5 = uuid_wait.clone();
let output_dir_p5 = output_dir_wait.clone();
crate::core::processor::vlm_summary::generate_vlm_summaries(
&db_p5, &uuid_p5, &output_dir_p5, Some(redis_p5),
).await;
} else {
warn!("[VLM] Skipping for {} (only {}/{} key_frame.jpg ready)", uuid_wait, ready_count, face_count);
}
});
}
// Trigger Rule 2 ingestion after TKG complete
if total_edges > 0 {
// Mark rule2_ingestion as running