Skip to content

Commit

Permalink
chore: manually specify job name instead of use Display
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Zhang <[email protected]>
  • Loading branch information
zwpaper committed Jan 17, 2025
1 parent b128268 commit 9bc4fb9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
45 changes: 18 additions & 27 deletions ee/tabby-webserver/src/service/background_job/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod license_check;
mod third_party_integration;
mod web_crawler;

use std::{fmt::Display, str::FromStr, sync::Arc};
use std::{str::FromStr, sync::Arc};

use cron::Schedule;
use daily::DailyJob;
Expand Down Expand Up @@ -38,7 +38,7 @@ pub use web_crawler::WebCrawlerJob;

use self::third_party_integration::SyncIntegrationJob;

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum BackgroundJobEvent {
SchedulerGitRepository(CodeRepository),
SchedulerGithubGitlabRepository(ID),
Expand All @@ -49,26 +49,6 @@ pub enum BackgroundJobEvent {
Daily,
}

impl Display for BackgroundJobEvent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BackgroundJobEvent::SchedulerGitRepository(repository) => {
write!(f, "SyncGitRepository::{}", repository.git_url)
}
BackgroundJobEvent::SchedulerGithubGitlabRepository(integration_id) => {
write!(f, "SyncGithubGitlabRepository::{}", integration_id)
}
BackgroundJobEvent::SyncThirdPartyRepositories(integration_id) => {
write!(f, "SyncThirdPartyRepositories::{}", integration_id)
}
BackgroundJobEvent::WebCrawler(job) => write!(f, "WebCrawler::{}", job.url()),
BackgroundJobEvent::IndexGarbageCollection => write!(f, "IndexGarbageCollection"),
BackgroundJobEvent::Hourly => write!(f, "Hourly"),
BackgroundJobEvent::Daily => write!(f, "Daily"),
}
}
}

impl BackgroundJobEvent {
pub fn name(&self) -> &'static str {
match self {
Expand All @@ -90,9 +70,20 @@ impl BackgroundJobEvent {
}

macro_rules! notify_job_error {
($notification_service:expr, $err:expr, $name:expr, $id:expr) => {{
($notification_service:expr, $err:expr, $event:expr, $id:expr) => {{
let id = $id.as_id();
warn!("job {} failed: {:?}", $name, $err);
warn!("job {:?} failed: {:?}", $event, $err);
let name = match $event {
BackgroundJobEvent::SchedulerGitRepository(_) => "Git repository synchronize",
BackgroundJobEvent::SchedulerGithubGitlabRepository(_) => {
"Github/Gitlab repository synchronize"
}
BackgroundJobEvent::SyncThirdPartyRepositories(_) => "Integration synchronize",
BackgroundJobEvent::WebCrawler(_) => "Web crawler",
BackgroundJobEvent::IndexGarbageCollection => "Index garbage collection",
BackgroundJobEvent::Hourly => "Hourly",
BackgroundJobEvent::Daily => "Daily",
};
$notification_service
.create(
NotificationRecipient::Admin,
Expand All @@ -103,7 +94,7 @@ Job `{}` has failed.
Please check the log at [Jobs Detail](/jobs/detail?id={}) to identify the underlying issue.
"#,
$name, id
name, id
),
)
.await
Expand Down Expand Up @@ -155,7 +146,7 @@ pub async fn start(
continue;
};

let job_name = event.to_string();
let event_clone = event.clone();
let result = match event {

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#L149-L150

Added lines #L149 - L150 were not covered by tests
BackgroundJobEvent::SchedulerGitRepository(repository_config) => {
let job = SchedulerGitJob::new(repository_config);
Expand Down Expand Up @@ -201,7 +192,7 @@ pub async fn start(
Err(err) => {
logkit::warn!(exit_code = 1; "Job failed: {}", err);
logger.finalize().await;
notify_job_error!(notification_service, err, job_name, job_id);
notify_job_error!(notification_service, err, event_clone, job_id);

Check warning on line 195 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#L191-L195

Added lines #L191 - L195 were not covered by tests
},
_ => {
logkit::info!(exit_code = 0; "Job completed successfully");
Expand Down
6 changes: 1 addition & 5 deletions ee/tabby-webserver/src/service/background_job/web_crawler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use super::helper::Job;

const CRAWLER_TIMEOUT_SECS: u64 = 7200;

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct WebCrawlerJob {
source_id: String,
url: String,
Expand All @@ -34,10 +34,6 @@ impl WebCrawlerJob {
}
}

pub fn url(&self) -> &str {
&self.url
}

pub async fn run_impl(self, embedding: Arc<dyn Embedding>) -> tabby_schema::Result<()> {
logkit::info!("Starting doc index pipeline for {}", self.url);
let embedding = embedding.clone();
Expand Down

0 comments on commit 9bc4fb9

Please sign in to comment.