Skip to content

Commit

Permalink
feat(notification): send notification to admin when jobs failed
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 eabcb1f
Showing 1 changed file with 52 additions and 16 deletions.
68 changes: 52 additions & 16 deletions ee/tabby-webserver/src/service/background_job/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use tabby_schema::{
integration::IntegrationService,
job::JobService,
license::LicenseService,
notification::NotificationService,
notification::{NotificationRecipient, NotificationService},
repository::{GitRepositoryService, RepositoryService, ThirdPartyRepositoryService},
};
use third_party_integration::SchedulerGithubGitlabJob;
Expand Down Expand Up @@ -60,6 +60,14 @@ impl BackgroundJobEvent {
}
}

macro_rules! append_error {
($errors:expr, $($arg:tt)*) => {{
let msg = format!($($arg)*);
warn!("{}", msg);
$errors.push(msg);
}};
}

pub async fn start(
db: DbConn,
job_service: Arc<dyn JobService>,
Expand All @@ -81,7 +89,7 @@ pub async fn start(

tokio::spawn(async move {
loop {
tokio::select! {
let result = tokio::select! {

Check warning on line 92 in ee/tabby-webserver/src/service/background_job/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/mod.rs#L92

Added line #L92 was not covered by tests
job = db.get_next_job_to_execute() => {
let Some(job) = job else {
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
Expand All @@ -100,7 +108,7 @@ pub async fn start(
continue;
};

if let Err(err) = match event {
let result = match event {

Check warning on line 111 in ee/tabby-webserver/src/service/background_job/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/mod.rs#L111

Added line #L111 was not covered by tests
BackgroundJobEvent::SchedulerGitRepository(repository_config) => {
let job = SchedulerGitJob::new(repository_config);
job.run(embedding.clone()).await
Expand All @@ -120,46 +128,74 @@ pub async fn start(
let job = IndexGarbageCollection;
job.run(repository_service.clone(), context_service.clone()).await
}
} {
logkit::info!(exit_code = 1; "Job failed {}", err);
} else {
logkit::info!(exit_code = 0; "Job completed successfully");
}
logger.finalize().await;
};
debug!("Background job {} completed", job.id);

match &result {
Err(err) => {
logkit::info!(exit_code = 1; "Job failed {}", err);
logger.finalize().await;
vec![err.to_string()]

Check warning on line 138 in ee/tabby-webserver/src/service/background_job/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/mod.rs#L134-L138

Added lines #L134 - L138 were not covered by tests
},
_ => {
logkit::info!(exit_code = 0; "Job completed successfully");
logger.finalize().await;
vec![]

Check warning on line 143 in ee/tabby-webserver/src/service/background_job/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/mod.rs#L141-L143

Added lines #L141 - L143 were not covered by tests
}
}
},
Some(now) = hourly.next() => {
let mut errors = vec![];

Check warning on line 148 in ee/tabby-webserver/src/service/background_job/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/mod.rs#L148

Added line #L148 was not covered by tests
if let Err(err) = DbMaintainanceJob::cron(now, context_service.clone(), db.clone()).await {
warn!("Database maintainance failed: {:?}", err);
append_error!(errors, "Database maintenance failed: {:?}", err);

Check warning on line 150 in ee/tabby-webserver/src/service/background_job/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/mod.rs#L150

Added line #L150 was not covered by tests
}

if let Err(err) = SchedulerGitJob::cron(now, git_repository_service.clone(), job_service.clone()).await {
warn!("Scheduler job failed: {:?}", err);
append_error!(errors, "Scheduler job failed: {:?}", err);

Check warning on line 154 in ee/tabby-webserver/src/service/background_job/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/mod.rs#L154

Added line #L154 was not covered by tests
}

if let Err(err) = SyncIntegrationJob::cron(now, integration_service.clone(), job_service.clone()).await {
warn!("Sync integration job failed: {:?}", err);
append_error!(errors, "Sync integration job failed: {:?}", err);

Check warning on line 158 in ee/tabby-webserver/src/service/background_job/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/mod.rs#L158

Added line #L158 was not covered by tests
}

if let Err(err) = SchedulerGithubGitlabJob::cron(now, third_party_repository_service.clone(), job_service.clone()).await {
warn!("Index issues job failed: {err:?}");
append_error!(errors, "Index issues job failed: {err:?}");

Check warning on line 162 in ee/tabby-webserver/src/service/background_job/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/mod.rs#L162

Added line #L162 was not covered by tests
}

if let Err(err) = IndexGarbageCollection.run(repository_service.clone(), context_service.clone()).await {
warn!("Index garbage collection job failed: {err:?}");
append_error!(errors, "Index garbage collection job failed: {err:?}");

Check warning on line 166 in ee/tabby-webserver/src/service/background_job/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/mod.rs#L166

Added line #L166 was not covered by tests
}

errors

Check warning on line 169 in ee/tabby-webserver/src/service/background_job/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/mod.rs#L169

Added line #L169 was not covered by tests
},
Some(now) = daily.next() => {
let mut errors = vec![];

Check warning on line 172 in ee/tabby-webserver/src/service/background_job/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/mod.rs#L172

Added line #L172 was not covered by tests
if let Err(err) = LicenseCheckJob::cron(now, license_service.clone(), notification_service.clone()).await {
warn!("License check job failed: {err:?}");
append_error!(errors, "License check job failed: {err:?}");

Check warning on line 174 in ee/tabby-webserver/src/service/background_job/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/mod.rs#L174

Added line #L174 was not covered by tests
}

errors

Check warning on line 177 in ee/tabby-webserver/src/service/background_job/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/mod.rs#L177

Added line #L177 was not covered by tests
}
else => {
warn!("Background job channel closed");
break;
return;

Check warning on line 181 in ee/tabby-webserver/src/service/background_job/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/mod.rs#L181

Added line #L181 was not covered by tests
}
};

if !result.is_empty() {
notification_service
.create(
NotificationRecipient::Admin,
&format!(
r#"Background job failed
{}"#,
&result.join("\n\n")
),
)
.await
.unwrap();
}

Check warning on line 198 in ee/tabby-webserver/src/service/background_job/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/mod.rs#L185-L198

Added lines #L185 - L198 were not covered by tests
}
});
}

0 comments on commit eabcb1f

Please sign in to comment.