Skip to main content

Component

Trait Component 

Source
pub trait Component: Send + Sync {
    // Required methods
    fn name(&self) -> &str;
    fn start<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             Self: 'async_trait;
    fn stop<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             Self: '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§

Source

fn name(&self) -> &str

Stable identifier used in logs and health responses.

Source

fn start<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: '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.

Source

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

Stop the component gracefully.

Implementations must be cancel-safe and idempotent.

Source

fn health(&self) -> Health

Return an instantaneous health snapshot.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementations on Foreign Types§

Source§

impl Component for Agent

Source§

fn name(&self) -> &'static str

Source§

fn start<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Agent: 'async_trait,

Source§

fn stop<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Agent: 'async_trait,

Source§

fn health(&self) -> Health

Source§

impl Component for Echo

Source§

fn name(&self) -> &str

Source§

fn start<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Echo: 'async_trait,

Source§

fn stop<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Echo: 'async_trait,

Source§

fn health(&self) -> Health

Source§

impl Component for GrpcServer

Source§

fn name(&self) -> &str

Source§

fn start<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
where 'life0: 'async_trait, GrpcServer: 'async_trait,

Source§

fn stop<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
where 'life0: 'async_trait, GrpcServer: 'async_trait,

Source§

fn health(&self) -> Health

Source§

impl Component for HttpServer

Source§

fn name(&self) -> &str

Source§

fn start<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
where 'life0: 'async_trait, HttpServer: 'async_trait,

Source§

fn stop<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
where 'life0: 'async_trait, HttpServer: 'async_trait,

Source§

fn health(&self) -> Health

Source§

impl Component for InMemoryDatabase

Source§

fn name(&self) -> &str

Source§

fn start<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
where 'life0: 'async_trait, InMemoryDatabase: 'async_trait,

Source§

fn stop<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
where 'life0: 'async_trait, InMemoryDatabase: 'async_trait,

Source§

fn health(&self) -> Health

Source§

impl Component for InMemoryProvider

Source§

fn name(&self) -> &'static str

Source§

fn start<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
where 'life0: 'async_trait, InMemoryProvider: 'async_trait,

Source§

fn stop<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
where 'life0: 'async_trait, InMemoryProvider: 'async_trait,

Source§

fn health(&self) -> Health

Source§

impl Component for Registry

Source§

fn name(&self) -> &'static str

Source§

fn start<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Registry: 'async_trait,

Source§

fn stop<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Registry: 'async_trait,

Source§

fn health(&self) -> Health

Source§

impl Component for WorkloadComponent

Source§

fn name(&self) -> &str

Source§

fn start<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
where 'life0: 'async_trait, WorkloadComponent: 'async_trait,

Source§

fn stop<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + 'async_trait>>
where 'life0: 'async_trait, WorkloadComponent: 'async_trait,

Source§

fn health(&self) -> Health

Implementors§

Source§

impl<F> Component for LazyComponent<F>
where F: Fn() -> Arc<dyn Component> + Send + Sync,