fix(ai): AI 分析队列 claim_next 添加租户隔离

This commit is contained in:
iven
2026-05-05 23:43:11 +08:00
parent 741aaf0e40
commit 087e23e57b

View File

@@ -88,15 +88,21 @@ impl AnalysisQueue {
Ok(id) Ok(id)
} }
pub async fn claim_next(&self) -> AiResult<Option<ai_analysis_queue::Model>> { pub async fn claim_next(&self, tenant_id: Option<Uuid>) -> AiResult<Option<ai_analysis_queue::Model>> {
let sql = r#" let sql = match tenant_id {
SELECT * FROM ai_analysis_queue Some(tid) => format!(
WHERE status = 'pending' "SELECT * FROM ai_analysis_queue WHERE tenant_id = '{}' AND status = 'pending' AND deleted_at IS NULL AND scheduled_at <= NOW() ORDER BY priority DESC, scheduled_at ASC LIMIT 1",
AND deleted_at IS NULL tid
AND scheduled_at <= NOW() ),
ORDER BY priority DESC, scheduled_at ASC None => r#"
LIMIT 1 SELECT * FROM ai_analysis_queue
"#; WHERE status = 'pending'
AND deleted_at IS NULL
AND scheduled_at <= NOW()
ORDER BY priority DESC, scheduled_at ASC
LIMIT 1
"#.to_string(),
};
let row: Option<QueueRow> = QueueRow::find_by_statement( let row: Option<QueueRow> = QueueRow::find_by_statement(
Statement::from_string(sea_orm::DatabaseBackend::Postgres, sql.to_string()), Statement::from_string(sea_orm::DatabaseBackend::Postgres, sql.to_string()),