Skip to main content

SubscriptionBuilder

Struct SubscriptionBuilder 

Source
pub struct SubscriptionBuilder<E: Executor> { /* private fields */ }
Expand description

Builder for creating event subscriptions.

Created via [Projection::subscription], this builder configures a continuous event processing subscription with retry logic, routing key filtering, and graceful shutdown support.

§Example

let subscription = projection
    .subscription()
    .routing_key("accounts")
    .chunk_size(100)
    .retry(5)
    .delay(Duration::from_secs(10))
    .start(&executor)
    .await?;

// Later, gracefully shutdown
subscription.shutdown().await?;

Implementations§

Source§

impl<E: Executor + 'static> SubscriptionBuilder<E>

Source

pub fn new(key: impl Into<String>) -> Self

Creates a new projection with the given key.

The key is used as the subscription identifier for cursor tracking.

Source

pub fn safety_check(self) -> Self

Enables safety checks for unhandled events.

When enabled, processing fails if an event is encountered without a handler.

Source

pub fn handler<H: Handler<E> + 'static>(self, h: H) -> Self

Registers an event handler with this subscription.

§Panics

Panics if a handler for the same event type is already registered.

Source

pub fn skip<EV: AggregatorEvent + Send + Sync + 'static>(self) -> Self

Registers a skip handler for an event type.

Events of this type will be acknowledged but not processed.

§Panics

Panics if a handler for the same event type is already registered.

Source

pub fn data<D: Send + Sync + 'static>(self, v: D) -> Self

Adds shared data to the subscription context.

Data added here is accessible in handlers via the context.

Source

pub fn accept_failure(self) -> Self

Allows the subscription to continue after handler failures.

By default, subscriptions stop on the first error. With this flag, errors are logged but processing continues.

Source

pub fn chunk_size(self, v: u16) -> Self

Sets the number of events to process per batch.

Default is 300.

Source

pub fn delay(self, v: Duration) -> Self

Sets a delay before starting the subscription.

Useful for staggering subscription starts in multi-node deployments.

Source

pub fn routing_key(self, v: impl Into<String>) -> Self

Filters events by routing key.

Only events with the matching routing key will be processed.

Source

pub fn retry(self, v: u8) -> Self

Sets the maximum number of retries on failure.

Uses exponential backoff. Default is 30.

Source

pub fn all(self) -> Self

Processes all events regardless of routing key.

Source

pub fn aggregator<A: Aggregator>(self, id: impl Into<String>) -> Self

Adds a related aggregate to process events from.

Source

pub async fn unretry_start(self, executor: &E) -> Result<Subscription>
where E: Clone,

Starts the subscription without retry logic.

Equivalent to calling start() with retries disabled.

Source

pub async fn start(self, executor: &E) -> Result<Subscription>
where E: Clone,

Starts a continuous background subscription.

Returns a Subscription handle that can be used for graceful shutdown. The subscription runs in a spawned tokio task and polls for new events.

Source

pub async fn unretry_execute(self, executor: &E) -> Result<()>

Executes the subscription once without retry logic.

Processes all pending events and returns. Does not poll for new events.

Source

pub async fn execute(&self, executor: &E) -> Result<()>

Executes the subscription once, processing all pending events.

Unlike start(), this does not run continuously. It processes all currently pending events and returns.

Auto Trait Implementations§

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> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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