use crate::error::StorageResult;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DlqRecord {
pub id: Uuid,
pub original_message_id: Option<String>,
pub from: Option<Vec<u8>>,
pub to: Option<Vec<u8>>,
pub raw_bytes: Vec<u8>,
pub error_message: String,
pub error_category: String,
pub trace_id: String,
pub request_id: Option<String>,
pub created_at: DateTime<Utc>,
pub redrive_attempts: u32,
pub last_redrive_at: Option<DateTime<Utc>>,
pub context: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DlqStats {
pub total_messages: u64,
pub messages_by_category: std::collections::HashMap<String, u64>,
pub messages_with_redrive_attempts: u64,
pub oldest_message_at: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, Default)]
pub struct DlqQuery {
pub error_category: Option<String>,
pub trace_id: Option<String>,
pub from: Option<Vec<u8>>,
pub limit: Option<u32>,
pub created_after: Option<DateTime<Utc>>,
}
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
pub trait DeadLetterQueue: Send + Sync {
async fn enqueue(&self, record: DlqRecord) -> StorageResult<Uuid>;
async fn query(&self, query: DlqQuery) -> StorageResult<Vec<DlqRecord>>;
async fn get(&self, id: Uuid) -> StorageResult<Option<DlqRecord>>;
async fn delete(&self, id: Uuid) -> StorageResult<()>;
async fn record_redrive_attempt(&self, id: Uuid) -> StorageResult<()>;
async fn stats(&self) -> StorageResult<DlqStats>;
}