Skip to content

Commit

Permalink
chore(job): change daily and hourly cron into standalone job
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Zhang <[email protected]>
  • Loading branch information
zwpaper committed Jan 16, 2025
1 parent 30680bf commit a27f123
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 25 deletions.
32 changes: 32 additions & 0 deletions ee/tabby-webserver/src/service/background_job/daily.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::sync::Arc;

use crate::service::background_job::LicenseCheckJob;
use chrono::Utc;
use serde::{Deserialize, Serialize};
use tabby_schema::{license::LicenseService, notification::NotificationService};

use super::helper::Job;

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct DailyJob;

impl Job for DailyJob {
const NAME: &'static str = "daily";
}

impl DailyJob {
pub async fn run(
&self,
license_service: Arc<dyn LicenseService>,
notification_service: Arc<dyn NotificationService>,
) -> tabby_schema::Result<()> {
let now = Utc::now();

if let Err(err) =
LicenseCheckJob::cron(now, license_service.clone(), notification_service.clone()).await
{
logkit::warn!("License check job failed: {err:?}");
}
Ok(())
}
}
73 changes: 73 additions & 0 deletions ee/tabby-webserver/src/service/background_job/hourly.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use std::sync::Arc;

use crate::service::background_job::{
db::DbMaintainanceJob, IndexGarbageCollection, SchedulerGitJob, SchedulerGithubGitlabJob,
SyncIntegrationJob,
};
use chrono::Utc;
use serde::{Deserialize, Serialize};
use tabby_db::DbConn;
use tabby_schema::{
context::ContextService,
integration::IntegrationService,
job::JobService,
repository::{GitRepositoryService, RepositoryService, ThirdPartyRepositoryService},
};

use super::helper::Job;

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct HourlyJob;

impl Job for HourlyJob {
const NAME: &'static str = "hourly";
}

impl HourlyJob {
pub async fn run(
&self,
db: DbConn,
context_service: Arc<dyn ContextService>,
git_repository_service: Arc<dyn GitRepositoryService>,
job_service: Arc<dyn JobService>,
integration_service: Arc<dyn IntegrationService>,
third_party_repository_service: Arc<dyn ThirdPartyRepositoryService>,
repository_service: Arc<dyn RepositoryService>,
) -> tabby_schema::Result<()> {
let now = Utc::now();

if let Err(err) = DbMaintainanceJob::cron(now, context_service.clone(), db.clone()).await {
logkit::warn!("Database maintainance failed: {:?}", err);
}

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

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

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

if let Err(err) = IndexGarbageCollection
.run(repository_service.clone(), context_service.clone())
.await
{
logkit::warn!("Index garbage collection job failed: {err:?}");
}
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::sync::Arc;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use tabby_schema::{
context::ContextService,
license::{LicenseService, LicenseType},
notification::{NotificationRecipient, NotificationService},
};
Expand Down
60 changes: 36 additions & 24 deletions ee/tabby-webserver/src/service/background_job/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod daily;
mod db;
mod git;
mod helper;
mod hourly;
mod index_garbage_collection;
mod license_check;
mod third_party_integration;
Expand All @@ -9,9 +11,11 @@ mod web_crawler;
use std::{str::FromStr, sync::Arc};

use cron::Schedule;
use daily::DailyJob;
use futures::StreamExt;
use git::SchedulerGitJob;
use helper::{CronStream, Job, JobLogger};
use hourly::HourlyJob;
use index_garbage_collection::IndexGarbageCollection;
use juniper::ID;
use license_check::LicenseCheckJob;
Expand All @@ -31,7 +35,7 @@ use third_party_integration::SchedulerGithubGitlabJob;
use tracing::{debug, warn};
pub use web_crawler::WebCrawlerJob;

use self::{db::DbMaintainanceJob, third_party_integration::SyncIntegrationJob};
use self::third_party_integration::SyncIntegrationJob;

#[derive(Debug, Serialize, Deserialize)]
pub enum BackgroundJobEvent {
Expand All @@ -40,6 +44,8 @@ pub enum BackgroundJobEvent {
SyncThirdPartyRepositories(ID),
WebCrawler(WebCrawlerJob),
IndexGarbageCollection,
Hourly,
Daily,
}

impl BackgroundJobEvent {
Expand All @@ -52,6 +58,8 @@ impl BackgroundJobEvent {
BackgroundJobEvent::SyncThirdPartyRepositories(_) => SyncIntegrationJob::NAME,
BackgroundJobEvent::WebCrawler(_) => WebCrawlerJob::NAME,
BackgroundJobEvent::IndexGarbageCollection => IndexGarbageCollection::NAME,
BackgroundJobEvent::Hourly => HourlyJob::NAME,
BackgroundJobEvent::Daily => DailyJob::NAME,
}
}

Expand Down Expand Up @@ -120,6 +128,25 @@ pub async fn start(
let job = IndexGarbageCollection;
job.run(repository_service.clone(), context_service.clone()).await
}
BackgroundJobEvent::Hourly => {
let job = HourlyJob;
job.run(
db.clone(),
context_service.clone(),
git_repository_service.clone(),
job_service.clone(),
integration_service.clone(),
third_party_repository_service.clone(),
repository_service.clone(),
).await
}
BackgroundJobEvent::Daily => {
let job = DailyJob;
job.run(
license_service.clone(),
notification_service.clone(),
).await
}
} {
logkit::info!(exit_code = 1; "Job failed {}", err);
} else {
Expand All @@ -128,31 +155,16 @@ pub async fn start(
logger.finalize().await;
debug!("Background job {} completed", job.id);
},
Some(now) = hourly.next() => {
if let Err(err) = DbMaintainanceJob::cron(now, context_service.clone(), db.clone()).await {
warn!("Database maintainance failed: {:?}", err);
}

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

if let Err(err) = SyncIntegrationJob::cron(now, integration_service.clone(), job_service.clone()).await {
warn!("Sync integration job failed: {:?}", err);
Some(_) = hourly.next() => {
match job_service.trigger(BackgroundJobEvent::Hourly.to_command()).await {
Err(err) => warn!("Hourly background job failed {}", err),
Ok(id) => debug!("Hourly background job {} completed", id),
}

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

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

},
Some(now) = daily.next() => {
if let Err(err) = LicenseCheckJob::cron(now, license_service.clone(), notification_service.clone()).await {
warn!("License check job failed: {err:?}");
Some(_) = daily.next() => {
match job_service.trigger(BackgroundJobEvent::Daily.to_command()).await {
Err(err) => warn!("Daily background job failed {}", err),
Ok(id) => debug!("Daily background job {} completed", id),
}
}
else => {
Expand Down

0 comments on commit a27f123

Please sign in to comment.