Lightweight library for staged graceful shutdown.
Add dependency:
leaffall = { version = "<VERSION>", features = ["event"] }Declare ShutdownRequest static field:
static SHUTDOWN: ShutdownRequest = ShutdownRequest::new();Check stage for completion:
let complete = SHUTDOWN.is_complete();
SHUTDOWN.wait_complete().await;Trigger first stage:
SHUTDOWN.trigger();Guard stage from completion:
let guard = SHUTDOWN.get_guard();
SHUTDOWN.trigger(); // Defer trigger until all guards dropped.
drop(guard); // SHUTDOWN will be triggered here.Guard futures:
let future = tokio::time::sleep(Duration::from_secs(5));
let guarded_future = SHUTDOWN.guard_future(future);
tokio::spawn(guarded_future); // Don't forget to spawn.Chain stages:
Note that stages chained in reverse order.
// First stage. Trigger this.
static SHUTDOWN_REQUEST: ShutdownRequest = SHUTDOWN_SERVER.request();
// After SHUTDOWN_REQUEST, but before SHUTDOWN_DATABASE.
static SHUTDOWN_SERVER: ShutdownStage = SHUTDOWN_DATABASE.chain();
// Last stage.
static SHUTDOWN_DATABASE: ShutdownStage = ShutdownStage::new();