use ruststream::OutgoingMessage;
use serde::{Deserialize, Serialize};
use sqlx::postgres::{PgConnectOptions, PgConnection, Postgres};
use ruststream_sqlx::prelude::*;
use sqlx::postgres::PgPoolOptions;
#[derive(Inbox, sqlx::FromRow)]
#[inbox(table = "email_jobs", checked, db = postgres)]
struct SendEmail {
#[field(id, generated)]
job_id: i64,
#[field(group)]
name: String,
#[field(attempt, generated)]
attempt: i16,
#[field(payload)]
payload: Vec<u8>,
}
impl Publish<Postgres> for SendEmail {
async fn publish(
conn: &mut PgConnection,
message: &OutgoingMessage<'_>,
) -> Result<(), sqlx::Error> {
sqlx::query("INSERT INTO email_jobs (name, payload) VALUES ($1, $2)")
.bind(message.name())
.bind(message.payload())
.execute(conn)
.await?;
Ok(())
}
}
#[derive(Debug, PartialEq, Deserialize, Serialize, Outgoing)]
#[outgoing(name = "emails")]
struct Email {
to: String,
}
#[subscriber(InboxQueue::<SendEmail>::new("emails"))]
async fn send(email: &Email, Ctx(attempt): Ctx<keys::Attempt>) -> HandlerOutcome {
tracing::info!(to = %email.to, ?attempt, "sending");
HandlerOutcome::ack()
}
fn service(options: PgConnectOptions) -> RustStream {
let broker = SqlxBroker::from_connect_options(options, PgPoolOptions::new());
RustStream::new(AppInfo::new("mailer", "0.1.0")).with_broker(
broker.route::<SendEmail>("emails"),
|b| {
b.include(send);
},
)
}
#[ruststream::app]
fn app() -> impl App {
service(PgConnectOptions::new())
}
#[cfg(test)]
#[path = "../tests/live/mod.rs"]
mod live;
#[cfg(test)]
mod tests {
use std::error::Error;
use ruststream::testing::TestApp;
use ruststream_sqlx::prelude::*;
use sqlx::Postgres;
use super::{Email, live, service};
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn the_checked_statements_serve_the_queue() -> Result<(), Box<dyn Error>> {
let Some(db) = live::postgres::database().await else {
return Ok(());
};
let tb = TestApp::start(service(db.options())).await?;
let email = Email {
to: "ops@example.com".to_owned(),
};
tb.broker::<SqlxBroker<Postgres>>()
.message(&email)
.publish()
.await?;
tb.broker::<SqlxBroker<Postgres>>()
.subscriber("emails")
.assert_called_once()
.with(&email)
.settled(HandlerOutcome::ack());
tb.shutdown().await?;
db.finish().await;
Ok(())
}
}