Struct Shutdown

Source
pub struct Shutdown { /* private fields */ }

Implementations§

Source§

impl Shutdown

Source

pub fn new() -> Result<Self, Error>

Create a new shutdown object with registered shutdown signals. In most cases the signal will be triggered when CTRL-C is pressed and the process receives a SIGINT or SIGTERM signal. If needed you can also call signal to send a shutdown signal programmatically.

§Examples
use shutdown::Shutdown;

let root = Shutdown::new().unwrap();
Source

pub fn unregistered() -> Self

Create a new shutdown object without registering shutdown signals. This can be useful if you want to create a shutdown object that is not listening for process signals but needs to be signalled by calling signal programmatically.

§Examples
use shutdown::Shutdown;

let root = Shutdown::unregistered();

// Do stuff...

// Manually signal a shutdown.
root.signal();
Source

pub fn register_signals(&mut self) -> Result<(), Error>

Register shutdown signals. This will listen for SIGINT and SIGTERM signals and trigger a shutdown signal when received. It is safe to call this method multiple times but it will only register the signals once.

Source

pub fn branch(&self) -> Self

Create a new branch (child) that can be signalled independent of the root (parent). When the root (or parent to be more precise) is signalled, the new branch (and any child branches) will also be signalled.

§Examples
use shutdown::Shutdown;

let root = Shutdown::new().unwrap();
let branch = root.branch();

// Signal a specific branch
branch.signal();
Source

pub fn subscribe(&self) -> Self

Create a new subscriber (sibling) that listens to an existing root (or previously created branch) for any shutdown signals.

§Examples
use shutdown::Shutdown;

let root = Shutdown::new().unwrap();
let subscriber = root.subscribe();
Source

pub fn is_signalled(&self) -> bool

Returns true if a shutdown signal has been received.

§Examples
use shutdown::Shutdown;

let root = Shutdown::new().unwrap();

while !root.is_signalled() {
    // Do stuff...
}
Source

pub fn signal(&self)

Manually signal the root or a branch. This causes all connected subscribers and any child branches to be signalled as well.

§Examples
use shutdown::Shutdown;

let root = Shutdown::new().unwrap();
let branch = root.branch();

// Trigger a signal from code
root.signal();
Source

pub async fn signalled(&self)

Block until a shutdown signal is received. This can, for example, be used in a select to block to wait for a long running task while still being able to respond to a shutdown signal.

§Examples
use shutdown::Shutdown;
use tokio::time::{sleep, Duration};

#[tokio::main]
async fn main() {
    let mut root = Shutdown::new().unwrap();

    tokio::select! {
        _ = root.signalled() => (),
        _ = sleep(Duration::from_secs(300)) => (), // Long runnnig task
    }
}

Trait Implementations§

Source§

impl Clone for Shutdown

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Shutdown

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Shutdown> for CancellationToken

Converts to a CancellationToken from a Shutdown. The returned token will be cancelled whenever the Shutdown would have been cancelled. Calling CancellationToken::cancel() on the returned token is equivalent to calling Shutdown::signal() on any subscribers of the Shutdown. In other words, the returned token behaves just like any other subscriber of the Shutdown.

Source§

fn from(value: Shutdown) -> Self

Converts to this type from the input type.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.