use std::error::Error;
use std::future::{Future, ready};
use ruststream::memory::prelude::*;
use serde::Deserialize;
#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct Order {
id: u64,
}
struct Notify;
impl Handle<Order> for Notify {
fn handle(
&self,
order: &Order,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
let id = order.id;
ready(Err(HandlerOutcome::ack().and_after(async move {
println!("order {id} acked; notifying downstream");
})))
}
}
struct NotifyBatch;
impl Handle<[Order]> for NotifyBatch {
fn handle(
&self,
orders: &[Order],
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), Vec<HandlerOutcome>>> {
ready(Err(orders
.iter()
.map(|order| {
if order.id == 0 {
HandlerOutcome::retry()
} else {
let id = order.id;
HandlerOutcome::ack().and_after(async move {
println!("order {id} acked in batch; following up");
})
}
})
.collect()))
}
}
fn app() -> RustStream {
RustStream::new(AppInfo::new("post_settle", "0.1.0")).with_broker(MemoryBroker::new(), |b| {
b.include(subscriber("orders", Notify).build());
b.include(
subscriber("orders", NotifyBatch)
.batch(nonzero!(64))
.build(),
);
})
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
app().run().await?;
Ok(())
}