Struct actix::Supervisor [] [src]

pub struct Supervisor<A: Supervised> where
    A: Actor<Context = Context<A>>, 
{ /* fields omitted */ }

Actor supervisor

Supervisor manages incomimng message for actor. In case of actor failure, supervisor creates new execution context and restarts actor lifecycle. Actor can be constructed lazily.

Supervisor has same livecycle as actor. In situation when all addresses to supervisor get dropped and actor does not execution anything supervisor terminates.

Supervisor can not garantee that actor successfully process incoming message. If actor fails during message processing, this message can not be recovered. Sender would receive Err(Cancelled) error in this situation.

Example

extern crate actix;

use actix::prelude::*;

// message
struct Die;

impl ResponseType for Die {
    type Item = ();
    type Error = ();
}

struct MyActor;

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

// To use actor with supervisor actor has to implement `Supervised` trait
impl Supervised for MyActor {
    fn restarting(&mut self, ctx: &mut Context<MyActor>) {
        println!("restarting");
    }
}

impl Handler<Die> for MyActor {

    fn handle(&mut self, _: Die, ctx: &mut Context<MyActor>) -> Response<Self, Die> {
        ctx.stop();
        Arbiter::system().send(msgs::SystemExit(0));
        Self::empty()
    }
}

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

    let (addr, _) = Supervisor::start(false, |_| MyActor);

    addr.send(Die);
    sys.run();
}

Methods

impl<A> Supervisor<A> where
    A: Supervised + Actor<Context = Context<A>>, 
[src]

[src]

Start new supervised actor. Depends on lazy argument actor could be started immidietly or on first incoming message.

[src]

Start new supervised actor in arbiter's thread. Depends on lazy argument actor could be started immidietly or on first incoming message.