backbone-queue 2.7.35

Backbone Framework Queue Module - Redis, SQS, and RabbitMQ support
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Worker Pool Example
//!
//! This example demonstrates how to build a worker pool that processes messages
//! from a queue with concurrent processing, error handling, and graceful shutdown.

use backbone_queue::{
    QueueService,
    redis::RedisQueueBuilder,
    types::{QueueMessage, QueuePriority, MessageStatus}
};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{RwLock, Semaphore};
use tokio::time::sleep;
use tokio_util::sync::CancellationToken;

/// Worker configuration
struct WorkerConfig {
    max_concurrent_tasks: usize,
    poll_interval: Duration,
    max_retries: u32,
    visibility_timeout: u64,
    shutdown_timeout: Duration,
}

impl Default for WorkerConfig {
    fn default() -> Self {
        Self {
            max_concurrent_tasks: 10,
            poll_interval: Duration::from_millis(100),
            max_retries: 3,
            visibility_timeout: 30,
            shutdown_timeout: Duration::from_secs(30),
        }
    }
}

/// Message processing result
#[derive(Debug, Clone)]
enum ProcessResult {
    Success,
    RetryableError(String),
    PermanentError(String),
}

/// Worker statistics
#[derive(Debug, Default)]
struct WorkerStats {
    messages_processed: u64,
    messages_failed: u64,
    messages_retried: u64,
    total_processing_time: Duration,
}

/// Queue Worker Pool
struct QueueWorker {
    queue: Arc<dyn QueueService + Send + Sync>,
    config: WorkerConfig,
    stats: Arc<RwLock<WorkerStats>>,
    semaphore: Arc<Semaphore>,
    cancellation_token: CancellationToken,
}

impl QueueWorker {
    fn new(
        queue: Arc<dyn QueueService + Send + Sync>,
        config: WorkerConfig,
    ) -> Self {
        Self {
            queue,
            semaphore: Arc::new(Semaphore::new(config.max_concurrent_tasks)),
            stats: Arc::new(RwLock::new(WorkerStats::default())),
            config,
            cancellation_token: CancellationToken::new(),
        }
    }

