use async_trait::async_trait;
use crate::error::EventError;
use crate::event::cloud::CloudEvent;
#[cfg(feature = "redis")]
pub mod redis_stream;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JobId(
pub String,
);
#[derive(Debug, Clone)]
pub struct QueuePolicy {
pub queue: &'static str,
pub consumer_group: &'static str,
pub max_attempts: u32,
pub max_in_flight: usize,
pub block_ms: u64,
pub claim_idle_ms: u64,
}
impl QueuePolicy {
pub fn new(queue: &'static str, consumer_group: &'static str) -> Self {
Self {
queue,
consumer_group,
max_attempts: 3,
max_in_flight: 8,
block_ms: 1000,
claim_idle_ms: 300_000,
}
}
}
#[derive(Debug, Clone)]
pub struct QueuedJob {
pub id: JobId,
pub event: CloudEvent,
pub attempts: u32,
}
#[async_trait]
pub trait QueueBroker: Send + Sync {
async fn enqueue(
&self,
queue: &str,
event: CloudEvent,
attempts: u32,
) -> Result<JobId, EventError>;
async fn read(
&self,
queue: &str,
group: &str,
consumer: &str,
count: usize,
block_ms: u64,
) -> Result<Vec<QueuedJob>, EventError>;
async fn claim_stale(
&self,
queue: &str,
group: &str,
consumer: &str,
count: usize,
min_idle_ms: u64,
) -> Result<Vec<QueuedJob>, EventError>;
async fn ack(&self, queue: &str, group: &str, id: &JobId) -> Result<(), EventError>;
async fn dead_letter(
&self,
queue: &str,
event: CloudEvent,
attempts: u32,
error: String,
) -> Result<JobId, EventError>;
}