Files
tokio/tokio-signal/examples/sighup-example.rs
T

52 lines
1.6 KiB
Rust
Raw Normal View History

2019-05-14 10:27:36 -07:00
#![deny(warnings, rust_2018_idioms)]
2018-09-02 11:32:50 +02:00
// A trick to not fail build on non-unix platforms when using unix-specific features.
#[cfg(unix)]
mod platform {
2018-09-02 11:32:50 +02:00
use futures::{Future, Stream};
use tokio_signal::unix::{Signal, SIGHUP};
2019-05-14 10:27:36 -07:00
pub fn main() -> Result<(), Box<dyn (::std::error::Error)>> {
2018-09-02 11:32:50 +02:00
// on Unix, we can listen to whatever signal we want, in this case: SIGHUP
let stream = Signal::new(SIGHUP).flatten_stream();
2018-09-02 11:32:50 +02:00
println!("Waiting for SIGHUPS (Ctrl+C to quit)");
println!(
" TIP: use `pkill -sighup sighup-example` from a second terminal \
2019-02-21 11:56:15 -08:00
to send a SIGHUP to all processes named 'sighup-example' \
(i.e. this binary)"
2018-09-02 11:32:50 +02:00
);
2018-09-02 11:32:50 +02:00
// for_each is a powerful primitive provided by the Futures crate
// it turns a Stream into a Future that completes after all stream-items
// have been completed.
let future = stream.for_each(|the_signal| {
println!(
"*Got signal {:#x}* I should probably reload my config \
2019-02-21 11:56:15 -08:00
or something",
the_signal
2018-09-02 11:32:50 +02:00
);
Ok(())
});
// Up until now, we haven't really DONE anything, just prepared
// now it's time to actually schedule, and thus execute, the stream
// on our event loop, and loop forever
::tokio::runtime::current_thread::block_on_all(future)?;
Ok(())
2018-09-02 11:32:50 +02:00
}
2018-09-02 11:32:50 +02:00
}
#[cfg(not(unix))]
mod platform {
2019-05-14 10:27:36 -07:00
pub fn main() -> Result<(), Box<dyn ::std::error::Error>> {
2019-02-21 11:56:15 -08:00
Ok(())
}
2018-09-02 11:32:50 +02:00
}
2019-05-14 10:27:36 -07:00
fn main() -> Result<(), Box<dyn std::error::Error>> {
2018-09-02 11:32:50 +02:00
platform::main()
}