Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(license): add license expiration check and daily cron job #3566

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ee/tabby-schema/src/schema/license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@

Ok(())
}

pub fn expire_in_days(&self) -> Option<i64> {
self.expires_at.map(|expires_at| {
let now = Utc::now();
let duration = expires_at.signed_duration_since(now);
duration.num_days()
})
}

Check warning on line 91 in ee/tabby-schema/src/schema/license.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-schema/src/schema/license.rs#L85-L91

Added lines #L85 - L91 were not covered by tests
}

#[async_trait]
Expand Down
54 changes: 54 additions & 0 deletions ee/tabby-webserver/src/service/background_job/license_check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::sync::Arc;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use tabby_schema::{
context::ContextService,
license::{LicenseService, LicenseType},
notification::{NotificationRecipient, NotificationService},
};

use super::helper::Job;

#[derive(Debug, Serialize, Deserialize, Clone)]

Check warning on line 13 in ee/tabby-webserver/src/service/background_job/license_check.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/license_check.rs#L13

Added line #L13 was not covered by tests
pub struct LicenseCheckJob;

impl Job for LicenseCheckJob {
const NAME: &'static str = "license_check";
}

impl LicenseCheckJob {
pub async fn cron(
_now: DateTime<Utc>,
license_service: Arc<dyn LicenseService>,
notification_service: Arc<dyn NotificationService>,
) -> tabby_schema::Result<()> {
let license = license_service.read().await?;
if license.r#type == LicenseType::Community {
return Ok(());
}
if let Some(expire_in_days) = license.expire_in_days() {
if expire_in_days < 7 && expire_in_days > 0 {
notification_service
.create(
NotificationRecipient::Admin,
&make_expring_message(expire_in_days),
)
.await?;
}
}
Ok(())
}

Check warning on line 41 in ee/tabby-webserver/src/service/background_job/license_check.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/license_check.rs#L21-L41

Added lines #L21 - L41 were not covered by tests
}

fn make_expring_message(expire_in_days: i64) -> String {
format!(
r#"Your license will expire in {} days.

To ensure uninterrupted access to Tabby and to continue enjoying all the premium features, please renew your license before it expires.

If you have any questions or need assistance with the renewal process, please don't hesitate to contact our support team at [email protected].
"#,
expire_in_days
)
}

Check warning on line 54 in ee/tabby-webserver/src/service/background_job/license_check.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/background_job/license_check.rs#L44-L54

Added lines #L44 - L54 were not covered by tests
14 changes: 14 additions & 0 deletions ee/tabby-webserver/src/service/background_job/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod git;
mod helper;
mod index_garbage_collection;
mod license_check;
mod third_party_integration;
mod web_crawler;

Expand All @@ -13,6 +14,7 @@
use helper::{CronStream, Job, JobLogger};
use index_garbage_collection::IndexGarbageCollection;
use juniper::ID;
use license_check::LicenseCheckJob;
use serde::{Deserialize, Serialize};
use tabby_common::config::CodeRepository;
use tabby_db::DbConn;
Expand All @@ -21,6 +23,8 @@
context::ContextService,
integration::IntegrationService,
job::JobService,
license::LicenseService,
notification::NotificationService,
repository::{GitRepositoryService, RepositoryService, ThirdPartyRepositoryService},
};
use third_party_integration::SchedulerGithubGitlabJob;
Expand Down Expand Up @@ -64,12 +68,17 @@
integration_service: Arc<dyn IntegrationService>,
repository_service: Arc<dyn RepositoryService>,
context_service: Arc<dyn ContextService>,
license_service: Arc<dyn LicenseService>,
notification_service: Arc<dyn NotificationService>,

Check warning on line 72 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#L71-L72

Added lines #L71 - L72 were not covered by tests
embedding: Arc<dyn Embedding>,
) {
let mut hourly =
CronStream::new(Schedule::from_str("@hourly").expect("Invalid cron expression"))
.into_stream();

let mut daily = CronStream::new(Schedule::from_str("@daily").expect("Invalid cron expression"))
.into_stream();

Check warning on line 81 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#L79-L81

Added lines #L79 - L81 were not covered by tests
tokio::spawn(async move {
loop {
tokio::select! {
Expand Down Expand Up @@ -141,6 +150,11 @@
}

},
Some(now) = daily.next() => {
if let Err(err) = LicenseCheckJob::cron(now, license_service.clone(), notification_service.clone()).await {
warn!("License check job failed: {err:?}");
}

Check warning on line 156 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#L153-L156

Added lines #L153 - L156 were not covered by tests
}
else => {
warn!("Background job channel closed");
break;
Expand Down
2 changes: 2 additions & 0 deletions ee/tabby-webserver/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@
integration.clone(),
repository.clone(),
context.clone(),
license.clone(),
notification.clone(),

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L134 - L135 were not covered by tests
embedding.clone(),
)
.await;
Expand Down
Loading