    /// Start the worker pool
    async fn start(&self) -> Result<(), Box<dyn std::error::Error>> {
        println!("๐Ÿš€ Starting queue worker pool");
        println!("๐Ÿ“Š Configuration:");
        println!("  - Max concurrent tasks: {}", self.config.max_concurrent_tasks);
        println!("  - Poll interval: {:?}", self.config.poll_interval);
        println!("  - Max retries: {}", self.config.max_retries);
        println!("  - Visibility timeout: {} seconds", self.config.visibility_timeout);

        let queue = self.queue.clone();
        let stats = self.stats.clone();
        let semaphore = self.semaphore.clone();
        let config = self.config.clone();
        let cancellation_token = self.cancellation_token.clone();

        // Spawn statistics reporter
        let stats_reporter = tokio::spawn({
            let stats = stats.clone();
            async move {
                let mut interval = tokio::time::interval(Duration::from_secs(10));
                loop {
                    tokio::select! {
                        _ = interval.tick() => {
                            let stats = stats.read().await;
                            Self::print_stats(&*stats);
                        }
                        _ = cancellation_token.cancelled() => {
                            break;
                        }
                    }
                }
            }
        });

        // Main worker loop
        let worker_handle = tokio::spawn(async move {
            let mut consecutive_empty_polls = 0;
            let max_empty_polls = 50; // Back off after many empty polls

            loop {
                tokio::select! {
                    _ = cancellation_token.cancelled() => {
                        println!("๐Ÿ›‘ Worker pool shutdown requested");
                        break;
                    }
                    _ = sleep(config.poll_interval) => {
                        // Get a permit for concurrent processing
                        let permit = semaphore.clone().acquire_owned().await;

                        // Check if we have permission to process
                        if permit.is_err() {
                            continue; // All workers are busy
                        }

                        match queue.dequeue().await {
                            Ok(Some(message)) => {
                                consecutive_empty_polls = 0;

                                // Clone necessary data for the task
                                let queue_clone = queue.clone();
                                let stats_clone = stats.clone();
                                let config_clone = config.clone();
                                let cancellation_token_clone = cancellation_token.clone();

                                // Spawn processing task
                                tokio::spawn(async move {
                                    let _permit = permit; // Hold permit for the duration of processing

                                    let start_time = std::time::Instant::now();
                                    let result = Self::process_message(&message).await;
                                    let processing_time = start_time.elapsed();

                                    // Update statistics
                                    {
                                        let mut stats = stats_clone.write().await;
                                        stats.total_processing_time += processing_time;

                                        match result {
                                            ProcessResult::Success => {
                                                stats.messages_processed += 1;
                                                println!("โœ… Processed message: {}", message.id);
                                            }
                                            ProcessResult::RetryableError(error) => {
                                                if message.receive_count < config_clone.max_retries {
                                                    stats.messages_retried += 1;
                                                    println!("โš ๏ธ  Retrying message: {} - {}", message.id, error);
                                                    // Return message to queue with delay
                                                    let _ = queue_clone.nack(&message.id, Some(60)).await;
                                                } else {
                                                    stats.messages_failed += 1;
                                                    println!("โŒ Max retries exceeded for message: {} - {}", message.id, error);
                                                    // Move to dead letter queue by acknowledging
                                                    let _ = queue_clone.ack(&message.id).await;
                                                }
                                            }
                                            ProcessResult::PermanentError(error) => {
                                                stats.messages_failed += 1;
                                                println!("โŒ Permanent error for message: {} - {}", message.id, error);
                                                // Acknowledge to remove from queue
                                                let _ = queue_clone.ack(&message.id).await;
                                            }
                                        }
                                    }

                                    // Check for shutdown
                                    if cancellation_token_clone.is_cancelled() {
                                        // Return message to queue for later processing
                                        let _ = queue_clone.nack(&message.id, None).await;
                                    }
                                });
                            }
                            Ok(None) => {
                                consecutive_empty_polls += 1;
                                if consecutive_empty_polls >= max_empty_polls {
                                    // Back off polling when queue is empty
                                    sleep(Duration::from_secs(1)).await;
                                    consecutive_empty_polls = 0;
                                }
                            }
                            Err(e) => {
                                eprintln!("โŒ Error dequeuing message: {}", e);
                                sleep(Duration::from_secs(1)).await;
                            }
                        }
                    }
                }
            }
        });

        // Setup Ctrl+C handler for graceful shutdown
        let cancellation_token_clone = self.cancellation_token.clone();
        tokio::spawn(async move {
            tokio::signal::ctrl_c().await.ok();
            println!("\n๐Ÿ›‘ Ctrl+C received, initiating graceful shutdown...");
            cancellation_token_clone.cancel();
        });

        // Wait for worker completion
        tokio::select! {
            result = worker_handle => {
                println!("๐Ÿ Worker completed: {:?}", result);
            }
            _ = tokio::signal::ctrl_c() => {
                println!("๐Ÿ›‘ Immediate shutdown requested");
            }
        }

        // Cancel statistics reporter
        stats_reporter.abort();

        // Wait for all processing tasks to complete
        println!("โณ Waiting for ongoing tasks to complete...");
        let _ = tokio::time::timeout(self.config.shutdown_timeout, async {
            // Wait for all permits to be released
            while self.semaphore.available_permits() < self.config.max_concurrent_tasks {
                sleep(Duration::from_millis(100)).await;
            }
        }).await;

        // Print final statistics
        let final_stats = self.stats.read().await;
        Self::print_final_stats(&*final_stats);

        println!("โœ… Worker pool shutdown complete");
        Ok(())
    }

    /// Process a single message
    async fn process_message(message: &QueueMessage) -> ProcessResult {
        // Simulate different processing scenarios based on message content
        let payload = message.payload.as_str().unwrap_or("");

        // Simulate processing time
        let processing_time = match message.priority {
            QueuePriority::Critical => Duration::from_millis(100),
            QueuePriority::High => Duration::from_millis(200),
            QueuePriority::Normal => Duration::from_millis(500),
            QueuePriority::Low => Duration::from_millis(1000),
        };

        sleep(processing_time).await;

        // Check for special message patterns that trigger different behaviors
        if payload.contains("error-retryable") {
            return ProcessResult::RetryableError("Simulated retryable error".to_string());
        }

        if payload.contains("error-permanent") {
            return ProcessResult::PermanentError("Simulated permanent error".to_string());
        }

        if payload.contains("slow") {
            sleep(Duration::from_secs(2)).await;
        }

        // Simulate different success scenarios
        match payload {
            p if p.contains("notification") => {
                println!("๐Ÿ“ง Sending notification: {}", p);
                ProcessResult::Success
            }
            p if p.contains("email") => {
                println!("๐Ÿ“ง Processing email: {}", p);
                ProcessResult::Success
            }
            p if p.contains("report") => {
                println!("๐Ÿ“Š Generating report: {}", p);
                ProcessResult::Success
            }
            p if p.contains("cleanup") => {
                println!("๐Ÿงน Performing cleanup: {}", p);
                ProcessResult::Success
            }
            _ => {
                println!("โœ… Processed generic task: {}", payload);
                ProcessResult::Success
            }
        }
    }

