From 959fd2e6c4a7218bb748f9fe9c5ce7cdbb7b8a01 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 16 Jun 2024 19:41:03 +0000 Subject: [PATCH] split main signal handler to unit Signed-off-by: Jason Volk --- src/main/main.rs | 57 ++++------------------------------------------ src/main/signal.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 53 deletions(-) create mode 100644 src/main/signal.rs diff --git a/src/main/main.rs b/src/main/main.rs index 566b0d67..618871a7 100644 --- a/src/main/main.rs +++ b/src/main/main.rs @@ -1,14 +1,15 @@ pub(crate) mod clap; mod mods; mod server; +mod signal; extern crate conduit_core as conduit; use std::{cmp, sync::Arc, time::Duration}; -use conduit::{debug_error, debug_info, error, trace, utils::available_parallelism, warn, Error, Result}; +use conduit::{debug_info, error, utils::available_parallelism, Error, Result}; use server::Server; -use tokio::{runtime, signal}; +use tokio::runtime; const WORKER_NAME: &str = "conduwuit:worker"; const WORKER_MIN: usize = 2; @@ -26,7 +27,7 @@ fn main() -> Result<(), Error> { .expect("built runtime"); let server: Arc = Server::build(args, Some(runtime.handle()))?; - runtime.spawn(signal(server.clone())); + runtime.spawn(signal::signal(server.clone())); runtime.block_on(async_main(&server))?; // explicit drop here to trace thread and tls dtors @@ -95,53 +96,3 @@ async fn async_main(server: &Arc) -> Result<(), Error> { debug_info!("Exit runtime"); Ok(()) } - -#[cfg(unix)] -#[tracing::instrument(skip_all)] -async fn signal(server: Arc) { - use signal::unix; - use unix::SignalKind; - - const CONSOLE: bool = cfg!(feature = "console"); - const RELOADING: bool = cfg!(all(conduit_mods, not(CONSOLE))); - - let mut quit = unix::signal(SignalKind::quit()).expect("SIGQUIT handler"); - let mut term = unix::signal(SignalKind::terminate()).expect("SIGTERM handler"); - loop { - trace!("Installed signal handlers"); - let sig: &'static str; - tokio::select! { - _ = signal::ctrl_c() => { sig = "SIGINT"; }, - _ = quit.recv() => { sig = "SIGQUIT"; }, - _ = term.recv() => { sig = "SIGTERM"; }, - } - - warn!("Received {sig}"); - let result = if RELOADING && sig == "SIGINT" { - server.server.reload() - } else if matches!(sig, "SIGQUIT" | "SIGTERM") || (!CONSOLE && sig == "SIGINT") { - server.server.shutdown() - } else { - server.server.signal(sig) - }; - - if let Err(e) = result { - debug_error!(?sig, "signal: {e}"); - } - } -} - -#[cfg(not(unix))] -#[tracing::instrument(skip_all)] -async fn signal(server: Arc) { - loop { - tokio::select! { - _ = signal::ctrl_c() => { - warn!("Received Ctrl+C"); - if let Err(e) = server.server.signal.send("SIGINT") { - debug_error!("signal channel: {e}"); - } - }, - } - } -} diff --git a/src/main/signal.rs b/src/main/signal.rs new file mode 100644 index 00000000..139aab78 --- /dev/null +++ b/src/main/signal.rs @@ -0,0 +1,56 @@ +use std::sync::Arc; + +use conduit::{debug_error, trace, warn}; +use tokio::signal; + +use super::server::Server; + +#[cfg(unix)] +#[tracing::instrument(skip_all)] +pub(super) async fn signal(server: Arc) { + use signal::unix; + use unix::SignalKind; + + const CONSOLE: bool = cfg!(feature = "console"); + const RELOADING: bool = cfg!(all(conduit_mods, not(CONSOLE))); + + let mut quit = unix::signal(SignalKind::quit()).expect("SIGQUIT handler"); + let mut term = unix::signal(SignalKind::terminate()).expect("SIGTERM handler"); + loop { + trace!("Installed signal handlers"); + let sig: &'static str; + tokio::select! { + _ = signal::ctrl_c() => { sig = "SIGINT"; }, + _ = quit.recv() => { sig = "SIGQUIT"; }, + _ = term.recv() => { sig = "SIGTERM"; }, + } + + warn!("Received {sig}"); + let result = if RELOADING && sig == "SIGINT" { + server.server.reload() + } else if matches!(sig, "SIGQUIT" | "SIGTERM") || (!CONSOLE && sig == "SIGINT") { + server.server.shutdown() + } else { + server.server.signal(sig) + }; + + if let Err(e) = result { + debug_error!(?sig, "signal: {e}"); + } + } +} + +#[cfg(not(unix))] +#[tracing::instrument(skip_all)] +pub(super) async fn signal(server: Arc) { + loop { + tokio::select! { + _ = signal::ctrl_c() => { + warn!("Received Ctrl+C"); + if let Err(e) = server.server.signal.send("SIGINT") { + debug_error!("signal channel: {e}"); + } + }, + } + } +}