Skip to content

Commit

Permalink
Merge pull request #93 from cspr-rad/feature/clean-shutdown
Browse files Browse the repository at this point in the history
clean shutdown + default log level INFO
  • Loading branch information
Rom3dius committed May 7, 2024
2 parents ac5946e + 2dd7947 commit 27ef3fc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
27 changes: 25 additions & 2 deletions kairos-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,33 @@ pub fn app_router(state: Arc<state::BatchStateManager>) -> Router {
pub async fn run(config: ServerConfig) {
let app = app_router(BatchStateManager::new_empty());

tracing::info!("starting http server on `{}`", config.socket_addr);
let listener = tokio::net::TcpListener::bind(config.socket_addr)
.await
.unwrap();
tracing::info!("listening on `{}`", listener.local_addr().unwrap());
axum::serve(listener, app).await.unwrap();

axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal())
.await
.unwrap();
}

async fn shutdown_signal() {
let ctrl_c = async {
tokio::signal::ctrl_c()
.await
.expect("Failed to install Ctrl+C handler");
};

let terminate = async {
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
.expect("Failed to install signal handler")
.recv()
.await;
};

tokio::select! {
_ = ctrl_c => {tracing::info!("Received CTRL+C signal, shutting down...")},
_ = terminate => {tracing::info!("Received shutdown signal, shutting down...")},
}
}
7 changes: 6 additions & 1 deletion kairos-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ use kairos_server::config::ServerConfig;

#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let subscriber = tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.finish();

tracing::subscriber::set_global_default(subscriber).expect("Failed to set subscriber");

// loads the environment from the current directories .env file
// if the .env does not exist in the current directory,
// we still go ahead and try to obtain a server config from the environment
Expand Down

0 comments on commit 27ef3fc

Please sign in to comment.