[][src]Crate signal_hook_tokio

A crate for integrating signal handling with the Tokio runtime.

This provides the 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(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]
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(())
}
}

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.