Skip to main content

Handler

Trait Handler 

Source
pub trait Handler<E: Event>:
    Send
    + Sync
    + 'static
    + Send {
    type Error: Into<OutboxError> + Send + Sync + 'static;

    // Required method
    fn handle(
        &self,
        event: E,
        ctx: &HandlerContext,
    ) -> impl Future<Output = Result<(), Self::Error>> + Send;
}
Expand description

Asynchronous handler dispatched by the outbox worker for each event of type E.

Implementors describe the side effect to perform when an event lands: write to an audit log, publish to a broker, send a notification, etc. The handler does not return a business value: success is the side effect itself.

§Idempotency

The outbox guarantees at-least-once delivery. Handlers MUST therefore be idempotent: the same event can be delivered more than once if a previous attempt crashed between the side effect and the database commit that marks the row as delivered.

§Example

use hexeract_core::HandlerContext;
use hexeract_outbox::{Event, Handler, OutboxError};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct UserRegistered {
    user_id: uuid::Uuid,
}

impl Event for UserRegistered {
    const EVENT_TYPE: &'static str = "users.registered";
}

struct AuditWriter;

impl Handler<UserRegistered> for AuditWriter {
    type Error = OutboxError;

    async fn handle(
        &self,
        event: UserRegistered,
        _ctx: &HandlerContext,
    ) -> Result<(), Self::Error> {
        let _ = event.user_id;
        Ok(())
    }
}

Required Associated Types§

Source

type Error: Into<OutboxError> + Send + Sync + 'static

Handler-defined error type, convertible into OutboxError.

Required Methods§

Source

fn handle( &self, event: E, ctx: &HandlerContext, ) -> impl Future<Output = Result<(), Self::Error>> + Send

Process the event and produce its side effect.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§