Skip to main content

Service

Trait Service 

Source
pub trait Service<S>:
    Send
    + Sync
    + ServiceAsAny<S>
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,
{ // Required methods fn name(&self) -> String; fn enabled(&self, state: &S) -> bool; fn run<'life0, 'async_trait>( self: Box<Self>, state: &'life0 S, cancel_token: CancellationToken, ) -> Pin<Box<dyn Future<Output = RoadsterResult<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; // Provided method fn before_run<'life0, 'life1, 'async_trait>( &'life0 self, _state: &'life1 S, ) -> Pin<Box<dyn Future<Output = RoadsterResult<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } }
Expand description

Trait to represent a service (e.g., a persistent task) to run in the app.

§Examples

Required Methods§

Source

fn name(&self) -> String

The name of the service.

Source

fn enabled(&self, state: &S) -> bool

Whether the service is enabled. If the service is not enabled, it will not be run.

Source

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

Run the service in a new tokio task.

  • cancel_token - A tokio CancellationToken to use as a signal to gracefully shut down the service.

Provided Methods§

Source

fn before_run<'life0, 'life1, 'async_trait>( &'life0 self, _state: &'life1 S, ) -> Pin<Box<dyn Future<Output = RoadsterResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Perform any initialization work or other checks that should be done before the service runs.

For example, checking that the service is healthy, removing stale items from the service’s queue, etc.

Note that this is run for every service that’s registered in the crate::service::registry::ServiceRegistry regardless of whether it’s enabled or not.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§

Source§

impl<S, F, Fut> Service<S> for FunctionService<S, F, Fut>
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>, F: 'static + Send + Sync + Fn(S, CancellationToken) -> Fut, Fut: 'static + Send + Future<Output = RoadsterResult<()>>,

Source§

impl<S> Service<S> for GrpcService
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,

Source§

impl<S> Service<S> for HttpService
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,

Source§

impl<S> Service<S> for PgWorkerService<S>
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,

Source§

impl<S> Service<S> for SidekiqWorkerService<S>
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,