Skip to content

Commit

Permalink
feat(webserver): when --webserver is specific, kick off scheduler sub… (
Browse files Browse the repository at this point in the history
#1397)

* feat(webserver): when --webserver is specific, kick off scheduler subprocess automatically

* fix cron string

* update
  • Loading branch information
wsxiaoys authored Feb 6, 2024
1 parent f0f9bd7 commit 0800ffa
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/tabby-scheduler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ serdeconv.workspace = true
cargo-lock = { version = "9.0.0", features = ["dependency-tree"] }
tokio-cron-scheduler = { workspace = true }
tokio = { workspace = true, features = ["process"] }
chrono.workspace = true

[dev-dependencies]
temp_testdir = "0.2"
Expand Down
7 changes: 5 additions & 2 deletions crates/tabby-scheduler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub async fn scheduler<T: RepositoryAccess + 'static>(
// Every 10 minutes
scheduler
.add(Job::new_async(
"* 1/10 * * * * *",
"0 1/10 * * * *",
move |uuid, mut scheduler| {
let access = access.clone();
let args = args.clone();
Expand Down Expand Up @@ -92,7 +92,10 @@ pub async fn scheduler<T: RepositoryAccess + 'static>(
}

if let Ok(Some(next_tick)) = scheduler.next_tick_for_job(uuid).await {
info!("Next time for scheduler job is {:?}", next_tick);
info!(
"Next time for scheduler job is {:?}",
next_tick.with_timezone(&chrono::Local)
);
}
})
},
Expand Down
3 changes: 2 additions & 1 deletion crates/tabby/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ pub async fn main(config: &Config, args: &ServeArgs) {
#[cfg(feature = "ee")]
let (api, ui) = if args.webserver {
let (api, ui) =
tabby_webserver::public::attach_webserver(api, ui, logger, code, config).await;
tabby_webserver::public::attach_webserver(api, ui, logger, code, config, args.port)
.await;
(api, ui)
} else {
let ui = ui.fallback(|| async { axum::response::Redirect::temporary("/swagger-ui") });
Expand Down
2 changes: 1 addition & 1 deletion ee/tabby-webserver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tabby-common = { path = "../../crates/tabby-common" }
tabby-db = { path = "../../ee/tabby-db" }
tarpc = { version = "0.33.0", features = ["serde-transport"] }
thiserror.workspace = true
tokio = { workspace = true, features = ["fs"] }
tokio = { workspace = true, features = ["fs", "process"] }
tokio-cron-scheduler = { workspace = true }
tokio-tungstenite = "0.20.1"
tower = { version = "0.4", features = ["util"] }
Expand Down
31 changes: 30 additions & 1 deletion ee/tabby-webserver/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{process::Stdio, sync::Arc};

use axum::{
extract::State,
Expand Down Expand Up @@ -27,6 +27,7 @@ pub async fn attach_webserver(
logger: Arc<dyn RawEventLogger>,
code: Arc<dyn CodeSearch>,
config: &Config,
local_port: u16,
) -> (Router, Router) {
let repository_cache = Arc::new(RepositoryCache::new_initialized(
config.repositories.clone(),
Expand Down Expand Up @@ -58,6 +59,17 @@ pub async fn attach_webserver(
.route("/graphiql", routing::get(graphiql("/graphql", None)))
.fallback(ui::handler);

tokio::spawn(async move {
loop {
// Give some time for server being ready.
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
start_scheduler_job(
local_port,
ctx.worker().read_registration_token().await.unwrap(),
)
.await;
}
});
(api, ui)
}

Expand All @@ -68,3 +80,20 @@ async fn distributed_tabby_layer(
) -> axum::response::Response {
ws.worker().dispatch_request(request, next).await
}

async fn start_scheduler_job(local_port: u16, registeration_token: String) {
let exe = std::env::current_exe().unwrap();
let mut child = tokio::process::Command::new(exe)
.arg("scheduler")
.arg("--url")
.arg(format!("localhost:{local_port}"))
.arg("--token")
.arg(registeration_token)
.stdin(Stdio::null())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.kill_on_drop(true)
.spawn()
.unwrap();
let _ = child.wait().await;
}

0 comments on commit 0800ffa

Please sign in to comment.