Skip to main content

Supervisor

Struct Supervisor 

Source
pub struct Supervisor;
Expand description

Actor supervisor

Supervisor gives the actor the ability to restart after failure. When the actor fails, recreate a new actor instance and replace it.

Implementations§

Source§

impl Supervisor

Source

pub async fn start<A, F>(f: F) -> Result<Addr<A>>
where A: Actor, F: Fn() -> A + Send + 'static,

Start a supervisor

§Examples
use xactor::*;
use std::time::Duration;

#[message]
struct Die;

#[message]
struct Add;

#[message(result = "i32")]
struct Get;

struct MyActor(i32);

impl Actor for MyActor {}

#[async_trait::async_trait]
impl Handler<Add> for MyActor {
    async fn handle(&mut self, ctx: &mut Context<Self>, _: Add) {
        self.0 += 1;
    }
}

#[async_trait::async_trait]
impl Handler<Get> for MyActor {
    async fn handle(&mut self, ctx: &mut Context<Self>, _: Get) -> i32 {
        self.0
    }
}

#[async_trait::async_trait]
impl Handler<Die> for MyActor {
    async fn handle(&mut self, ctx: &mut Context<Self>, _: Die) {
        ctx.stop(None);
    }
}

#[xactor::main]
async fn main() -> Result<()> {
    let mut addr = Supervisor::start(|| MyActor(0)).await?;

    addr.send(Add)?;
    assert_eq!(addr.call(Get).await?, 1);

    addr.send(Add)?;
    assert_eq!(addr.call(Get).await?, 2);

    addr.send(Die)?;
    sleep(Duration::from_secs(1)).await; // Wait for actor restart

    assert_eq!(addr.call(Get).await?, 0);
    Ok(())
}
Examples found in repository?
examples/supervisor_clear_send_later.rs (line 44)
43async fn main() -> Result<(), Box<dyn std::error::Error>> {
44    let service_supervisor = xactor::Supervisor::start(PingLater::default).await?;
45    let service_addr = service_supervisor.clone();
46
47    let supervisor_task = xactor::spawn(async {
48        service_supervisor.wait_for_stop().await;
49    });
50
51    let send_ping = async {
52        println!("  main  :: sending Ping");
53        service_addr.send(Ping("before halt")).unwrap();
54    };
55
56    let send_halt = async {
57        xactor::sleep(Duration::from_millis(1_000)).await;
58        println!("  main  :: sending Halt");
59        service_addr.send(Halt).unwrap();
60    };
61
62    let _ = futures::join!(supervisor_task, send_halt, send_ping);
63
64    Ok(())
65}
More examples
Hide additional examples
examples/supervisor_clear_interval.rs (line 69)
68async fn main() -> Result<(), Box<dyn std::error::Error>> {
69    let service_supervisor = xactor::Supervisor::start(PingTimer::default).await?;
70    let service_addr = service_supervisor.clone();
71
72    let supervisor_task = xactor::spawn(async {
73        service_supervisor.wait_for_stop().await;
74    });
75
76    let send_halt = async {
77        xactor::sleep(Duration::from_millis(5_200)).await;
78        println!("  main  :: sending Halt");
79        service_addr.send(Halt).unwrap();
80    };
81
82    let _ = futures::join!(supervisor_task, send_halt);
83    // run this to see that the interval is not properly stopped if the ctx is stopped
84    // futures::join!(supervisor_task, send_panic); // there is no panic recovery
85
86    Ok(())
87}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.