#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

#2037 in Asynchronous

Download history 1063/week @ 2024-03-14 2113/week @ 2024-03-21 2915/week @ 2024-03-28 1829/week @ 2024-04-04 2910/week @ 2024-04-11 2347/week @ 2024-04-18 2702/week @ 2024-04-25 2372/week @ 2024-05-02 2886/week @ 2024-05-09 2285/week @ 2024-05-16 2366/week @ 2024-05-23 2430/week @ 2024-05-30 2823/week @ 2024-06-06 2613/week @ 2024-06-13 2106/week @ 2024-06-20 1272/week @ 2024-06-27

9,406 downloads per month
Used in 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–3MB
~46K SLoC