use std::time::Duration;
use wingfoil::adapters::redis::*;
use wingfoil::*;
const URL: &str = "redis://127.0.0.1:6379";
const SOURCE: &str = "example-source";
const DEST: &str = "example-dest";
fn main() -> anyhow::Result<()> {
let conn = RedisConnection::new(URL);
let processor_conn = conn.clone();
let processor = std::thread::spawn(move || -> anyhow::Result<()> {
redis_sub(processor_conn.clone(), SOURCE)
.map(|burst| {
burst
.into_iter()
.map(|event| {
let upper = event
.payload_str()
.unwrap_or("")
.to_uppercase()
.into_bytes();
RedisEntry {
channel: DEST.to_string(),
payload: upper,
}
})
.collect::<Burst<RedisEntry>>()
})
.redis_pub(processor_conn)
.run(RunMode::RealTime, RunFor::Duration(Duration::from_secs(2)))?;
Ok(())
});
let verifier_conn = conn.clone();
let verifier = std::thread::spawn(move || -> anyhow::Result<()> {
redis_sub(verifier_conn, DEST)
.collapse()
.for_each(|event, _| {
println!(
" {} -> {}",
event.channel,
event.payload_str().unwrap_or("?")
);
})
.run(RunMode::RealTime, RunFor::Duration(Duration::from_secs(2)))?;
Ok(())
});
std::thread::sleep(Duration::from_millis(500));
constant(burst![
RedisEntry {
channel: SOURCE.into(),
payload: b"hello".to_vec()
},
RedisEntry {
channel: SOURCE.into(),
payload: b"world".to_vec()
},
])
.redis_pub(conn)
.run(RunMode::RealTime, RunFor::Cycles(1))?;
processor.join().expect("processor thread panicked")?;
verifier.join().expect("verifier thread panicked")?;
Ok(())
}