use std::time::Duration;
use flashq::FlashQ;
#[tokio::main]
async fn main() -> flashq::Result<()> {
let client = FlashQ::new();
client.connect().await?;
let job_id = client
.push(
"stream-demo",
serde_json::json!({"prompt": "Tell me about Rust"}),
None,
)
.await?;
println!("Pushed streaming job: {job_id}");
let worker_client = FlashQ::new();
worker_client.connect().await?;
if let Some(job) = worker_client
.pull("stream-demo", Some(Duration::from_secs(5)))
.await?
{
println!("Processing job {}...", job.id);
let chunks = vec![
"Rust is a ",
"systems programming ",
"language focused on ",
"safety and performance.",
];
for (i, chunk) in chunks.iter().enumerate() {
worker_client
.partial(job.id, serde_json::json!({"chunk": chunk}), Some(i as u32))
.await?;
println!(" Sent chunk {i}: {chunk}");
tokio::time::sleep(Duration::from_millis(100)).await;
}
worker_client
.log(job.id, "Generated 4 chunks", Some("info"))
.await?;
worker_client
.ack(
job.id,
Some(serde_json::json!({
"full_response": "Rust is a systems programming language focused on safety and performance.",
"chunks": 4,
})),
)
.await?;
println!("Job completed");
let logs = client.get_logs(job.id).await?;
println!("\nJob logs:");
for log in &logs {
println!(" [{}] {}: {}", log.level, log.timestamp, log.message);
}
}
worker_client.close().await?;
client.close().await?;
Ok(())
}