pub trait Actor<E: SystemEvent>: Send + Sync + 'static {
    fn supervision_strategy() -> SupervisionStrategy { ... }
    fn pre_start<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        _ctx: &'life1 mut ActorContext<E>
    ) -> Pin<Box<dyn Future<Output = Result<(), ActorError>> + Send + 'async_trait>>
    where
        Self: 'async_trait,
        'life0: 'async_trait,
        'life1: 'async_trait
, { ... } fn pre_restart<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 mut self,
        ctx: &'life1 mut ActorContext<E>,
        _error: Option<&'life2 ActorError>
    ) -> Pin<Box<dyn Future<Output = Result<(), ActorError>> + Send + 'async_trait>>
    where
        Self: 'async_trait,
        'life0: 'async_trait,
        'life1: 'async_trait,
        'life2: 'async_trait
, { ... } fn post_stop<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        _ctx: &'life1 mut ActorContext<E>
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
    where
        Self: 'async_trait,
        'life0: 'async_trait,
        'life1: 'async_trait
, { ... } }
Expand description

Basic trait for actors. Allows you to define tasks that should be run before actor startup, when an actor restarts, and tasks that should be run after the actor is stopped. It also allows you to define a supervisor strategy that should govern the actor when it fails to start up properly. For example:

use tiny_tokio_actor::*;
use std::time::Duration;

#[derive(Clone, Debug)]
struct TestEvent(String);

impl SystemEvent for TestEvent {}


struct MyActor {
    db: Option<Database>
}

#[async_trait]
impl Actor<TestEvent> for MyActor {

    // If it fails to start up, retry the actor 5 times, with a wait period
    // of 5 seconds before each retry
    fn supervision_strategy() -> SupervisionStrategy {
        let strategy = supervision::FixedIntervalStrategy::new(5, Duration::from_secs(5));
        SupervisionStrategy::Retry(Box::new(strategy))
    }

    // Initialize the database
    async fn pre_start(&mut self, _ctx: &mut ActorContext<TestEvent>) -> Result<(), ActorError> {
        let db = Database::init().map_err(ActorError::new)?;
        self.db = Some(db);
        Ok(())
    }

    // When restarting, log an error message and try starting again
    async fn pre_restart(&mut self, ctx: &mut ActorContext<TestEvent>, error: Option<&ActorError>) -> Result<(), ActorError> {
        log::error!("Actor '{}' is restarting due to {:#?}. Restarting...", ctx.path, error);
        self.pre_start(ctx).await
    }
}

Provided Methods§

Defines the supervision strategy to use for this actor. By default it is Stop which simply stops the actor if an error occurs at startup. You can also set this to SupervisionStrategy::Retry with a chosen supervision::RetryStrategy.

Override this function if you like to perform initialization of the actor

Override this function if you want to define what should happen when an error occurs in Actor::pre_start(). By default it simply calls pre_start() again, but you can also choose to reinitialize the actor in some other way.

Override this function if you like to perform work when the actor is stopped

Implementors§