Expand description
Gracefully shut down a process
ragequit provides a set of utilities to gracefully shut down a process. It is primarily
targeted at server processes, but may have other applications as well.
§Usage
The global SHUTDOWN instance is used to signal shutdown events and handle them gracefully
by creating ShutdownListeners.
use ragequit::{init, SHUTDOWN};
#[tokio::main]
async fn main() {
// Install default system signal handlers.
init();
let listener = SHUTDOWN.listen();
tokio::spawn(async move {
// Wait for the shutdown signal.
tokio::pin!(listener);
(&mut listener).await;
// Drop the listener, allowing the main process to exit.
println!("Goodbye");
drop(listener);
});
// Wait for a shutdown signal and for all listeners to be dropped.
SHUTDOWN.wait().await;
}Call init once during the start of the process to install the default system signal
handlers. Alternatively you can install system signal handlers yourself.
§Example for *nix systems
use core::ffi::c_int;
use nix::sys::signal::{sigaction, SaFlags, SigAction, SigHandler, SigSet, Signal};
use ragequit::SHUTDOWN;
let action = SigAction::new(SigHandler::Handler(quit), SaFlags::empty(), SigSet::empty());
unsafe {
let _ = sigaction(Signal::SIGINT, &action);
let _ = sigaction(Signal::SIGTERM, &action);
}
extern "C" fn quit(_: c_int) {
SHUTDOWN.quit();
}§Tokio dependency
ragequit depends on tokio only for synchronization primitives. It does not depend on the
tokio runtime. ragequit works in any asynchronous runtime.
Structs§
- Shutdown
- A group of
ShutdownListeners waiting for a shutdown signal. - Shutdown
Listener - A future and RAII structure waiting for a shutdown signal.
- Wait
- A future that completes once a shutdown signal has been received and all
ShutdownListeners have been dropped.