#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

#1788 in Asynchronous

Download history 21/week @ 2023-12-18 14/week @ 2023-12-25 49/week @ 2024-01-01 85/week @ 2024-01-08 51/week @ 2024-01-15 136/week @ 2024-01-22 351/week @ 2024-01-29 287/week @ 2024-02-05 505/week @ 2024-02-12 352/week @ 2024-02-19 364/week @ 2024-02-26 752/week @ 2024-03-04 951/week @ 2024-03-11 1691/week @ 2024-03-18 2695/week @ 2024-03-25 2075/week @ 2024-04-01

7,470 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