3 releases (breaking)

0.3.0 Jan 26, 2021
0.2.0 Dec 21, 2020
0.1.0 Nov 22, 2020

#12 in #signal

Download history 198/week @ 2021-04-05 334/week @ 2021-04-12 164/week @ 2021-04-19 269/week @ 2021-04-26 345/week @ 2021-05-03 1949/week @ 2021-05-10 1569/week @ 2021-05-17 1689/week @ 2021-05-24 1794/week @ 2021-05-31 1362/week @ 2021-06-07 1547/week @ 2021-06-14 1586/week @ 2021-06-21 986/week @ 2021-06-28 1812/week @ 2021-07-05 1787/week @ 2021-07-12 1914/week @ 2021-07-19

1,438 downloads per month
Used in 4 crates (3 directly)

Apache-2.0/MIT

120KB
1.5K SLoC

Signal-hook-tokio

Travis Build Status

This is a tokio adapter crate for the signal-hook crate. See the documentation for further details.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

A crate for integrating signal handling with the Tokio runtime.

This provides the [Signals][Signals] struct which acts as a [Stream] of signals.

Note that the futures-v0_3 feature of this crate must be enabled for Signals to implement the Stream trait.

Example

# #[cfg(feature = "futures-v0_3")]
# mod test {
use std::io::Error;

use signal_hook::consts::signal::*;
use signal_hook_tokio::Signals;

use futures::stream::StreamExt;

async fn handle_signals(signals: Signals) {
    let mut signals = signals.fuse();
    while let Some(signal) = signals.next().await {
        match signal {
            SIGHUP => {
                // Reload configuration
                // Reopen the log file
            }
            SIGTERM | SIGINT | SIGQUIT => {
                // Shutdown the system;
            },
            _ => unreachable!(),
        }
    }
}

#[tokio::main]
# pub
async fn main() -> Result<(), Error> {
    let signals = Signals::new(&[
        SIGHUP,
        SIGTERM,
        SIGINT,
        SIGQUIT,
    ])?;
    let handle = signals.handle();

    let signals_task = tokio::spawn(handle_signals(signals));

    // Execute your main program logic

    // Terminate the signal stream.
    handle.close();
    signals_task.await?;

    Ok(())
}
}
# fn main() -> Result<(), std::io::Error> {
#    #[cfg(feature = "futures-v0_3")]
#    test::main()?;
#    Ok(())
# }

Dependencies

~2.5MB
~39K SLoC