Skip to main content

OutboxManager

Struct OutboxManager 

Source
pub struct OutboxManager<S, P, PT>
where PT: Debug + Clone + Serialize,
{ /* private fields */ }
Expand description

Long-running worker that publishes pending outbox events to the broker.

The manager owns one concrete OutboxStorage implementation (S), one Transport implementation (P), and the user’s domain event payload type (PT). After run is invoked, it will keep processing events until the watched shutdown channel flips to true.

Prefer constructing through OutboxManagerBuilder rather than calling new directly — the builder reports missing dependencies with a structured OutboxError::ConfigError instead of requiring all arguments to line up positionally.

Implementations§

Source§

impl<S, P, PT> OutboxManager<S, P, PT>
where S: OutboxStorage<PT> + Send + Sync + 'static, P: Transport<PT> + Send + Sync + 'static, PT: Debug + Clone + Serialize + Send + Sync + 'static,

Source

pub fn new( storage: Arc<S>, publisher: Arc<P>, config: Arc<OutboxConfig<PT>>, shutdown_rx: Receiver<bool>, ) -> Self

Direct constructor used by OutboxManagerBuilder.

Application code should normally go through the builder.

This signature is compiled when the dlq feature is disabled and omits the DLQ heap argument.

Source

pub async fn run(self) -> Result<(), OutboxError>

Starts the main outbox worker loop.

This method will run until a shutdown signal is received via the shutdown_rx channel. It coordinates three concerns:

  • Event processing — on each wake-up it drives OutboxProcessor in an inner drain loop until the fetched batch is empty, at which point it returns to waiting.
  • Wake-up sources — a tokio::select! races a storage-level LISTEN/notify call, a poll interval (config.poll_interval_secs), and the shutdown receiver. A notification error is logged and the loop sleeps for 5 seconds before retrying.
  • Garbage collection — a background task is spawned that ticks on config.gc_interval_secs and calls [GarbageCollector::collect_garbage], exiting when the shutdown signal fires.
§Errors

Returns an OutboxError if the worker encounters a terminal failure that it cannot recover from. In the current implementation transient errors from the storage and transport layers are logged and the loop continues, so a returned error signals that the worker observed a graceful shutdown via shutdown_rx.

§Example
use std::sync::Arc;
use tokio::sync::watch;
use outbox_core::prelude::*;

let (shutdown_tx, shutdown_rx) = watch::channel(false);
let manager = OutboxManagerBuilder::new()
    .storage(storage)
    .publisher(publisher)
    .config(Arc::new(OutboxConfig::default()))
    .shutdown_rx(shutdown_rx)
    .build()?;

let handle = tokio::spawn(async move { manager.run().await });

// ... later, on a signal or process exit:
let _ = shutdown_tx.send(true);
handle.await.expect("worker panicked")?;

Auto Trait Implementations§

§

impl<S, P, PT> Freeze for OutboxManager<S, P, PT>

§

impl<S, P, PT> RefUnwindSafe for OutboxManager<S, P, PT>

§

impl<S, P, PT> Send for OutboxManager<S, P, PT>
where S: Sync + Send, P: Sync + Send,

§

impl<S, P, PT> Sync for OutboxManager<S, P, PT>
where S: Sync + Send, P: Sync + Send,

§

impl<S, P, PT> Unpin for OutboxManager<S, P, PT>

§

impl<S, P, PT> UnsafeUnpin for OutboxManager<S, P, PT>

§

impl<S, P, PT> UnwindSafe for OutboxManager<S, P, PT>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more