Crates.io | tari_shutdown |
lib.rs | tari_shutdown |
version | 1.0.0-rc.5 |
source | src |
created_at | 2020-01-12 07:21:26.132564 |
updated_at | 2024-02-07 10:40:41.616632 |
description | A convenient shutdown signal |
homepage | https://tari.com |
repository | https://github.com/tari-project/tari |
max_upload_size | |
id | 197743 |
size | 13,407 |
ShutdownSignal
is a convenient wrapper around a one-shot channel that allows different threads to let each other know
that they should stop working.
First, create the shutdown signal.
let mut shutdown = Shutdown::new();
Use to_signal
to create a future which will resolve when Shutdown
is triggered.
let signal = shutdown.to_signal();
assert_eq!(shutdown.is_triggered(), false);
You can clone the signal and move it into threads that need to be informed of when to shut down. We're using tokio here, but this will work in any futures-based runtime:
tokio::spawn(async move {
signal.await.unwrap();
println!("Finished");
});
Then when you want to trigger the shutdown signal, call trigger
. All signals will resolve.
shutdown.trigger().unwrap(); // "Finished" is printed
// Shutdown::trigger is idempotent
shutdown.trigger().unwrap();
assert_eq!(shutdown.is_triggered(), true);
Note: If the ShutdownSignal instance is dropped, it will trigger the signal, so the Shutdown
instance should be held
as long as required by the application.