Struct tokio_utils::ShutdownController
source · pub struct ShutdownController { /* private fields */ }Expand description
A ShutdownController is used to control the shutdown of an application.
This is accomplished by creating a ShutdownMonitor instance for each task
that should be monitored. When ShutdownController::shutdown is called,
all ShutdownMonitor instances will be notified that shutdown has started.
Examples
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);Implementations§
source§impl ShutdownController
impl ShutdownController
sourcepub fn new() -> ShutdownController
pub fn new() -> ShutdownController
sourcepub fn subscribe(&self) -> ShutdownMonitor
pub fn subscribe(&self) -> ShutdownMonitor
Create a new ShutdownMonitor instance that can listen for the shutdown signal.
Examples
let shutdown = shutdown_async::ShutdownController::new();
let monitor = shutdown.subscribe();sourcepub async fn shutdown(self) -> impl Future<Output = ()>
pub async fn shutdown(self) -> impl Future<Output = ()>
Begin shutting down and wait for all ShutdownMonitor instances to be dropped.
Examples
#[tokio::main]
async fn main() {
let shutdown = shutdown_async::ShutdownController::new();
// ... do stuff ...
// Tell all tasks to shutdown
shutdown.shutdown().await;
}Trait Implementations§
source§impl Default for ShutdownController
impl Default for ShutdownController
source§fn default() -> ShutdownController
fn default() -> ShutdownController
Returns the “default value” for a type. Read more