redis
ModuleThe Redis broker.
Contents
use utopia_queue::broker::redis::Redis;pub struct RedisThe Redis broker: publishes onto a list and delivers with at-least-once semantics through Lua scripts that keep every claim recoverable.
Per queue <namespace>.<kind>.<name> keys hold the work list (queue),
in-flight pids (processing), rejected pids (failed), parked pids
(dead), undecodable bytes (poison), each claim's payload (jobs.<pid>),
heartbeat (claims.<pid>) and owner (owners.<pid>), and counters
(stats.<name>.total|processing|success|failed).
It takes two connections: receive for the blocking receive loop and its
claim writes (one caller), and commands for settlements and publishing
(wrap it in crate::connection::Locking when tasks share it).
use std::sync::Arc;
use utopia_queue::broker::Redis;
use utopia_queue::connection::Redis as Connection;
use utopia_queue::{Consumer, Publisher, Queue};
let broker = Redis::new(Arc::new(Connection::new("localhost", 6379)), Arc::new(Connection::new("localhost", 6379)));
let queue = Queue::new("emails")?;
broker.publish(&queue, utopia_queue::payload(&serde_json::json!({"to": "ada@example.com"}))?).await?;
for message in broker.receive(&queue, 1, 10).await? {
broker.commit(&queue, &message).await?;
}pub type ReconnectCallback = Box<dyn Fn(&Queue, &Error, i64, i64) + Send + Sync>Called when a receive fails on the transport: the queue, the error, the attempt number and the milliseconds the broker sleeps before returning.
pub type ReconnectSuccessCallback = Box<dyn Fn(&Queue, i64) + Send + Sync>Called on the first successful receive after failures, with their count.