Function spirit::utils::support_emergency_shutdown [−][src]
pub fn support_emergency_shutdown() -> Result<Arc<AtomicBool>, AnyError>
Expand description
Installs a stage-shutdown handling.
If CTRL+C (or some other similar signal) is received for the first time, a graceful shutdown is initiated and a flag is set to true. If it is received for a second time, the application is terminated abruptly.
The flag handle is returned to the caller, so the graceful shutdown and second stage kill can be aborted.
Note that this API doesn’t allow for removing the staged shutdown (due to the needed API
clutter). If that is needed, you can use signal_hook
directly.
Usage
This is supposed to be called early in the program (usually as the first thing in main
). This
is for two reasons:
- One usually wants this kind of emergency handling even during startup ‒ if something gets stuck during the initialization.
- Installing signal handlers once there are multiple threads is inherently racy, therefore it is better to be done before any additional threads are started.
Examples
use spirit::prelude::*;
use spirit::{utils, Empty, Spirit};
fn main() {
// Do this first, so double CTRL+C works from the very beginning.
utils::support_emergency_shutdown().expect("This doesn't fail on healthy systems");
// Proceed to doing something useful.
Spirit::<Empty, Empty>::new()
.run(|_spirit| {
println!("Hello world");
Ok(())
});
}
Errors
This manipulates low-level signal handlers internally, so in theory this can fail. But this is not expected to fail in practice (not on a system that isn’t severely broken anyway). As such, it is probably reasonable to unwrap here.