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
Create a new ShutdownController
.
§Examples
let shutdown = shutdown_async::ShutdownController::new();
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)
pub async fn shutdown(self)
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
Auto Trait Implementations§
impl Freeze for ShutdownController
impl RefUnwindSafe for ShutdownController
impl Send for ShutdownController
impl Sync for ShutdownController
impl Unpin for ShutdownController
impl UnwindSafe for ShutdownController
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more