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)
}
pub async fn claim_next(&self) -> AiResult<Option<ai_analysis_queue::Model>> {
let sql = r#"
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
"#;
pub async fn claim_next(&self, tenant_id: Option<Uuid>) -> AiResult<Option<ai_analysis_queue::Model>> {
let sql = match tenant_id {
Some(tid) => format!(
"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",
tid
),
None => r#"
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(
Statement::from_string(sea_orm::DatabaseBackend::Postgres, sql.to_string()),