pub trait Component: Send + Sync {
// Required methods
fn name(&self) -> &str;
fn start<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = AppResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn stop<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = AppResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn health(&self) -> Health;
}Expand description
Lifecycle-managed infrastructure component.
Implement this trait for databases, caches, servers, message brokers, and other infrastructure that must participate in ordered startup, shutdown, and health reporting.
§Example
use rskit_component::{Component, Health, Registry};
use rskit_errors::AppResult;
use std::sync::Arc;
struct Cache;
#[async_trait::async_trait]
impl Component for Cache {
fn name(&self) -> &str {
"cache"
}
async fn start(&self) -> AppResult<()> {
Ok(())
}
async fn stop(&self) -> AppResult<()> {
Ok(())
}
fn health(&self) -> Health {
Health::healthy(self.name())
}
}
let mut registry = Registry::new();
registry.register(Arc::new(Cache));
registry.start_all().await?;
registry.stop_all().await?;Required Methods§
Sourcefn start<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = AppResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn start<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = AppResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Start the component.
Implementations must be cancel-safe: if the future is dropped because a
registry timeout expires, a subsequent Component::stop call must be
able to clean up any partially initialized resources.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".