pub trait Service<S>:
Send
+ Sync
+ ServiceAsAny<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§
Sourcefn enabled(&self, state: &S) -> bool
fn enabled(&self, state: &S) -> bool
Whether the service is enabled. If the service is not enabled, it will not be run.
Sourcefn 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,
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
CancellationTokento use as a signal to gracefully shut down the service.
Provided Methods§
Sourcefn 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,
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".