[][src]Module actix::actors::signal

An actor implementation of Unix signal handling.

This module implements asynchronous signal handling for Actix. For each signal, the ProcessSignals actor sends a Signal message to all subscribers. To subscribe, send a Subscribe message to the ProcessSignals actor.

Examples

use actix::actors::signal;
use actix::prelude::*;

struct Signals;

impl Actor for Signals {
    type Context = Context<Self>;
}

// Shutdown system on and of `SIGINT`, `SIGTERM`, `SIGQUIT` signals
impl Handler<signal::Signal> for Signals {
    type Result = ();

    fn handle(&mut self, msg: signal::Signal, _: &mut Context<Self>) {
        match msg.0 {
            signal::SignalType::Int => {
                println!("SIGINT received, exiting");
                System::current().stop();
            }
            signal::SignalType::Hup => {
                println!("SIGHUP received, reloading");
            }
            signal::SignalType::Term => {
                println!("SIGTERM received, stopping");
                System::current().stop();
            }
            signal::SignalType::Quit => {
                println!("SIGQUIT received, exiting");
                System::current().stop();
            }
            _ => (),
        }
    }
}

fn main() {
    // initialize system
    System::run(|| {
        // Start signals handler
        let addr = Signals.start();

        // send SIGTERM
        std::thread::spawn(move || {
            // emulate SIGNTERM
            addr.do_send(signal::Signal(signal::SignalType::Term));
        });
    });

    std::process::exit(0);
}

Structs

DefaultSignalsHandler

Default signals handler.

ProcessSignals

An actor that handles Unix signals.

Signal

A message representing a received signal.

Subscribe

Subscribe to process signals.

Enums

SignalType

Represents the different types of signals a process can receive.