Skip to content

Commit

Permalink
fix: OS signal
Browse files Browse the repository at this point in the history
  • Loading branch information
a-wing committed Jan 4, 2024
1 parent b9640a4 commit 409e8a9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use axum::{
};
use forward::info::Layer;
use http::header::ToStrError;
use log::info;
use log::{info, debug, error};
use thiserror::Error;
#[cfg(debug_assertions)]
use tower_http::services::{ServeDir, ServeFile};
Expand All @@ -37,6 +37,7 @@ mod forward;
mod media;
mod metrics;
mod path;
mod signal;

#[tokio::main]
async fn main() {
Expand Down Expand Up @@ -91,10 +92,11 @@ async fn main() {
.route("/metrics", get(metrics))
.with_state(app_state);
app = static_server(app);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
tokio::select!{
Err(e) = axum::Server::bind(&addr).serve(app.into_make_service()) => error!("Application error: {e}"),
msg = signal::wait_for_stop_signal() => debug!("Received signal: {}", msg),
}
info!("Server shutdown");
}

async fn metrics() -> String {
Expand Down
43 changes: 43 additions & 0 deletions src/signal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/// References: https://stackoverflow.com/questions/77585473/rust-tokio-how-to-handle-more-signals-than-just-sigint-i-e-sigquit
/// Waits for a signal that requests a graceful shutdown, like SIGTERM or SIGINT.
#[cfg(unix)]
async fn wait_for_signal_impl() -> String {
use tokio::signal::unix::{signal, SignalKind};

// Infos here:
// https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html
let mut signal_terminate = signal(SignalKind::terminate()).unwrap();
let mut signal_interrupt = signal(SignalKind::interrupt()).unwrap();

tokio::select! {
_ = signal_terminate.recv() => "SIGTERM".to_string(),
_ = signal_interrupt.recv() => "SIGINT".to_string(),
}
}

/// Waits for a signal that requests a graceful shutdown, Ctrl-C (SIGINT).
#[cfg(windows)]
async fn wait_for_signal_impl() -> String {
use tokio::signal::windows;

// Infos here:
// https://learn.microsoft.com/en-us/windows/console/handlerroutine
let mut signal_c = windows::ctrl_c().unwrap();
let mut signal_break = windows::ctrl_break().unwrap();
let mut signal_close = windows::ctrl_close().unwrap();
let mut signal_shutdown = windows::ctrl_shutdown().unwrap();

tokio::select! {
_ = signal_c.recv() => "CTRL_C".to_string(),
_ = signal_break.recv() => "CTRL_BREAK".to_string(),
_ = signal_close.recv() => "CTRL_CLOSE".to_string(),
_ = signal_shutdown.recv() => "CTRL_SHUTDOWN".to_string(),
}
}

/// Registers signal handlers and waits for a signal that
/// indicates a shutdown request.
pub(crate) async fn wait_for_stop_signal() -> String {
wait_for_signal_impl().await
}

0 comments on commit 409e8a9

Please sign in to comment.