backbone-bucket 0.2.0

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
//! Event handlers for Bucket module
//!
//! Generated by backbone-schema. Do not edit manually.
//!
//! Implement these traits to handle events from other modules.

use std::sync::Arc;
use anyhow::Result;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};
use uuid::Uuid;

// ============================================================================
// EVENT HANDLER TRAIT
// ============================================================================

/// Trait for handling domain events
#[async_trait]
pub trait EventHandler<E>: Send + Sync {
    /// Handle the event
    async fn handle(&self, event: E) -> Result<()>;

    /// Get the handler name for logging
    fn name(&self) -> &'static str;
}

/// Envelope for incoming events from event bus
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IncomingEvent<T> {
    /// Unique event ID
    pub event_id: Uuid,
    /// Source module that published the event
    pub source_module: String,
    /// Event type name
    pub event_type: String,
    /// The actual event payload
    pub payload: T,
    /// When the event was published
    pub published_at: DateTime<Utc>,
    /// Correlation ID for tracing
    pub correlation_id: Option<Uuid>,
}

/// Result of event handling
#[derive(Debug)]
pub enum HandlerResult {
    /// Event handled successfully
    Success,
    /// Event should be retried
    Retry { delay_ms: u64 },
    /// Event should go to dead letter queue
    DeadLetter { reason: String },
}

/// Event handler with retry logic
#[async_trait]
pub trait RetryableEventHandler<E>: Send + Sync {
    /// Handle the event with retry support
    async fn handle(&self, event: E, attempt: u32) -> HandlerResult;

    /// Maximum retry attempts
    fn max_retries(&self) -> u32 { 3 }

    /// Base delay for exponential backoff (ms)
    fn base_delay_ms(&self) -> u64 { 1000 }

    /// Get the handler name for logging
    fn name(&self) -> &'static str;
}

// ============================================================================
// SAMPLE HANDLERS
// ============================================================================

/// Sample external event (replace with actual event types)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExternalEntityCreatedEvent {
    pub id: Uuid,
    pub data: serde_json::Value,
}

/// Sample handler for external entity created events
pub struct ExternalEntityCreatedHandler {
    // Add dependencies here
}

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<()> {
        // TODO: Implement event handling logic
        tracing::info!("Handling ExternalEntityCreatedEvent: {:?}", event.id);
        Ok(())
    }

    fn name(&self) -> &'static str {
        "ExternalEntityCreatedHandler"
    }
}

// ============================================================================
// CUSTOM HANDLERS
// ============================================================================
// <<< CUSTOM HANDLERS START >>>

// <<< CUSTOM HANDLERS START >>>
// Add custom event handlers here
// Example:
//
// pub struct BillingInvoicePaidHandler {
//     user_service: Arc<UserService>,
// }
//
// #[async_trait]
// impl EventHandler<billing::InvoicePaidEvent> for BillingInvoicePaidHandler {
//     async fn handle(&self, event: billing::InvoicePaidEvent) -> Result<()> {
//         // Update user subscription status
//         self.user_service.activate_subscription(event.user_id).await
//     }
// }
// <<< CUSTOM HANDLERS END >>>