#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

#2165 in Asynchronous

Download history 2192/week @ 2024-08-23 1065/week @ 2024-08-30 2779/week @ 2024-09-06 1827/week @ 2024-09-13 2667/week @ 2024-09-20 2158/week @ 2024-09-27 2204/week @ 2024-10-04 1720/week @ 2024-10-11 2988/week @ 2024-10-18 2433/week @ 2024-10-25 1597/week @ 2024-11-01 2472/week @ 2024-11-08 2242/week @ 2024-11-15 1586/week @ 2024-11-22 2130/week @ 2024-11-29 1724/week @ 2024-12-06

8,388 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–7.5MB
~47K SLoC