Skip to main content

AttemptColumn

Trait AttemptColumn 

Source
pub trait AttemptColumn {
    // Required method
    fn attempt(&self) -> u64;
}
Expand description

The type of a field playing attempt: the delivery’s redelivery count.

The first delivery of a row reads 1, so a table’s attempt column starts at 1. A negative value reads as 0.

§Examples

use ruststream_sqlx::prelude::*;
use serde::Deserialize;
use sqlx::PgPool;

#[derive(Inbox, sqlx::FromRow)]
#[inbox(table = "charge_jobs")]
pub struct ChargeJob {
    #[field(id, generated)]
    id: i64,
    // sqlx has no unsigned integers on Postgres, so the column is a `SMALLINT DEFAULT 1`.
    #[field(attempt, generated)]
    attempt: i16,
    #[field(payload)]
    payload: Vec<u8>,
}

#[derive(Deserialize)]
struct Charge {
    order: u64,
}

#[subscriber(InboxQueue::<ChargeJob>::new("charges"))]
async fn charge(request: &Charge, Ctx(attempt): Ctx<keys::Attempt>) -> HandlerOutcome {
    tracing::info!(request.order, ?attempt, "charging");
    if try_charge(request).await { HandlerOutcome::ack() } else { HandlerOutcome::retry() }
}

pub fn app(pool: PgPool) -> RustStream {
    RustStream::new(AppInfo::new("payments", "0.1.0")).with_broker(SqlxBroker::new(pool), |b| {
        // The attempt counts the deliveries: the fifth failure moves the row into
        // `failed_charge_jobs`, a table with the same columns.
        b.include(charge).max_attempts(nonzero!(5u32)).dead_letter("failed_charge_jobs");
    })
}

Required Methods§

Source

fn attempt(&self) -> u64

The attempt, counting the first delivery as 1.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl AttemptColumn for i16

Source§

fn attempt(&self) -> u64

Source§

impl AttemptColumn for i32

Source§

fn attempt(&self) -> u64

Source§

impl AttemptColumn for i64

Source§

fn attempt(&self) -> u64

Source§

impl AttemptColumn for u16

Source§

fn attempt(&self) -> u64

Source§

impl AttemptColumn for u32

Source§

fn attempt(&self) -> u64

Source§

impl AttemptColumn for u64

Source§

fn attempt(&self) -> u64

Implementors§