use std::sync::Arc;
use anyhow::Result;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};
use uuid::Uuid;
#[async_trait]
pub trait EventHandler<E>: Send + Sync {
async fn handle(&self, event: E) -> Result<()>;
fn name(&self) -> &'static str;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IncomingEvent<T> {
pub event_id: Uuid,
pub source_module: String,
pub event_type: String,
pub payload: T,
pub published_at: DateTime<Utc>,
pub correlation_id: Option<Uuid>,
}
#[derive(Debug)]
pub enum HandlerResult {
Success,
Retry { delay_ms: u64 },
DeadLetter { reason: String },
}
#[async_trait]
pub trait RetryableEventHandler<E>: Send + Sync {
async fn handle(&self, event: E, attempt: u32) -> HandlerResult;
fn max_retries(&self) -> u32 { 3 }
fn base_delay_ms(&self) -> u64 { 1000 }
fn name(&self) -> &'static str;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExternalEntityCreatedEvent {
pub id: Uuid,
pub data: serde_json::Value,
}
pub struct ExternalEntityCreatedHandler {
}
impl ExternalEntityCreatedHandler {
pub fn new() -> Self {
Self {}
}
}
impl Default for ExternalEntityCreatedHandler {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl EventHandler<ExternalEntityCreatedEvent> for ExternalEntityCreatedHandler {
async fn handle(&self, event: ExternalEntityCreatedEvent) -> Result<()> {
tracing::info!("Handling ExternalEntityCreatedEvent: {:?}", event.id);
Ok(())
}
fn name(&self) -> &'static str {
"ExternalEntityCreatedHandler"
}
}