fix signal handling

This commit is contained in:
John Smith 2024-03-25 23:06:47 -04:00 committed by Christien Rioux
parent 6ed458260a
commit 309492e9a8
3 changed files with 31 additions and 24 deletions

View File

@ -405,6 +405,12 @@ fn main() -> EyreResult<()> {
// Init combined console/file logger
let veilid_logs = VeilidLogs::setup(settings.clone())?;
run_veilid_server(settings, server_mode, veilid_logs).await
cfg_if! {
if #[cfg(windows)] {
run_veilid_server(settings, server_mode, veilid_logs).await
} else if #[cfg(unix)] {
unix::run_veilid_server_with_signals(settings, server_mode, veilid_logs).await
}
}
})
}

View File

@ -32,16 +32,8 @@ pub fn shutdown() {
}
}
pub async fn run_veilid_server(
settings: Settings,
server_mode: ServerMode,
veilid_logs: VeilidLogs,
) -> EyreResult<()> {
run_veilid_server_internal(settings, server_mode, veilid_logs).await
}
//#[instrument(err, skip_all)]
pub async fn run_veilid_server_internal(
pub async fn run_veilid_server(
settings: Settings,
server_mode: ServerMode,
veilid_logs: VeilidLogs,

View File

@ -24,6 +24,28 @@ async fn handle_signals(mut signals: Signals) {
}
}
pub async fn run_veilid_server_with_signals(
settings: Settings,
server_mode: ServerMode,
veilid_logs: VeilidLogs,
) -> EyreResult<()> {
// Catch signals
let signals =
Signals::new([SIGHUP, SIGTERM, SIGINT, SIGQUIT]).wrap_err("failed to init signals")?;
let handle = signals.handle();
let signals_task = spawn(handle_signals(signals));
// Run veilid server
let res = run_veilid_server(settings, server_mode, veilid_logs).await;
// Terminate the signal stream.
handle.close();
let _ = signals_task.await;
res
}
#[warn(missing_docs)]
#[instrument(err)]
pub fn run_daemon(settings: Settings, _args: CmdlineArgs) -> EyreResult<()> {
@ -82,19 +104,6 @@ pub fn run_daemon(settings: Settings, _args: CmdlineArgs) -> EyreResult<()> {
// Daemonize
daemon.start().wrap_err("Failed to daemonize")?;
// Catch signals
let signals =
Signals::new([SIGHUP, SIGTERM, SIGINT, SIGQUIT]).wrap_err("failed to init signals")?;
let handle = signals.handle();
let signals_task = spawn(handle_signals(signals));
let res = run_veilid_server(settings, ServerMode::Normal, veilid_logs).await;
// Terminate the signal stream.
handle.close();
let _ = signals_task.await;
res
run_veilid_server_with_signals(settings, ServerMode::Normal, veilid_logs).await
})
}