sigterm 0.3.10

Signal-aware async control and cancellation primitives for Tokio.
Documentation
/* examples/guard.rs */

use sigterm::{CancellationToken, ShutdownGuard};
use std::time::Duration;
use tokio::time::sleep;

#[tokio::main]
async fn main() {
	let token = CancellationToken::new();
	let child_token = token.child_token();

	tokio::spawn(async move {
		println!("Worker waiting for cancellation...");
		child_token.cancelled().await;
		println!("Worker cancelled!");
	});

	{
		// Guard will cancel the token when it goes out of scope
		let _guard = ShutdownGuard::new(token);

		println!("Doing some work inside a scope...");
		sleep(Duration::from_millis(500)).await;
		println!("Leaving scope (guard will drop)...");
	}

	// Give time for the worker to print
	sleep(Duration::from_millis(100)).await;
	println!("Main exited.");
}