Skip to main content

bus_core/
handler.rs

1use crate::{error::HandlerError, event::Event, id::MessageId};
2use async_trait::async_trait;
3use tracing::Span;
4
5/// Context passed to every handler invocation.
6#[derive(Debug)]
7pub struct HandlerCtx {
8    /// The message ID associated with this delivery.
9    pub msg_id: MessageId,
10
11    /// Stream sequence number for this message.
12    pub stream_seq: u64,
13
14    /// Number of times this message has been delivered.
15    pub delivered: u64,
16
17    /// Subject this message was received on.
18    pub subject: String,
19
20    /// Active tracing span for attaching handler work.
21    pub span: Span,
22}
23
24/// Processes a strongly typed event received from the event bus.
25#[async_trait]
26pub trait EventHandler<E: Event>: Send + Sync + 'static {
27    async fn handle(&self, ctx: HandlerCtx, event: E) -> Result<(), HandlerError>;
28}