Ensure SurrealDB listens to SIGTERM/SIGINT signals and quits gracefully (#1639)
This commit is contained in:
parent
c3620d01eb
commit
15f7fb3eee
2 changed files with 36 additions and 4 deletions
|
@ -11,6 +11,7 @@ mod output;
|
|||
mod params;
|
||||
mod rpc;
|
||||
mod session;
|
||||
mod signals;
|
||||
mod signin;
|
||||
mod signup;
|
||||
mod sql;
|
||||
|
@ -74,21 +75,29 @@ pub async fn init() -> Result<(), Error> {
|
|||
.cert_path(c)
|
||||
.key_path(k)
|
||||
.bind_with_graceful_shutdown(opt.bind, async move {
|
||||
tokio::signal::ctrl_c().await.expect("Failed to listen to shutdown signal");
|
||||
// Capture the shutdown signals and log that the graceful shutdown has started
|
||||
let result = signals::listen().await.expect("Failed to listen to shutdown signal");
|
||||
info!(target: LOG, "{} received. Start graceful shutdown...", result);
|
||||
});
|
||||
// Log the server startup status
|
||||
info!(target: LOG, "Started web server on {}", &adr);
|
||||
// Run the server forever
|
||||
srv.await
|
||||
srv.await;
|
||||
// Log the server shutdown event
|
||||
info!(target: LOG, "Shutdown complete. Bye!")
|
||||
} else {
|
||||
// Bind the server to the desired port
|
||||
let (adr, srv) = warp::serve(net).bind_with_graceful_shutdown(opt.bind, async move {
|
||||
tokio::signal::ctrl_c().await.expect("Failed to listen to shutdown signal");
|
||||
// Capture the shutdown signals and log that the graceful shutdown has started
|
||||
let result = signals::listen().await.expect("Failed to listen to shutdown signal");
|
||||
info!(target: LOG, "{} received. Start graceful shutdown...", result);
|
||||
});
|
||||
// Log the server startup status
|
||||
info!(target: LOG, "Started web server on {}", &adr);
|
||||
// Run the server forever
|
||||
srv.await
|
||||
srv.await;
|
||||
// Log the server shutdown event
|
||||
info!(target: LOG, "Shutdown complete. Bye!")
|
||||
};
|
||||
|
||||
Ok(())
|
||||
|
|
23
src/net/signals.rs
Normal file
23
src/net/signals.rs
Normal file
|
@ -0,0 +1,23 @@
|
|||
use crate::err::Error;
|
||||
|
||||
#[cfg(unix)]
|
||||
use tokio::signal::unix as signal_os;
|
||||
#[cfg(windows)]
|
||||
use tokio::signal::windows as signal_os;
|
||||
|
||||
pub async fn listen() -> Result<String, Error> {
|
||||
// Get the operating system signal types
|
||||
let mut interrupt = signal_os::signal(signal_os::SignalKind::interrupt())?;
|
||||
let mut terminate = signal_os::signal(signal_os::SignalKind::terminate())?;
|
||||
// Wait until we receive a shutdown signal
|
||||
tokio::select! {
|
||||
// Wait for an interrupt signal
|
||||
_ = interrupt.recv() => {
|
||||
Ok(String::from("SIGINT"))
|
||||
}
|
||||
// Wait for a terminate signal
|
||||
_ = terminate.recv() => {
|
||||
Ok(String::from("SIGTERM"))
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue