Trait ractor::actor::Actor

source ·
pub trait Actor: Sized + Sync + Send + 'static {
    type Msg: Message;
    type State: State;
    type Arguments: State;

    // Required method
    fn pre_start<'life0, 'async_trait>(
        &'life0 self,
        myself: ActorRef<Self>,
        args: Self::Arguments
    ) -> Pin<Box<dyn Future<Output = Result<Self::State, ActorProcessingErr>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided methods
    fn post_start<'life0, 'life1, 'async_trait>(
        &'life0 self,
        myself: ActorRef<Self>,
        state: &'life1 mut Self::State
    ) -> Pin<Box<dyn Future<Output = Result<(), ActorProcessingErr>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn post_stop<'life0, 'life1, 'async_trait>(
        &'life0 self,
        myself: ActorRef<Self>,
        state: &'life1 mut Self::State
    ) -> Pin<Box<dyn Future<Output = Result<(), ActorProcessingErr>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn handle<'life0, 'life1, 'async_trait>(
        &'life0 self,
        myself: ActorRef<Self>,
        message: Self::Msg,
        state: &'life1 mut Self::State
    ) -> Pin<Box<dyn Future<Output = Result<(), ActorProcessingErr>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn handle_supervisor_evt<'life0, 'life1, 'async_trait>(
        &'life0 self,
        myself: ActorRef<Self>,
        message: SupervisionEvent,
        state: &'life1 mut Self::State
    ) -> Pin<Box<dyn Future<Output = Result<(), ActorProcessingErr>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn spawn<'async_trait>(
        name: Option<ActorName>,
        handler: Self,
        startup_args: Self::Arguments
    ) -> Pin<Box<dyn Future<Output = Result<(ActorRef<Self>, JoinHandle<()>), SpawnErr>> + Send + 'async_trait>>
       where Self: 'async_trait { ... }
    fn spawn_linked<'async_trait>(
        name: Option<ActorName>,
        handler: Self,
        startup_args: Self::Arguments,
        supervisor: ActorCell
    ) -> Pin<Box<dyn Future<Output = Result<(ActorRef<Self>, JoinHandle<()>), SpawnErr>> + Send + 'async_trait>>
       where Self: 'async_trait { ... }
}
Expand description

Actor defines the behavior of an Actor. It specifies the Message type, State type, and all processing logic for the actor

Additionally it aliases the calls for spawn and spawn_linked from ActorRuntime for convenient startup + lifecycle management

NOTE: All of the implemented trait functions

  • pre_start
  • post_start
  • post_stop
  • handle
  • handle_serialized (Available with cluster feature only)
  • handle_supervision_evt

return a Result<_, ActorProcessingError> where the error type is an alias of [Box<dyn std::error::Error + Send + Sync + ’static>]. This is treated as an “unhandled” error and will terminate the actor + execute necessary supervision patterns. Panics are also captured from the inner functions and wrapped into an Error type, however should an [Err(_)] result from any of these functions the actor will terminate and cleanup.

Required Associated Types§

source

type Msg: Message

The message type for this actor

source

type State: State

The type of state this actor manages internally

source

type Arguments: State

Initialization arguments

Required Methods§

source

