shutdown-async

Crates.ioshutdown-async
lib.rsshutdown-async
version0.1.1
sourcesrc
created_at2023-03-30 01:03:02.156654
updated_at2023-03-31 03:36:58.738316
descriptionAsynchronous Shutdown
homepage
repository
max_upload_size
id824555
size21,171
Will Cygan (wcygan)

documentation

README

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);
Commit count: 0

cargo fmt