Skip to main content

Module supervisor

Module supervisor 

Source
Available on crate feature actor only.
Expand description

Actor lifecycle notifications.

A supervisor is an ordinary actor that handles SupervisionEvent for a child type. Attach it while spawning the child:

use std::{convert::Infallible, marker::PhantomData};

use compio_actor::{Actor, Cluster, Handler, Mailbox, supervisor::SupervisionEvent};

struct Child;
struct Supervisor<C>(PhantomData<fn() -> C>);

impl<C> Supervisor<C> {
    fn new() -> Self {
        Self(PhantomData)
    }
}


impl<C: Actor> Handler<SupervisionEvent<C>> for Supervisor<C> {
    async fn handle(
        &self,
        _myself: &Mailbox<Self>,
        event: SupervisionEvent<C>,
        _state: &mut Self::State,
    ) -> Result<(), Self::Error> {
        match event {
            SupervisionEvent::ActorStarted(child) => {
                child.stop();
            }
            SupervisionEvent::ActorTerminated(_) => {}
            SupervisionEvent::ActorFailed(_) => {}
        }
        Ok(())
    }
}

let cluster = Cluster::new()?;
let (supervisor, _supervisor_handle) =
    cluster.spawn(Supervisor::<Child>::new, ()).await.unwrap();
let (_child, _child_handle) = cluster
    .spawn(|| Child, ())
    .with_supervisor(&supervisor)
    .await
    .unwrap();

Enumsยง

SupervisionEvent
A lifecycle event emitted by a supervised actor.