use chrono::{DateTime, Utc};
use ruststream::OutgoingMessage;
use serde::{Deserialize, Serialize};
use ruststream_sqlx::prelude::*;
use sqlx::Connection;
use sqlx::sqlite::SqlitePoolOptions;
use ruststream_sqlx::Ack;
use sqlx::{Sqlite, SqliteConnection};
#[derive(Inbox, sqlx::FromRow)]
#[inbox(table = "email_jobs", custom(ack))]
struct SendEmail {
#[field(id, generated)]
job_id: i64,
#[field(group)]
name: String,
#[field(locked_until)]
locked_until: Option<DateTime<Utc>>,
#[field(payload)]
payload: Vec<u8>,
}
impl Ack<Sqlite> for SendEmail {
async fn ack(conn: &mut SqliteConnection, id: &i64) -> Result<(), sqlx::Error> {
sqlx::query("UPDATE email_jobs SET name = 'sent' WHERE job_id = ?")
.bind(id)
.execute(conn)
.await?;
Ok(())
}
}
#[subscriber(InboxQueue::<SendEmail>::new("emails"))]
async fn send(email: &Email) -> HandlerOutcome {
tracing::info!(to = %email.to, "sending");
HandlerOutcome::ack()
}
fn service(url: &str) -> RustStream {
let schema_url = url.to_owned();
let broker = SqlxBroker::from_url(url, SqlitePoolOptions::new());
RustStream::new(AppInfo::new("mailer", "0.1.0"))
.on_startup(async move |()| {
let mut conn = SqliteConnection::connect(&schema_url).await?;
sqlx::raw_sql(SCHEMA).execute(&mut conn).await?;
conn.close().await
})
.with_broker(broker.route::<SendEmail>("emails"), |b| {
b.include(send);
})
}
#[ruststream::app]
fn app() -> impl App {
service("sqlite:mailer.db?mode=rwc")
}
const SCHEMA: &str = "CREATE TABLE IF NOT EXISTS email_jobs (
job_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
locked_until TEXT,
payload BLOB NOT NULL
)";
impl Publish<Sqlite> for SendEmail {
async fn publish(
conn: &mut SqliteConnection,
message: &OutgoingMessage<'_>,
) -> Result<(), sqlx::Error> {
sqlx::query("INSERT INTO email_jobs (name, payload) VALUES (?, ?)")
.bind(message.name())
.bind(message.payload())
.execute(conn)
.await?;
Ok(())
}
}
#[derive(Debug, PartialEq, Deserialize, Serialize, Outgoing)]
#[outgoing(name = "emails")]
struct Email {
to: String,
}
#[cfg(test)]
mod tests {
use std::error::Error;
use ruststream::testing::TestApp;
use ruststream_sqlx::prelude::*;
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
use sqlx::{Connection, Sqlite, SqliteConnection, SqlitePool};
use super::{Email, SCHEMA, service};
async fn database(
name: &str,
) -> Result<(String, SqlitePool, SqliteConnection), Box<dyn Error>> {
let url = format!("sqlite:file:{name}?mode=memory&cache=shared");
let options: SqliteConnectOptions = url.parse()?;
let keeper = SqliteConnection::connect_with(&options).await?;
let pool = SqlitePoolOptions::new()
.min_connections(1)
.idle_timeout(None)
.connect_with(options)
.await?;
sqlx::raw_sql(SCHEMA).execute(&pool).await?;
Ok((url, pool, keeper))
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_sent_email_stays_in_the_sent_group() -> Result<(), Box<dyn Error>> {
let (url, pool, _keeper) = database("sqlx_tables_events").await?;
let tb = TestApp::start(service(&url)).await?;
tb.broker::<SqlxBroker<Sqlite>>()
.message(&Email {
to: "ops@example.com".to_owned(),
})
.publish()
.await?;
tb.broker::<SqlxBroker<Sqlite>>()
.subscriber("emails")
.assert_called_once()
.settled(HandlerOutcome::ack());
let groups: Vec<String> = sqlx::query_scalar("SELECT name FROM email_jobs")
.fetch_all(&pool)
.await?;
assert_eq!(
groups,
["sent"],
"the service's own ack kept the row in the sent group"
);
Ok(())
}
}