Crates.io | elegant-departure |
lib.rs | elegant-departure |
version | 0.3.1 |
source | src |
created_at | 2021-11-21 16:41:26.61169 |
updated_at | 2024-11-19 15:16:49.732397 |
description | Utility crate to simplify graceful shutdown |
homepage | |
repository | https://github.com/Dav1dde/elegant-departure |
max_upload_size | |
id | 485359 |
size | 87,309 |
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
}