Struct 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

Source

pub fn new() -> ShutdownController

Create a new ShutdownController.

§Examples
let shutdown = shutdown_async::ShutdownController::new();
Source

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();
Source

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

Source§

fn default() -> ShutdownController

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V