backbone-bucket 0.3.0

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
//! Event subscription registry for Bucket module
//!
//! Generated by backbone-schema. Do not edit manually.
//!
//! Register all event subscriptions with the event bus here.

use std::sync::Arc;
use anyhow::Result;
use async_trait::async_trait;

use super::handlers::*;

// ============================================================================
// SUBSCRIPTION TYPES
// ============================================================================

/// Event subscription configuration
#[derive(Debug, Clone)]
pub struct Subscription {
    /// Source module name
    pub source_module: String,
    /// Event type to subscribe to
    pub event_type: String,
    /// Handler name for logging
    pub handler_name: String,
}

/// Trait for event bus implementations
#[async_trait]
pub trait EventBus: Send + Sync {
    /// Subscribe to events
    async fn subscribe(&self, subscription: Subscription) -> Result<()>;

    /// Unsubscribe from events
    async fn unsubscribe(&self, subscription: &Subscription) -> Result<()>;
}

// ============================================================================
// SUBSCRIPTION REGISTRY
// ============================================================================

/// Registry for Bucket module event subscriptions
pub struct SubscriptionRegistry {
    subscriptions: Vec<Subscription>,
}

impl SubscriptionRegistry {
    /// Create a new subscription registry
    pub fn new() -> Self {
        Self {
            subscriptions: Self::default_subscriptions(),
        }
    }

    /// Get default subscriptions for this module
    fn default_subscriptions() -> Vec<Subscription> {
        vec![
            // Add subscriptions here, e.g.:
            // Subscription {
            //     source_module: "billing".to_string(),
            //     event_type: "InvoicePaidEvent".to_string(),
            //     handler_name: "BillingInvoicePaidHandler".to_string(),
            // },
        ]
    }

    /// Register all subscriptions with an event bus
    pub async fn register_all<B: EventBus>(&self, bus: &B) -> Result<()> {
        for subscription in &self.subscriptions {
            tracing::info!(
                "Registering subscription: {}::{}",
                subscription.source_module,
                subscription.event_type
            );
            bus.subscribe(subscription.clone()).await?;
        }
        Ok(())
    }

    /// Unregister all subscriptions
    pub async fn unregister_all<B: EventBus>(&self, bus: &B) -> Result<()> {
        for subscription in &self.subscriptions {
            bus.unsubscribe(subscription).await?;
        }
        Ok(())
    }

    /// Get all registered subscriptions
    pub fn subscriptions(&self) -> &[Subscription] {
        &self.subscriptions
    }

    /// Add a subscription
    pub fn add(&mut self, subscription: Subscription) {
        self.subscriptions.push(subscription);
    }
}

impl Default for SubscriptionRegistry {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// DEAD LETTER QUEUE
// ============================================================================

/// Failed event for dead letter queue
#[derive(Debug, Clone)]
pub struct FailedEvent {
    pub event_id: uuid::Uuid,
    pub source_module: String,
    pub event_type: String,
    pub payload: serde_json::Value,
    pub error: String,
    pub attempts: u32,
    pub failed_at: chrono::DateTime<chrono::Utc>,
}

/// Dead letter queue for failed events
#[async_trait]
pub trait DeadLetterQueue: Send + Sync {
    /// Send event to dead letter queue
    async fn send(&self, event: FailedEvent) -> Result<()>;

    /// Retrieve events from dead letter queue
    async fn retrieve(&self, limit: usize) -> Result<Vec<FailedEvent>>;

    /// Retry a failed event
    async fn retry(&self, event_id: uuid::Uuid) -> Result<()>;

    /// Discard a failed event
    async fn discard(&self, event_id: uuid::Uuid) -> Result<()>;
}

// ============================================================================
// CUSTOM SUBSCRIPTIONS
// ============================================================================
// <<< CUSTOM SUBSCRIPTIONS START >>>

// <<< CUSTOM SUBSCRIPTIONS START >>>
// Add custom subscription configuration here
// <<< CUSTOM SUBSCRIPTIONS END >>>