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 = 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§

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 = 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.

Source

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

Implementors§

Source§

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