Module actix::actors::signal

source ·
Expand description

An actor implementation of Unix signal handling

This module implements asynchronous signal handling for Actix. For each signal ProcessSignals actor sends Signal message to all subscriber. To subscriber, send Subscribe message to 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

Default signals handler. This actor sends SystemExit message to System actor for each of SIGINT, SIGTERM, SIGQUIT signals.
An actor implementation of Unix signal handling
Process signal message
Subscribe to process signals.

Enums

Different types of process signals