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
DaemonHandleon startup for accessing daemon resources - Handles events from the event bus
- Performs cleanup on shutdown
§Lifecycle
- Construction: Component is created (usually via
new()) - Start:
start()is called with aDaemonHandle - Running:
handle_event()is called for each event on the bus - 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§
Sourcefn 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 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.
Sourcefn 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 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.