fn pre_start<'life0, 'async_trait>( &'life0 self, myself: ActorRef<Self>, args: Self::Arguments ) -> Pin<Box<dyn Future<Output = Result<Self::State, ActorProcessingErr>> + Send + 'async_trait>>where Self: 'async_trait, 'life0: 'async_trait,

Invoked when an actor is being started by the system.

Any initialization inherent to the actor’s role should be performed here hence why it returns the initial state.

Panics in pre_start do not invoke the supervision strategy and the actor won’t be started. Actor::spawn will return an error to the caller

  • myself - A handle to the ActorCell representing this actor
  • args - Arguments that are passed in the spawning of the actor which might be necessary to construct the initial state

Returns an initial Actor::State to bootstrap the actor

Provided Methods§

source

fn post_start<'life0, 'life1, 'async_trait>( &'life0 self, myself: ActorRef<Self>, state: &'life1 mut Self::State ) -> Pin<Box<dyn Future<Output = Result<(), ActorProcessingErr>> + Send + 'async_trait>>where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Invoked after an actor has started.

Any post initialization can be performed here, such as writing to a log file, emitting metrics.

Panics in post_start follow the supervision strategy.

  • myself - A handle to the ActorCell representing this actor
  • state - A mutable reference to the internal actor’s state
source

fn post_stop<'life0, 'life1, 'async_trait>( &'life0 self, myself: ActorRef<Self>, state: &'life1 mut Self::State ) -> Pin<Box<dyn Future<Output = Result<(), ActorProcessingErr>> + Send + 'async_trait>>where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Invoked after an actor has been stopped to perform final cleanup. In the event the actor is terminated with Signal::Kill or has self-panicked, post_stop won’t be called.

Panics in post_stop follow the supervision strategy.

  • myself - A handle to the ActorCell representing this actor
  • state - A mutable reference to the internal actor’s last known state
source

fn handle<'life0, 'life1, 'async_trait>( &'life0 self, myself: ActorRef<Self>, message: Self::Msg, state: &'life1 mut Self::State ) -> Pin<Box<dyn Future<Output = Result<(), ActorProcessingErr>> + Send + 'async_trait>>where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Handle the incoming message from the event processing loop. Unhandled panickes will be captured and sent to the supervisor(s)

  • myself - A handle to the ActorCell representing this actor
  • message - The message to process
  • state - A mutable reference to the internal actor’s state
source

fn handle_supervisor_evt<'life0, 'life1, 'async_trait>( &'life0 self, myself: ActorRef<Self>, message: SupervisionEvent, state: &'life1 mut Self::State ) -> Pin<Box<dyn Future<Output = Result<(), ActorProcessingErr>> + Send + 'async_trait>>where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Handle the incoming supervision event. Unhandled panicks will captured and sent the the supervisor(s). The default supervision behavior is to exit the supervisor on any child exit. To override this behavior, implement this function.

  • myself - A handle to the ActorCell representing this actor
  • message - The message to process
  • state - A mutable reference to the internal actor’s state
source

fn spawn<'async_trait>( name: Option<ActorName>, handler: Self, startup_args: Self::Arguments ) -> Pin<Box<dyn Future<Output = Result<(ActorRef<Self>, JoinHandle<()>), SpawnErr>> + Send + 'async_trait>>where Self: 'async_trait,

Spawn an actor of this type, which is unsupervised, automatically starting

  • name: A name to give the actor. Useful for global referencing or debug printing
  • handler The implementation of Self
  • startup_args: Arguments passed to the pre_start call of the Actor to facilitate startup and initial state creation

Returns a [Ok((ActorRef, JoinHandle<()>))] upon successful start, denoting the actor reference along with the join handle which will complete when the actor terminates. Returns [Err(SpawnErr)] if the actor failed to start

source

fn spawn_linked<'async_trait>( name: Option<ActorName>, handler: Self, startup_args: Self::Arguments, supervisor: ActorCell ) -> Pin<Box<dyn Future<Output = Result<(ActorRef<Self>, JoinHandle<()>), SpawnErr>> + Send + 'async_trait>>where Self: 'async_trait,

Spawn an actor of this type with a supervisor, automatically starting the actor

  • name: A name to give the actor. Useful for global referencing or debug printing
  • handler The implementation of Self
  • startup_args: Arguments passed to the pre_start call of the Actor to facilitate startup and initial state creation
  • supervisor: The ActorCell which is to become the supervisor (parent) of this actor

Returns a [Ok((ActorRef, JoinHandle<()>))] upon successful start, denoting the actor reference along with the join handle which will complete when the actor terminates. Returns [Err(SpawnErr)] if the actor failed to start

Implementors§

source§

impl<TKey, TMsg, TWorker> Actor for Factory<TKey, TMsg, TWorker>where TKey: JobKey, TMsg: Message, TWorker: Actor<Msg = WorkerMessage<TKey, TMsg>, Arguments = WorkerStartContext<TKey, TMsg, TWorker>>,

§

type Msg = FactoryMessage<TKey, TMsg>

§

type State = FactoryState<TKey, TMsg, TWorker>

§

type Arguments = Box<dyn WorkerBuilder<TWorker> + 'static, Global>