Skip to main content

AttemptRow

Trait AttemptRow 

Source
pub trait AttemptRow {
    type Attempt: AttemptColumn + 'static;

    // Required method
    fn attempt(&self) -> &Self::Attempt;
}
Expand description

The row’s attempt, which a table described by hand reads off its row where its description sets InboxSpec::attempt.

The column’s type is part of the trait, so a row that does not decode still reports its attempt, read alone.

§Examples

use chrono::{DateTime, Utc};
use ruststream_sqlx::dialect::Column;
use ruststream_sqlx::prelude::*;
use ruststream_sqlx::spec::{Attempt, Lease, Payload};
use ruststream_sqlx::{AttemptRow, InboxSpec, InboxTable, PayloadRow};
use serde::Deserialize;
use sqlx::{Sqlite, SqlitePool};

#[derive(sqlx::FromRow)]
pub struct EmailJob {
    job_id: i64,
    attempt: i16,
    payload: Vec<u8>,
}

impl InboxTable for EmailJob {
    type Id = i64;
    type Table = InboxSpec<(Lease<DateTime<Utc>>, Attempt, Payload)>;
    const TABLE: Self::Table = InboxSpec::new("email_jobs", Column::new("job_id").generated())
        .lease(Column::new("locked_until"))
        .group(Column::new("name"))
        .attempt(Column::new("attempt").generated())
        .payload(Column::new("payload"));

    fn id(&self) -> &i64 {
        &self.job_id
    }
}

impl PayloadRow for EmailJob {
    type Column = Vec<u8>;

    fn payload(&self) -> &[u8] {
        &self.payload
    }
}

impl AttemptRow for EmailJob {
    type Attempt = i16;

    fn attempt(&self) -> &i16 {
        &self.attempt
    }
}

#[derive(Deserialize)]
struct Email {
    to: String,
}

// The third attempt gives up on an address the first two could not reach.
#[subscriber(InboxQueue::<EmailJob>::new("emails"))]
async fn send(email: &Email, Ctx(attempt): Ctx<keys::Attempt>) -> HandlerOutcome {
    match attempt {
        Some(3..) => HandlerOutcome::drop(),
        _ if email.to.contains('@') => HandlerOutcome::ack(),
        _ => HandlerOutcome::retry(),
    }
}

pub fn app(pool: SqlitePool) -> RustStream {
    RustStream::new(AppInfo::new("mailer", "1.0.0")).with_broker(
        SqlxBroker::<Sqlite>::new(pool),
        |b| {
            b.include(send);
        },
    )
}

Required Associated Types§

Source

type Attempt: AttemptColumn + 'static

The attempt column’s type: a signed integer.

Required Methods§

Source

fn attempt(&self) -> &Self::Attempt

The attempt field.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§