Skip to main content

Notifies

Trait Notifies 

Source
pub trait Notifies: QueueDatabase + Listens { }
Expand description

A database whose sessions notify each other: a broker on it may listen_notify.

Postgres alone implements it, with LISTEN and pg_notify.

§Examples

use ruststream_sqlx::prelude::*;
use ruststream_sqlx::{BuiltInDialect, Notifies};
use serde::Deserialize;
use sqlx::{PgPool, Pool};

#[derive(Inbox, sqlx::FromRow)]
#[inbox(table = "email_jobs")]
pub struct SendEmail {
    #[field(id, generated)]
    job_id: i64,
    #[field(group)]
    name: String,
    #[field(payload)]
    payload: Vec<u8>,
}

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

#[subscriber(InboxQueue::<SendEmail>::new("emails"))]
async fn send(email: &Email) -> HandlerOutcome {
    tracing::info!(to = %email.to, "sending");
    HandlerOutcome::ack()
}

// The service's broker wakes on notifications, on whichever database sends them.
pub fn notified<DB: Notifies + BuiltInDialect>(pool: Pool<DB>) -> SqlxBroker<DB> {
    SqlxBroker::new(pool).listen_notify()
}

pub fn app(pool: PgPool) -> RustStream {
    RustStream::new(AppInfo::new("mailer", "0.1.0")).with_broker(notified(pool), |b| {
        b.include(send);
    })
}

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementations on Foreign Types§

Source§

impl Notifies for Postgres

Available on crate feature postgres only.

Implementors§