mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2025-07-28 02:38:30 +00:00
split main signal handler to unit
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
f40a3ea4a6
commit
959fd2e6c4
2 changed files with 60 additions and 53 deletions
56
src/main/signal.rs
Normal file
56
src/main/signal.rs
Normal file
|
@ -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<Server>) {
|
||||
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<Server>) {
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = signal::ctrl_c() => {
|
||||
warn!("Received Ctrl+C");
|
||||
if let Err(e) = server.server.signal.send("SIGINT") {
|
||||
debug_error!("signal channel: {e}");
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue