Skip to main content

Worker

Trait Worker 

Source
pub trait Worker:
    Send
    + Sync
    + 'static {
    type Key: JobKey;
    type Message: Message;
    type Arguments: Message;
    type State: State;

    // Required method
    fn pre_start<'life0, 'life1, 'async_trait>(
        &'life0 self,
        wid: usize,
        factory: &'life1 ActorRef<FactoryMessage<Self::Key, Self::Message>>,
        args: Self::Arguments,
    ) -> Pin<Box<dyn Future<Output = Result<Self::State, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;

    // Provided methods
    fn post_start<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        wid: usize,
        factory: &'life1 ActorRef<FactoryMessage<Self::Key, Self::Message>>,
        state: &'life2 mut Self::State,
    ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             Self: 'async_trait { ... }
    fn post_stop<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        wid: usize,
        factory: &'life1 ActorRef<FactoryMessage<Self::Key, Self::Message>>,
        state: &'life2 mut Self::State,
    ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             Self: 'async_trait { ... }
    fn handle<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        wid: usize,
        factory: &'life1 ActorRef<FactoryMessage<Self::Key, Self::Message>>,
        job: Job<Self::Key, Self::Message>,
        state: &'life2 mut Self::State,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Key, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             Self: 'async_trait { ... }
    fn handle_supervisor_evt<'life0, 'life1, 'async_trait>(
        &'life0 self,
        myself: ActorCell,
        message: SupervisionEvent,
        state: &'life1 mut Self::State,
    ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait { ... }
}
Expand description

A factory worker trait, which is a basic wrapper around actor logic, with predefined type information specific to workers

IMPORTANT: Workers are actors at their core principal, but with somewhat customized logic. This logic assists in tracking worker health, processing messages in a load-balanced manner, and managing necessary start automatically without copying the code repeatedly.

This trait implements as much of the custom wrapping logic as possible without breaking the factory <-> worker API requirement. If you so wish you can fully specify the actor properties instead of using this assistance trait.

Required Associated Types§

Source

type Key: JobKey

The worker’s job-key type

Source

type Message: Message

The worker’s message type

Source

type Arguments: Message

The optional startup arguments for the worker (use () to ignore)

Source

type State: State

The worker’s internal state

Required Methods§

Source

fn pre_start<'life0, 'life1, 'async_trait>( &'life0 self, wid: usize, factory: &'life1 ActorRef<FactoryMessage<Self::Key, Self::Message>>, args: Self::Arguments, ) -> Pin<Box<dyn Future<Output = Result<Self::State, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Invoked when a worker 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. The spawn will return an error to the caller

  • wid - The id of this worker in the factory
  • factory - The handle to the factory that owns and manages this worker
  • args - Arguments that are passed in the spawning of the worker which are necessary to construct the initial state

Returns an initial Worker::State to bootstrap the actor

Provided Methods§

Source

fn post_start<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, wid: usize, factory: &'life1 ActorRef<FactoryMessage<Self::Key, Self::Message>>, state: &'life2 mut Self::State, ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, Self: 'async_trait,

Invoked after an actor has started.

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

  • wid - The id of this worker in the factory
  • factory - The handle to the factory that owns and manages this worker
  • state - The worker’s internal state, which is mutable and owned by the worker

Panics in post_start follow the supervision strategy.

Source

fn post_stop<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, wid: usize, factory: &'life1 ActorRef<FactoryMessage<Self::Key, Self::Message>>, state: &'life2 mut Self::State, ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, Self: 'async_trait,

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

  • wid - The id of this worker in the factory
  • factory - The handle to the factory that owns and manages this worker
  • state - The worker’s internal state, which is mutable and owned by the worker

Panics in post_stop follow the supervision strategy.

Source

fn handle<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, wid: usize, factory: &'life1 ActorRef<FactoryMessage<Self::Key, Self::Message>>, job: Job<Self::Key, Self::Message>, state: &'life2 mut Self::State, ) -> Pin<Box<dyn Future<Output = Result<Self::Key, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, Self: 'async_trait,

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

  • wid - The id of this worker in the factory
  • factory - The handle to the factory that owns and manages this worker
  • job - The Job which this worker should process
  • state - The worker’s internal state, which is mutable and owned by the worker

Returns the Job::key upon success or the error on failure

Source

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

Handle the incoming supervision event. Unhandled panics will be 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 reference to this actor’s ActorCell
  • message - The message to process
  • state - A mutable reference to the internal actor’s state

Implementors§