pub struct Shutdown { /* private fields */ }
Implementations§
Source§impl Shutdown
impl Shutdown
Sourcepub fn new() -> Result<Self, Error>
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();
Sourcepub fn unregistered() -> Self
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();
Sourcepub fn register_signals(&mut self) -> Result<(), Error>
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.
Sourcepub fn branch(&self) -> Self
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();
Sourcepub fn subscribe(&self) -> Self
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();
Sourcepub fn is_signalled(&self) -> bool
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...
}
Sourcepub fn signal(&self)
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();
Sourcepub async fn signalled(&self)
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 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
.
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
.