Skip to content

Commit

Permalink
chore(job): db job collect errors instead of returning when met error
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Zhang <[email protected]>
  • Loading branch information
zwpaper committed Jan 8, 2025
1 parent 6d46695 commit 8eeeb15
Showing 1 changed file with 42 additions and 15 deletions.
57 changes: 42 additions & 15 deletions ee/tabby-webserver/src/service/background_job/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use tabby_db::DbConn;
use tabby_schema::context::ContextService;
use tabby_schema::{context::ContextService, CoreError};

use super::helper::Job;

Expand All @@ -20,21 +20,48 @@ impl DbMaintainanceJob {
context: Arc<dyn ContextService>,
db: DbConn,
) -> tabby_schema::Result<()> {
db.delete_expired_token().await?;
db.delete_expired_password_resets().await?;
db.delete_expired_ephemeral_threads().await?;
let mut errors = vec![];

Check warning on line 23 in ee/tabby-webserver/src/service/background_job/db.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/db.rs#L23

Added line #L23 was not covered by tests

if let Err(e) = db.delete_expired_token().await {
errors.push(format!("Failed to delete expired token: {}", e));
};
if let Err(e) = db.delete_expired_password_resets().await {
errors.push(format!("Failed to delete expired password resets: {}", e));
};
if let Err(e) = db.delete_expired_ephemeral_threads().await {
errors.push(format!("Failed to delete expired ephemeral threads: {}", e));
};

Check warning on line 33 in ee/tabby-webserver/src/service/background_job/db.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/db.rs#L25-L33

Added lines #L25 - L33 were not covered by tests

// Read all active sources
let active_source_ids = context
.read(None)
.await?
.sources
.into_iter()
.map(|x| x.source_id())
.collect::<Vec<_>>();

db.delete_unused_source_id_read_access_policy(&active_source_ids)
.await?;
Ok(())
match context.read(None).await {
Ok(info) => {
let active_source_ids = info
.sources
.into_iter()
.map(|x| x.source_id())
.collect::<Vec<_>>();
if let Err(e) = db
.delete_unused_source_id_read_access_policy(&active_source_ids)
.await
{
errors.push(format!(
"Failed to delete unused source id read access policy: {}",
e
));
};

Check warning on line 51 in ee/tabby-webserver/src/service/background_job/db.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/db.rs#L36-L51

Added lines #L36 - L51 were not covered by tests
}
Err(e) => {
errors.push(format!("Failed to read active sources: {}", e));
}

Check warning on line 55 in ee/tabby-webserver/src/service/background_job/db.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/db.rs#L53-L55

Added lines #L53 - L55 were not covered by tests
}

if errors.is_empty() {
Ok(())

Check warning on line 59 in ee/tabby-webserver/src/service/background_job/db.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/db.rs#L58-L59

Added lines #L58 - L59 were not covered by tests
} else {
Err(CoreError::Other(anyhow::anyhow!(
"Failed to run db maintenance job:\n{}",
errors.join(";\n")
)))

Check warning on line 64 in ee/tabby-webserver/src/service/background_job/db.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/db.rs#L61-L64

Added lines #L61 - L64 were not covered by tests
}
}
}

0 comments on commit 8eeeb15

Please sign in to comment.