use std::future::{Future, ready};
use chrono::{DateTime, Utc};
use ruststream::OutgoingMessage;
use ruststream::schemars::JsonSchema;
use ruststream_sqlx::dialect::Column;
use ruststream_sqlx::spec::{Lease, Payload};
use ruststream_sqlx::{InboxSpec, InboxTable, PayloadRow};
use serde::{Deserialize, Serialize};
use sqlx::{Sqlite, SqliteConnection};
use ruststream_sqlx::prelude::*;
use sqlx::Connection;
use sqlx::sqlite::SqlitePoolOptions;
#[derive(sqlx::FromRow)]
struct Notify {
id: i64,
payload: Vec<u8>,
}
impl InboxTable for Notify {
type Id = i64;
type Table = InboxSpec<(Lease<DateTime<Utc>>, Payload)>;
const TABLE: Self::Table = InboxSpec::new("notification_jobs", Column::new("id").generated())
.group(Column::new("channel"))
.lease(Column::new("locked_until"))
.payload(Column::new("payload"));
fn id(&self) -> &i64 {
&self.id
}
}
impl PayloadRow for Notify {
type Column = Vec<u8>;
fn payload(&self) -> &[u8] {
&self.payload
}
}
struct SendEmail;
impl Handle<Notice> for SendEmail {
fn handle(
&self,
notice: &Notice,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
tracing::info!(to = %notice.to, "sending an email");
ready(Ok(()))
}
}
struct SendSms;
impl Handle<Notice> for SendSms {
fn handle(
&self,
notice: &Notice,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
tracing::info!(to = %notice.to, "sending a text message");
ready(Ok(()))
}
}
fn service(url: &str) -> RustStream {
let schema_url = url.to_owned();
let broker = SqlxBroker::from_url(url, SqlitePoolOptions::new());
let broker = broker.route::<Notify>("email").route::<Notify>("sms");
RustStream::new(AppInfo::new("notifier", "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, |b| {
let email =
subscriber("email", SendEmail).map_source(|_| InboxQueue::<Notify>::new("email"));
b.include(email.build());
let sms = subscriber("sms", SendSms).map_source(|_| InboxQueue::<Notify>::new("sms"));
b.include(sms.build());
})
}
#[ruststream::app]
fn app() -> impl App {
service("sqlite:notifications.db?mode=rwc")
}
const SCHEMA: &str = "CREATE TABLE IF NOT EXISTS notification_jobs (
id INTEGER PRIMARY KEY,
channel TEXT NOT NULL,
locked_until TEXT,
payload BLOB NOT NULL
)";
impl Publish<Sqlite> for Notify {
async fn publish(
conn: &mut SqliteConnection,
message: &OutgoingMessage<'_>,
) -> Result<(), sqlx::Error> {
sqlx::query("INSERT INTO notification_jobs (channel, payload) VALUES (?, ?)")
.bind(message.name())
.bind(message.payload())
.execute(conn)
.await?;
Ok(())
}
}
#[derive(Debug, PartialEq, Deserialize, Serialize, Outgoing, JsonSchema)]
#[schemars(crate = "ruststream::schemars")]
struct Notice {
to: String,
text: 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::{Notice, 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_notice_reaches_the_subscription_of_its_group() -> Result<(), Box<dyn Error>> {
let (url, _pool, _keeper) = database("manual_sqlx_subscriptions_queue").await?;
let tb = TestApp::start(service(&url)).await?;
let notice = Notice {
to: "+15550100".to_owned(),
text: "Your order shipped".to_owned(),
};
tb.broker::<SqlxBroker<Sqlite>>()
.message(¬ice)
.to("sms")
.publish()
.await?;
tb.broker::<SqlxBroker<Sqlite>>()
.subscriber("sms")
.assert_called_once()
.with(¬ice)
.settled(HandlerOutcome::ack());
tb.broker::<SqlxBroker<Sqlite>>()
.subscriber("email")
.assert_not_called();
Ok(())
}
}