1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/// Example 01: Basic Operations - Push, Pull, Ack
///
/// Demonstrates fundamental queue operations:
/// - Connecting to flashQ server
/// - Pushing a job to a queue
/// - Pulling a job from the queue
/// - Acknowledging job completion
use std::time::Duration;
use flashq::FlashQ;
#[tokio::main]
async fn main() -> flashq::Result<()> {
// Create client with default options
let client = FlashQ::new();
// Connect to server
client.connect().await?;
println!("Connected to flashQ server");
// Push a job
let job_id = client
.push(
"emails",
serde_json::json!({
"to": "user@example.com",
"subject": "Welcome!",
"body": "Hello from flashQ",
}),
None,
)
.await?;
println!("Pushed job: {job_id}");
// Pull the job (blocking)
let job = client.pull("emails", Some(Duration::from_secs(5))).await?;
if let Some(job) = job {
println!("Pulled job {}: {:?}", job.id, job.data);
// Process the job (simulate work)
println!("Processing email...");
tokio::time::sleep(Duration::from_millis(100)).await;
// Acknowledge completion with result
client
.ack(job.id, Some(serde_json::json!({"sent": true})))
.await?;
println!("Acknowledged job");
// Get the result
let result = client.get_result(job.id).await?;
println!("Job result: {:?}", result);
}
client.close().await?;
println!("\nDone!");
Ok(())
}