[][src]Crate signal_hook_async_std

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;

use futures::stream::StreamExt;

use signal_hook;
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 {
            signal_hook::SIGHUP => {
                // Reload configuration
                // Reopen the log file
            }
            signal_hook::SIGTERM | signal_hook::SIGINT | signal_hook::SIGQUIT => {
                // Shutdown the system;
            },
            _ => unreachable!(),
        }
    }
}

#[async_std::main]
async fn main() -> Result<(), Error> {
    let signals = Signals::new(&[
        signal_hook::SIGHUP,
        signal_hook::SIGTERM,
        signal_hook::SIGINT,
        signal_hook::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(())
}

Structs

Handle

A struct to control an instance of an associated type (like for example Signals).

SignalsInfo

An asynchronous Stream of arriving signals.

Type Definitions

Signals

Simplified version of the signals stream.