    /// Print current statistics
    fn print_stats(stats: &WorkerStats) {
        println!("๐Ÿ“Š Worker Statistics:");
        println!("  - Messages processed: {}", stats.messages_processed);
        println!("  - Messages failed: {}", stats.messages_failed);
        println!("  - Messages retried: {}", stats.messages_retried);

        if stats.messages_processed > 0 {
            let avg_processing_time = stats.total_processing_time / stats.messages_processed as u32;
            println!("  - Avg processing time: {:?}", avg_processing_time);
        }

        let success_rate = if stats.messages_processed + stats.messages_failed > 0 {
            stats.messages_processed as f64 / (stats.messages_processed + stats.messages_failed) as f64 * 100.0
        } else {
            0.0
        };
        println!("  - Success rate: {:.1}%", success_rate);
    }

    /// Print final statistics summary
    fn print_final_stats(stats: &WorkerStats) {
        println!("\n๐Ÿ“‹ Final Worker Statistics:");
        println!("===========================");

        let total_messages = stats.messages_processed + stats.messages_failed;

        println!("  ๐Ÿ“ˆ Total messages handled: {}", total_messages);
        println!("  โœ… Successfully processed: {}", stats.messages_processed);
        println!("  โŒ Failed: {}", stats.messages_failed);
        println!("  ๐Ÿ”„ Retries attempted: {}", stats.messages_retried);

        if total_messages > 0 {
            let success_rate = stats.messages_processed as f64 / total_messages as f64 * 100.0;
            let error_rate = stats.messages_failed as f64 / total_messages as f64 * 100.0;

            println!("  ๐Ÿ“Š Success rate: {:.1}%", success_rate);
            println!("  ๐Ÿ“Š Error rate: {:.1}%", error_rate);

            if stats.messages_processed > 0 {
                let avg_time = stats.total_processing_time / stats.messages_processed as u32;
                let throughput = stats.messages_processed as f64 / stats.total_processing_time.as_secs_f64();
                println!("  โฑ๏ธ  Average processing time: {:?}", avg_time);
                println!("  ๐Ÿš€ Throughput: {:.2} messages/sec", throughput);
            }
        }

        println!("  โณ Total processing time: {:?}", stats.total_processing_time);
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize logging
    env_logger::init();

    println!("๐Ÿš€ Queue Worker Pool Example");
    println!("============================");

    // Create Redis queue
    println!("๐Ÿ“ก Setting up Redis queue...");
    let queue = Arc::new(
        RedisQueueBuilder::new()
            .url("redis://localhost:6379")
            .queue_name("worker_pool_queue")
            .key_prefix("worker")
            .pool_size(20)
            .build()
            .await?
    );

    // Test connection
    if !queue.test_connection().await? {
        eprintln!("โŒ Failed to connect to Redis");
        return Ok(());
    }

    // Clear any existing messages
    println!("๐Ÿงน Clearing existing messages...");
    queue.purge().await?;

    // Populate queue with test messages
    println!("๐Ÿ“ค Populating queue with test messages...");
    let test_messages = vec![
        ("Send welcome email notification", QueuePriority::High),
        ("Generate daily report", QueuePriority::Normal),
        ("Process user cleanup task", QueuePriority::Low),
        ("Send critical alert", QueuePriority::Critical),
        ("Process payment notification", QueuePriority::High),
        ("Generate weekly analytics report", QueuePriority::Normal),
        ("Perform system maintenance cleanup", QueuePriority::Low),
        ("Handle emergency notification", QueuePriority::Critical),
        ("Process bulk email notification", QueuePriority::Normal),
        ("Simulate error-retryable scenario", QueuePriority::Normal),
        ("Simulate error-permanent scenario", QueuePriority::Low),
        ("Process slow report generation task", QueuePriority::Low),
    ];

    let mut message_ids = Vec::new();
    for (i, (payload, priority)) in test_messages.into_iter().enumerate() {
        let message = QueueMessage::builder()
            .id(format!("test-msg-{}", i + 1))
            .payload(payload)
            .priority(priority)
            .max_receive_count(3)
            .visibility_timeout(30)
            .build();

        match queue.enqueue(message).await {
            Ok(id) => {
                message_ids.push(id);
                println!("  โœ… Enqueued: {}", payload);
            }
            Err(e) => {
                eprintln!("  โŒ Failed to enqueue: {}", e);
            }
        }
    }

    println!("๐Ÿ“Š Enqueued {} messages", message_ids.len());
    println!("๐Ÿ“ˆ Queue size: {}", queue.size().await?);

    // Configure worker
    let worker_config = WorkerConfig {
        max_concurrent_tasks: 5,
        poll_interval: Duration::from_millis(50),
        max_retries: 2,
        visibility_timeout: 30,
        shutdown_timeout: Duration::from_secs(10),
    };

    // Create and start worker pool
    let worker = QueueWorker::new(queue, worker_config);

    // Start the worker pool
    worker.start().await?;

    println!("\n๐ŸŽ‰ Worker pool example completed!");
    Ok(())
}