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

52 lines
1.6 KiB
Rust
Raw Normal View History

extern crate futures;
2018-09-02 11:32:50 +02:00
extern crate tokio;
extern crate tokio_signal;
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};
pub fn main() -> Result<(), Box<::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 \
2018-05-03 19:12:23 +02: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 \
2018-05-03 19:12:23 +02:00
or something",
2018-09-02 11:32:50 +02:00
the_signal
);
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 {
pub fn main() -> Result<(), Box<::std::error::Error>> {Ok(())}
2018-09-02 11:32:50 +02:00
}
fn main() -> Result<(), Box<std::error::Error>> {
2018-09-02 11:32:50 +02:00
platform::main()
}