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 9, 2025
1 parent 61b15b7 commit bba7916
Showing 1 changed file with 68 additions and 25 deletions.
93 changes: 68 additions & 25 deletions ee/tabby-webserver/src/service/background_job/db.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::sync::Arc;

use anyhow::Context;
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 @@ -21,36 +20,80 @@ 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?;

Self::data_retention(now, &db).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 let Err(e) = Self::data_retention(now, &db).await {
errors.push(format!("Failed to run data retention job: {}", e));
}

Check warning on line 60 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-L60

Added lines #L58 - L60 were not covered by tests

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

Check warning on line 63 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#L62-L63

Added lines #L62 - L63 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 68 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#L65-L68

Added lines #L65 - L68 were not covered by tests
}
}

async fn data_retention(now: DateTime<Utc>, db: &DbConn) -> tabby_schema::Result<()> {
db.delete_job_run_before_three_months(now)
.await
.context("Failed to clean up and retain only the last 3 months of jobs")?;
let mut errors = vec![];

db.delete_user_events_before_three_months(now)
.await
.context("Failed to clean up and retain only the last 3 months of user events")?;
if let Err(e) = db.delete_job_run_before_three_months(now).await {
errors.push(format!(
"Failed to clean up and retain only the last 3 months of jobs: {}",
e
));

Check warning on line 79 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#L76-L79

Added lines #L76 - L79 were not covered by tests
}

Ok(())
if let Err(e) = db.delete_user_events_before_three_months(now).await {
errors.push(format!(
"Failed to clean up and retain only the last 3 months of user events: {}",
e
));

Check warning on line 86 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#L83-L86

Added lines #L83 - L86 were not covered by tests
}

if errors.is_empty() {
Ok(())
} else {
Err(CoreError::Other(anyhow::anyhow!(
"Failed to run data retention job:\n{}",
errors.join(";\n")
)))

Check warning on line 95 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#L92-L95

Added lines #L92 - L95 were not covered by tests
}
}
}

Expand Down

0 comments on commit bba7916

Please sign in to comment.