Skip to main content

Worker

Trait Worker 

Source
pub trait Worker: Debug + MaybeSend {
    // Required methods
    fn run<'life0, 'async_trait>(
        &'life0 mut self,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn trigger<'life0, 'async_trait>(
        &'life0 mut self,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided methods
    fn initial_trigger<'life0, 'async_trait>(
        &'life0 mut self,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: Send + 'async_trait,
             'life0: 'async_trait { ... }
    fn reset(&mut self) { ... }
    fn shutdown<'life0, 'async_trait>(
        &'life0 mut self,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: Send + 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

A background worker meant to be spawned on a SharedRuntime.

§Lifecycle

The worker’s run method is executed every time trigger returns. On startup initial_trigger is called before the first run.

§Cancellation safety

The trigger function can be interrupted at any yield point (.awaited call). The state of the worker at this point will be saved and used to restart the worker. To be able to safely restart, the worker must be in a valid state on every call to .await within the trigger function. See tokio::select for more details.

Required Methods§

Source

fn run<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Main worker function

Code in this function must always use timeout on long-running await calls to avoid blocking forks if an await call takes too long to complete.

Source

fn trigger<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Function called between each run to wait for the next run. This function should be cancellation safe as it can be cancelled at any yield point.

Provided Methods§

Source

fn initial_trigger<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: Send + 'async_trait, 'life0: 'async_trait,

Alternative trigger called on start to provide custom behavior. Defaults to trigger behavior.

Source

fn reset(&mut self)

Reset the worker state. Called in the child after a fork to cleanup parent state.

Source

fn shutdown<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: Send + 'async_trait, 'life0: 'async_trait,

Hook called when the app is shutting down. Can be used to flush remaining data.

Trait Implementations§

Source§

impl Worker for Box<dyn Worker + Sync>

Source§

fn run<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Main worker function Read more
Source§

fn trigger<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Function called between each run to wait for the next run. This function should be cancellation safe as it can be cancelled at any yield point.
Source§

fn initial_trigger<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Alternative trigger called on start to provide custom behavior. Defaults to trigger behavior.
Source§

fn reset(&mut self)

Reset the worker state. Called in the child after a fork to cleanup parent state.
Source§

fn shutdown<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Hook called when the app is shutting down. Can be used to flush remaining data.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Worker for Box<dyn Worker + Sync>

Source§

fn run<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn trigger<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn initial_trigger<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn reset(&mut self)

Source§

fn shutdown<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Implementors§