#shutdown #async

shutdown-async

Asynchronous Shutdown

3 unstable releases

0.1.1 Mar 31, 2023
0.1.0 Mar 30, 2023
0.0.0 Mar 30, 2023

#2276 in Asynchronous

Download history 2689/week @ 2024-11-14 1704/week @ 2024-11-21 1837/week @ 2024-11-28 2168/week @ 2024-12-05 1752/week @ 2024-12-12 1424/week @ 2024-12-19 1069/week @ 2024-12-26 1744/week @ 2025-01-02 1509/week @ 2025-01-09 2728/week @ 2025-01-16 2427/week @ 2025-01-23 2206/week @ 2025-01-30 2643/week @ 2025-02-06 3141/week @ 2025-02-13 3002/week @ 2025-02-20 2564/week @ 2025-02-27

11,673 downloads per month
Used in 2 crates (via tokio-utils)

MIT license

13KB
124 lines

Shutdown Async

github crates.io docs.rs build status codecov

A library for gracefully shutting down asynchronous applications

This may be useful when you want to allow all in-flight processing to complete before shutting down in order to maintain a consistent state.

Usage

Add this to your Cargo.toml:

[dependencies]
shutdown-async = "0.1.1"

You can use the library like so:

use shutdown_async::ShutdownController;

#[tokio::main]
async fn main() {
  let shutdown = ShutdownController::new();
   
  tokio::task::spawn({
    let mut monitor = shutdown.subscribe();
    async move {
      // Wait for something to happen
      tokio::select! {
       _ = monitor.recv() => { println!("shutdown initiated"); }
       _ = tokio::time::sleep(ONE_YEAR) => { println!("one year has passed!"); }
      }
    }
  });

  shutdown.shutdown().await;
}

static ONE_YEAR: std::time::Duration = std::time::Duration::from_secs(60 * 60 * 24 * 365);

Dependencies

~2–7.5MB
~47K SLoC