Skip to content

Commit

Permalink
add shutdown handler
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Jan 9, 2024
1 parent 9438024 commit c9ca869
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions kitsune/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#[macro_use]
extern crate tracing;

use clap::Parser;
use kitsune::consts::STARTUP_FIGLET;
use kitsune_config::Configuration;
use kitsune_core::consts::VERSION;
use kitsune_job_runner::JobDispatcherState;
use miette::{Context, IntoDiagnostic};
use std::{env, future, path::PathBuf};
use std::{env, path::PathBuf};
use tokio::signal::unix::SignalKind;

#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
Expand All @@ -21,6 +19,28 @@ struct Args {
config: PathBuf,
}

async fn shutdown_signal() {
let ctrl_c = tokio::signal::ctrl_c();

#[cfg(target_family = "unix")]
let second_signal = async {
let mut terminate = tokio::signal::unix::signal(SignalKind::terminate()).unwrap();
let mut quit = tokio::signal::unix::signal(SignalKind::quit()).unwrap();

tokio::select! {
_ = terminate.recv() => (),
_ = quit.recv() => (),
}
};
#[cfg(not(target_family = "unix"))]
let second_signal = std::future::pending();

tokio::select! {
_ = ctrl_c => (),
() = second_signal => (),
}
}

async fn boot() -> miette::Result<()> {
println!("{STARTUP_FIGLET}");

Expand All @@ -40,17 +60,6 @@ async fn boot() -> miette::Result<()> {
.wrap_err("Failed to connect to the Redis instance for the job scheduler")?;

let state = kitsune::initialise_state(&config, conn, job_queue.clone()).await?;

tokio::spawn({
let server_fut = kitsune::http::run(state.clone(), config.server.clone());

async move {
if let Err(error) = server_fut.await {
error!(?error, "failed to run http server");
}
}
});

let dispatcher_state = JobDispatcherState::builder()
.attachment_service(state.service.attachment.clone())
.db_pool(state.db_pool.clone())
Expand All @@ -59,13 +68,18 @@ async fn boot() -> miette::Result<()> {
.url_service(state.service.url.clone())
.build();

tokio::spawn(kitsune_job_runner::run_dispatcher(
let server_fut = tokio::spawn(kitsune::http::run(state, config.server.clone()));
let job_runner_fut = tokio::spawn(kitsune_job_runner::run_dispatcher(
job_queue,
dispatcher_state,
config.job_queue.num_workers.get(),
));

future::pending::<()>().await;
tokio::select! {
res = server_fut => res.into_diagnostic()??,
res = job_runner_fut => res.into_diagnostic()?,
() = shutdown_signal() => (),
}

Ok(())
}
Expand Down

0 comments on commit c9ca869

Please sign in to comment.