Skip to content

Commit

Permalink
support running with TABBY_WEBSERVER_CONTROLLER_ONESHOT
Browse files Browse the repository at this point in the history
  • Loading branch information
wsxiaoys committed Apr 28, 2024
1 parent 4868c91 commit 568be7a
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions ee/tabby-webserver/src/cron/controller.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{pin::Pin, sync::Arc};
use std::{pin::Pin, sync::Arc, time::Duration};

use futures::Future;
use juniper::ID;
Expand All @@ -10,6 +10,7 @@ use crate::schema::job::JobService;
pub struct JobController {
scheduler: JobScheduler,
service: Arc<dyn JobService>,
is_oneshot: bool,
}

impl JobController {
Expand All @@ -18,7 +19,17 @@ impl JobController {
let scheduler = JobScheduler::new()
.await
.expect("failed to create job scheduler");
Self { scheduler, service }
let is_oneshot = std::env::var("TABBY_WEBSERVER_CONTROLLER_ONESHOT").is_ok();
if is_oneshot {
warn!(
"Running controller job as oneshot, this should only be used for debugging purpose..."
);
}
Self {
scheduler,
service,
is_oneshot,
}
}

Check warning on line 33 in ee/tabby-webserver/src/cron/controller.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/controller.rs#L17-L33

Added lines #L17 - L33 were not covered by tests

pub async fn run(&self) {
Expand Down Expand Up @@ -60,6 +71,47 @@ impl JobController {
}

Check warning on line 71 in ee/tabby-webserver/src/cron/controller.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/controller.rs#L55-L71

Added lines #L55 - L71 were not covered by tests

async fn register_impl<T>(&mut self, is_public: bool, name: &str, schedule: &str, func: T)
where
T: FnMut(&JobContext) -> Pin<Box<dyn Future<Output = anyhow::Result<i32>> + Send>>
+ Send
+ Sync
+ Clone
+ 'static,
{
if self.is_oneshot {
self.run_oneshot(is_public, name, func).await;

Check warning on line 82 in ee/tabby-webserver/src/cron/controller.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/controller.rs#L73-L82

Added lines #L73 - L82 were not covered by tests
} else {
self.run_schedule(is_public, name, schedule, func).await;

Check warning on line 84 in ee/tabby-webserver/src/cron/controller.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/controller.rs#L84

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

Check warning on line 86 in ee/tabby-webserver/src/cron/controller.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/controller.rs#L86

Added line #L86 was not covered by tests

async fn run_oneshot<T>(&self, is_public: bool, name: &str, mut func: T)
where
T: FnMut(&JobContext) -> Pin<Box<dyn Future<Output = anyhow::Result<i32>> + Send>>
+ Send
+ Sync
+ Clone
+ 'static,
{
let name = name.to_owned();
let context = JobContext::new(is_public, &name, self.service.clone()).await;
tokio::spawn(async move {
tokio::time::sleep(Duration::from_secs(5)).await;

Check warning on line 99 in ee/tabby-webserver/src/cron/controller.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/controller.rs#L88-L99

Added lines #L88 - L99 were not covered by tests

match func(&context).await {
Ok(exit_code) => {
debug!("Job `{}` completed with exit code {}", &name, exit_code);
context.complete(exit_code).await;

Check warning on line 104 in ee/tabby-webserver/src/cron/controller.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/controller.rs#L101-L104

Added lines #L101 - L104 were not covered by tests
}
Err(e) => {
warn!("Job `{}` failed: {}", &name, e);
context.complete(-1).await;

Check warning on line 108 in ee/tabby-webserver/src/cron/controller.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/controller.rs#L106-L108

Added lines #L106 - L108 were not covered by tests
}
}
});
}

Check warning on line 112 in ee/tabby-webserver/src/cron/controller.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/controller.rs#L111-L112

Added lines #L111 - L112 were not covered by tests

async fn run_schedule<T>(&mut self, is_public: bool, name: &str, schedule: &str, func: T)
where
T: FnMut(&JobContext) -> Pin<Box<dyn Future<Output = anyhow::Result<i32>> + Send>>
+ Send
Expand Down

0 comments on commit 568be7a

Please sign in to comment.