Skip to main content

Component

Trait Component 

Source
pub trait Component: Send + Sync {
    // Required methods
    fn name(&self) -> &'static str;
    fn start<'life0, 'async_trait>(
        &'life0 mut self,
        handle: DaemonHandle,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn handle_event<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        event: &'life1 DaemonEvent,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn stop<'life0, 'async_trait>(
        &'life0 mut self,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

A daemon component that handles a specific domain.

Components are the building blocks of the daemon. Each component:

  • Has a unique name for logging and debugging
  • Can optionally expose gRPC services
  • Receives a DaemonHandle on startup for accessing daemon resources
  • Handles events from the event bus
  • Performs cleanup on shutdown

§Lifecycle

  1. Construction: Component is created (usually via new())
  2. Start: start() is called with a DaemonHandle
  3. Running: handle_event() is called for each event on the bus
  4. Shutdown: stop() is called for cleanup

§Example

pub struct MyComponent {
    handle: Option<DaemonHandle>,
}

#[async_trait]
impl Component for MyComponent {
    fn name(&self) -> &'static str { "my-component" }

    async fn start(&mut self, handle: DaemonHandle) -> Result<()> {
        self.handle = Some(handle);
        Ok(())
    }

    async fn handle_event(&mut self, event: &DaemonEvent) -> Result<()> {
        match event {
            DaemonEvent::SomeEvent => {
                // Handle the event
                if let Some(handle) = &self.handle {
                    handle.emit(DaemonEvent::ResponseEvent);
                }
            }
            _ => {}
        }
        Ok(())
    }

    async fn stop(&mut self) -> Result<()> {
        Ok(())
    }
}

Required Methods§

Source

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

Human-readable name for logging and debugging.

Source

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

Called once at startup.

Store the handle if you need to emit events or access daemon resources later. The handle is cheaply cloneable, so feel free to clone it for spawned tasks.

Source

fn handle_event<'life0, 'life1, 'async_trait>( &'life0 mut self, event: &'life1 DaemonEvent, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Handle an incoming event.

Called for every event on the bus. To emit new events in response, use the handle stored during start(). Events emitted here will be processed in subsequent event loop iterations.

Source

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

Called on graceful shutdown.

Use this to clean up resources, abort spawned tasks, etc.

Implementors§