feat: 添加 AI 索引重建功能,优化相关 API 和工作流,增强内存管理配置
Some checks failed
docker-images / resolve-build-targets (push) Successful in 6s
ui-regression / playwright-regression (push) Successful in 4m43s
docker-images / build-and-push (admin) (push) Successful in 42s
docker-images / submit-indexnow (push) Has been cancelled
docker-images / build-and-push (frontend) (push) Has been cancelled
docker-images / build-and-push (backend) (push) Has started running
Some checks failed
docker-images / resolve-build-targets (push) Successful in 6s
ui-regression / playwright-regression (push) Successful in 4m43s
docker-images / build-and-push (admin) (push) Successful in 42s
docker-images / submit-indexnow (push) Has been cancelled
docker-images / build-and-push (frontend) (push) Has been cancelled
docker-images / build-and-push (backend) (push) Has started running
This commit is contained in:
55
backend/src/workers/ai_reindex.rs
Normal file
55
backend/src/workers/ai_reindex.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
use loco_rs::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::services::{ai, worker_jobs};
|
||||
|
||||
pub struct AiReindexWorker {
|
||||
pub ctx: AppContext,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||
pub struct AiReindexWorkerArgs {
|
||||
#[serde(default)]
|
||||
pub job_id: Option<i32>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl BackgroundWorker<AiReindexWorkerArgs> for AiReindexWorker {
|
||||
fn build(ctx: &AppContext) -> Self {
|
||||
Self { ctx: ctx.clone() }
|
||||
}
|
||||
|
||||
fn tags() -> Vec<String> {
|
||||
vec!["ai".to_string(), "reindex".to_string()]
|
||||
}
|
||||
|
||||
async fn perform(&self, args: AiReindexWorkerArgs) -> Result<()> {
|
||||
if let Some(job_id) = args.job_id {
|
||||
if !worker_jobs::begin_job_execution(&self.ctx, job_id).await? {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
match ai::rebuild_index(&self.ctx).await {
|
||||
Ok(summary) => {
|
||||
worker_jobs::mark_job_succeeded(
|
||||
&self.ctx,
|
||||
job_id,
|
||||
Some(serde_json::json!({
|
||||
"indexed_chunks": summary.indexed_chunks,
|
||||
"last_indexed_at": summary.last_indexed_at.map(|value| value.to_rfc3339()),
|
||||
})),
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
Err(error) => {
|
||||
worker_jobs::mark_job_failed(&self.ctx, job_id, error.to_string()).await?;
|
||||
Err(error)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ai::rebuild_index(&self.ctx).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user