4 releases (2 breaking)

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

#147 in Asynchronous

Download history 31624/week @ 2023-12-13 28471/week @ 2023-12-20 15252/week @ 2023-12-27 28322/week @ 2024-01-03 28747/week @ 2024-01-10 32223/week @ 2024-01-17 35056/week @ 2024-01-24 33113/week @ 2024-01-31 35778/week @ 2024-02-07 34424/week @ 2024-02-14 31485/week @ 2024-02-21 33122/week @ 2024-02-28 36112/week @ 2024-03-06 39465/week @ 2024-03-13 34517/week @ 2024-03-20 39279/week @ 2024-03-27

155,121 downloads per month
Used in 55 crates (39 directly)

Apache-2.0/MIT

125KB
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

use std::io::Error;

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

use futures::stream::StreamExt;

async fn handle_signals(mut signals: Signals) {
    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]
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(())
}

Dependencies

~2–12MB
~100K SLoC