#signal #unix #async-std

signal-hook-async-std

async-std support for signal-hook

3 unstable releases

0.2.1 Jan 31, 2021
0.2.0 Dec 21, 2020
0.1.0 Nov 22, 2020

#9 in #async-std

Download history 114/week @ 2021-04-05 89/week @ 2021-04-12 90/week @ 2021-04-19 64/week @ 2021-04-26 51/week @ 2021-05-03 82/week @ 2021-05-10 137/week @ 2021-05-17 115/week @ 2021-05-24 234/week @ 2021-05-31 170/week @ 2021-06-07 76/week @ 2021-06-14 71/week @ 2021-06-21 57/week @ 2021-06-28 257/week @ 2021-07-05 238/week @ 2021-07-12 104/week @ 2021-07-19

329 downloads per month
Used in 6 crates (5 directly)

Apache-2.0/MIT

120KB
1.5K SLoC

Signal-hook-async-std

Travis Build Status

This is a async-std 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 module for integrating signal handling with the async-std runtime.

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

Example

use std::io::Error;

use async_std::prelude::*;

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

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!(),
        }
    }
}

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

    let signals_task = async_std::task::spawn(handle_signals(signals));

    // Execute your main program logic

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

    Ok(())
}

Dependencies

~1MB
~15K SLoC