修改队列处理逻辑,添加自动停止机制

This commit is contained in:
imeepos 2025-07-14 16:33:11 +08:00
parent bb240833a4
commit 29e77c2b6f
2 changed files with 36 additions and 23 deletions

View File

@ -266,7 +266,17 @@ impl VideoClassificationQueue {
sleep(self.processing_delay).await; sleep(self.processing_delay).await;
} }
Ok(None) => { Ok(None) => {
// 没有待处理任务,等待 // 没有待处理任务,检查是否应该自动停止
if let Ok(should_stop) = self.check_should_auto_stop().await {
if should_stop {
println!("🛑 检测到没有任务需要处理,自动停止队列");
let mut status = self.status.write().await;
*status = QueueStatus::Stopped;
break;
}
}
// 继续等待
sleep(Duration::from_secs(5)).await; sleep(Duration::from_secs(5)).await;
} }
Err(e) => { Err(e) => {
@ -286,6 +296,31 @@ impl VideoClassificationQueue {
println!("AI视频分类队列处理循环已退出"); println!("AI视频分类队列处理循环已退出");
} }
/// 检查是否应该自动停止队列
/// 当等待中=0且处理中=0时说明没有任务需要处理
async fn check_should_auto_stop(&self) -> Result<bool> {
// 获取当前统计信息
let stats = self.stats.read().await;
// 检查是否有待处理或正在处理的任务
let has_pending = stats.pending_tasks > 0;
let has_processing = stats.processing_tasks > 0;
let has_current_task = self.current_task.lock().await.is_some();
// 如果没有待处理、没有正在处理、也没有当前任务,则可以停止
let should_stop = !has_pending && !has_processing && !has_current_task;
if should_stop {
println!("📊 队列状态检查:");
println!(" 等待中任务: {}", stats.pending_tasks);
println!(" 处理中任务: {}", stats.processing_tasks);
println!(" 当前任务: {}", if has_current_task { "" } else { "" });
println!(" 结论: 没有任务需要处理,准备自动停止");
}
Ok(should_stop)
}
/// 处理下一个任务 /// 处理下一个任务
async fn process_next_task(&self) -> Result<Option<String>> { async fn process_next_task(&self) -> Result<Option<String>> {
println!("🔍 查找待处理任务..."); println!("🔍 查找待处理任务...");

View File

@ -173,21 +173,6 @@ export const AiAnalysisLogViewer: React.FC<AiAnalysisLogViewerProps> = ({ projec
} }
}; };
// 创建测试数据
const handleCreateTestData = async () => {
try {
const result = await invoke('create_test_ai_analysis_logs', {
projectId,
});
console.log('创建测试数据结果:', result);
// 重新加载数据
loadLogs();
loadStats();
} catch (err) {
console.error('创建测试数据失败:', err);
}
};
// 重试失败任务 // 重试失败任务
const handleRetryTask = async (taskId: string) => { const handleRetryTask = async (taskId: string) => {
try { try {
@ -354,13 +339,6 @@ export const AiAnalysisLogViewer: React.FC<AiAnalysisLogViewerProps> = ({ projec
> >
<Download className="w-4 h-4" /> <Download className="w-4 h-4" />
</button> </button>
<button
onClick={handleCreateTestData}
className="px-4 py-2 bg-purple-600 text-white rounded-lg hover:bg-purple-700 transition-colors text-sm"
title="创建测试数据"
>
</button>
</div> </div>
</div> </div>
</div> </div>