Rust crate to simplify graceful shutdowns
MIT License
Rust crate to simplify graceful async shutdowns:
This crate is on crates.io and can be
used by adding it to your dependencies in your project's Cargo.toml
.
[dependencies]
elegant-departure = "0.3"
For a optional tokio integration, you need to enable the tokio feature:
[dependencies]
elegant-departure = { version = "0.3", features = "tokio" }
Examples can be found in the example directory:
select!
Minimal example using the tokio integration:
use std::time::Duration;
async fn worker(name: &'static str) {
let guard = elegant_departure::get_shutdown_guard();
println!("[{}] working", name);
guard.wait().await;
println!("[{}] shutting down", name);
tokio::time::sleep(Duration::from_secs(1)).await;
println!("[{}] done", name);
}
#[tokio::main]
async fn main() {
tokio::spawn(worker("worker 1"));
tokio::spawn(worker("worker 2"));
elegant_departure::tokio::depart()
// Shutdown on Ctrl+C and SIGTERM
.on_termination()
.await
}