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

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::prelude::*;
use actix::actors::signal;

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");
                Arbiter::system().do_send(actix::msgs::SystemExit(0));
            },
            signal::SignalType::Hup => {
                println!("SIGHUP received, reloading");
            },
            signal::SignalType::Term => {
                println!("SIGTERM received, stopping");
                Arbiter::system().do_send(actix::msgs::SystemExit(0));
            },
            signal::SignalType::Quit => {
                println!("SIGQUIT received, exiting");
                Arbiter::system().do_send(actix::msgs::SystemExit(0));
            }
            _ => (),
        }
    }
}

fn main() {
   // initialize system
   let sys = System::new("test");

   // Start signals handler
   let addr: Addr<Syn, _> = Signals.start();

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

   // Run system, this function blocks until system runs
   let code = sys.run();
   std::process::exit(code);
}

Structs

DefaultSignalsHandler

Default signals handler. This actor sends SystemExit message to System actor for each of SIGINT, SIGTERM, SIGQUIT signals.

ProcessSignals

An actor implementation of Unix signal handling

Signal

Process signal message

Subscribe

Subscribe to process signals.

Enums

SignalType

Different types of process signals