celers 0.2.0

Celery-compatible distributed task queue for Rust (Facade crate)
Documentation
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
use crate::{Broker, SerializedTask};
use async_trait::async_trait;
use celers_core::broker::BrokerMessage;
use celers_core::error::CelersError;
use celers_core::task::TaskId;
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};

type Result<T> = std::result::Result<T, CelersError>;

/// Mock broker for testing
///
/// This broker stores tasks in memory and provides inspection capabilities
/// for testing task execution.
#[derive(Clone)]
pub struct MockBroker {
    queue: Arc<Mutex<VecDeque<SerializedTask>>>,
    published_tasks: Arc<Mutex<Vec<SerializedTask>>>,
}

impl MockBroker {
    /// Create a new mock broker
    pub fn new() -> Self {
        Self {
            queue: Arc::new(Mutex::new(VecDeque::new())),
            published_tasks: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Get the number of tasks in the queue
    pub fn queue_len(&self) -> usize {
        self.queue
            .lock()
            .expect("lock should not be poisoned")
            .len()
    }

    /// Get all published tasks
    pub fn published_tasks(&self) -> Vec<SerializedTask> {
        self.published_tasks
            .lock()
            .expect("lock should not be poisoned")
            .clone()
    }

    /// Clear all tasks
    pub fn clear(&self) {
        self.queue
            .lock()
            .expect("lock should not be poisoned")
            .clear();
        self.published_tasks
            .lock()
            .expect("lock should not be poisoned")
            .clear();
    }

    /// Push a task to the front of the queue (for testing)
    pub fn push_task(&self, task: SerializedTask) {
        self.queue
            .lock()
            .expect("lock should not be poisoned")
            .push_back(task);
    }
}

impl Default for MockBroker {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Broker for MockBroker {
    async fn enqueue(&self, task: SerializedTask) -> Result<TaskId> {
        let task_id = task.metadata.id;
        self.published_tasks
            .lock()
            .expect("lock should not be poisoned")
            .push(task.clone());
        self.queue
            .lock()
            .expect("lock should not be poisoned")
            .push_back(task);
        Ok(task_id)
    }

    async fn dequeue(&self) -> Result<Option<BrokerMessage>> {
        let task = self
            .queue
            .lock()
            .expect("lock should not be poisoned")
            .pop_front();
        Ok(task.map(BrokerMessage::new))
    }

    async fn ack(&self, _task_id: &TaskId, _receipt_handle: Option<&str>) -> Result<()> {
        Ok(())
    }

    async fn reject(
        &self,
        _task_id: &TaskId,
        _receipt_handle: Option<&str>,
        _requeue: bool,
    ) -> Result<()> {
        Ok(())
    }

    async fn queue_size(&self) -> Result<usize> {
        Ok(self
            .queue
            .lock()
            .expect("lock should not be poisoned")
            .len())
    }

    async fn cancel(&self, task_id: &TaskId) -> Result<bool> {
        let mut queue = self.queue.lock().expect("lock should not be poisoned");
        let original_len = queue.len();
        queue.retain(|t| &t.metadata.id != task_id);
        Ok(queue.len() < original_len)
    }
}

/// Task builder for testing
pub struct TaskBuilder {
    name: String,
    id: Option<String>,
    max_retries: u32,
    payload: Vec<u8>,
}

impl TaskBuilder {
    /// Create a new task builder
    pub fn new(task_name: &str) -> Self {
        Self {
            name: task_name.to_string(),
            id: None,
            max_retries: 0,
            payload: Vec::new(),
        }
    }

    /// Set task ID
    pub fn id(mut self, id: String) -> Self {
        self.id = Some(id);
        self
    }

    /// Set max retries
    pub fn max_retries(mut self, max_retries: u32) -> Self {
        self.max_retries = max_retries;
        self
    }

    /// Set payload
    pub fn payload(mut self, payload: Vec<u8>) -> Self {
        self.payload = payload;
        self
    }

    /// Build the task
    pub fn build(self) -> SerializedTask {
        use uuid::Uuid;

        let mut task = SerializedTask::new(self.name, self.payload);
        if let Some(id) = self.id {
            task.metadata.id = Uuid::parse_str(&id).unwrap_or_else(|_| Uuid::new_v4());
        }
        task.metadata.max_retries = self.max_retries;
        task
    }
}

/// Helper to create a simple test task
pub fn create_test_task(name: &str) -> SerializedTask {
    TaskBuilder::new(name).build()
}

/// Task debugger for inspecting task state and execution
pub struct TaskDebugger {
    task_history: Arc<Mutex<Vec<TaskDebugInfo>>>,
}

/// Debug information for a task
#[derive(Debug, Clone)]
pub struct TaskDebugInfo {
    /// Unique identifier for the task
    pub task_id: String,
    /// Name of the task
    pub task_name: String,
    /// Current state of the task
    pub state: String,
    /// Timestamp when this debug info was captured
    pub timestamp: std::time::SystemTime,
    /// Additional metadata about the task
    pub metadata: std::collections::HashMap<String, String>,
}

impl TaskDebugger {
    /// Create a new task debugger
    pub fn new() -> Self {
        Self {
            task_history: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Record task execution
    pub fn record_task(&self, task: &SerializedTask, state: &str) {
        let mut history = self
            .task_history
            .lock()
            .expect("lock should not be poisoned");
        history.push(TaskDebugInfo {
            task_id: task.metadata.id.to_string(),
            task_name: task.metadata.name.clone(),
            state: state.to_string(),
            timestamp: std::time::SystemTime::now(),
            metadata: std::collections::HashMap::new(),
        });
    }

    /// Get task history
    pub fn history(&self) -> Vec<TaskDebugInfo> {
        self.task_history
            .lock()
            .expect("lock should not be poisoned")
            .clone()
    }

    /// Clear history
    pub fn clear(&self) {
        self.task_history
            .lock()
            .expect("lock should not be poisoned")
            .clear();
    }

    /// Get tasks by state
    pub fn tasks_by_state(&self, state: &str) -> Vec<TaskDebugInfo> {
        self.task_history
            .lock()
            .unwrap()
            .iter()
            .filter(|info| info.state == state)
            .cloned()
            .collect()
    }

    /// Print task history in a formatted table
    pub fn print_history(&self) {
        let history = self.history();
        println!(
            "\n╔══════════════════════════════════════════════════════════════════════════════╗"
        );
        println!(
            "║                            Task Execution History                             ║"
        );
        println!(
            "╚══════════════════════════════════════════════════════════════════════════════╝\n"
        );

        for (idx, info) in history.iter().enumerate() {
            println!("Task #{}", idx + 1);
            println!("  ID:        {}", info.task_id);
            println!("  Name:      {}", info.task_name);
            println!("  State:     {}", info.state);
            println!("  Timestamp: {:?}", info.timestamp);
            if !info.metadata.is_empty() {
                println!("  Metadata:");
                for (key, value) in &info.metadata {
                    println!("    {}: {}", key, value);
                }
            }
            println!();
        }
    }
}

impl Default for TaskDebugger {
    fn default() -> Self {
        Self::new()
    }
}

/// Event tracker for debugging task events
pub struct EventTracker {
    events: Arc<Mutex<Vec<TrackedEvent>>>,
}

/// Tracked event information
#[derive(Debug, Clone)]
pub struct TrackedEvent {
    /// Type of the event (e.g., "task_received", "task_started", "task_completed")
    pub event_type: String,
    /// Optional task ID associated with this event
    pub task_id: Option<String>,
    /// Human-readable message describing the event
    pub message: String,
    /// Timestamp when this event occurred
    pub timestamp: std::time::SystemTime,
}

impl EventTracker {
    /// Create a new event tracker
    pub fn new() -> Self {
        Self {
            events: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Track an event
    pub fn track(&self, event_type: &str, task_id: Option<String>, message: String) {
        let mut events = self.events.lock().expect("lock should not be poisoned");
        events.push(TrackedEvent {
            event_type: event_type.to_string(),
            task_id,
            message,
            timestamp: std::time::SystemTime::now(),
        });
    }

    /// Get all events
    pub fn events(&self) -> Vec<TrackedEvent> {
        self.events
            .lock()
            .expect("lock should not be poisoned")
            .clone()
    }

    /// Get events by type
    pub fn events_by_type(&self, event_type: &str) -> Vec<TrackedEvent> {
        self.events
            .lock()
            .unwrap()
            .iter()
            .filter(|e| e.event_type == event_type)
            .cloned()
            .collect()
    }

    /// Clear events
    pub fn clear(&self) {
        self.events
            .lock()
            .expect("lock should not be poisoned")
            .clear();
    }

    /// Print events in a formatted table
    pub fn print_events(&self) {
        let events = self.events();
        println!(
            "\n╔══════════════════════════════════════════════════════════════════════════════╗"
        );
        println!(
            "║                              Event Log                                        ║"
        );
        println!(
            "╚══════════════════════════════════════════════════════════════════════════════╝\n"
        );

        for (idx, event) in events.iter().enumerate() {
            println!("Event #{}", idx + 1);
            println!("  Type:      {}", event.event_type);
            if let Some(ref task_id) = event.task_id {
                println!("  Task ID:   {}", task_id);
            }
            println!("  Message:   {}", event.message);
            println!("  Timestamp: {:?}", event.timestamp);
            println!();
        }
    }
}

impl Default for EventTracker {
    fn default() -> Self {
        Self::new()
    }
}

/// Performance profiler for debugging task execution time
pub struct PerformanceProfiler {
    measurements: Arc<Mutex<Vec<PerformanceMeasurement>>>,
}

/// Performance measurement data
#[derive(Debug, Clone)]
pub struct PerformanceMeasurement {
    /// Name of the operation being measured
    pub name: String,
    /// Duration of the operation in milliseconds
    pub duration_ms: u128,
    /// Timestamp when this measurement was taken
    pub timestamp: std::time::SystemTime,
    /// Additional metadata about the measurement
    pub metadata: std::collections::HashMap<String, String>,
}

impl PerformanceProfiler {
    /// Create a new performance profiler
    pub fn new() -> Self {
        Self {
            measurements: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Start a measurement
    pub fn start_measurement(&self, name: &str) -> MeasurementGuard {
        MeasurementGuard {
            name: name.to_string(),
            start: std::time::Instant::now(),
            profiler: self.clone(),
        }
    }

    /// Record a measurement
    fn record(&self, name: String, duration_ms: u128) {
        let mut measurements = self
            .measurements
            .lock()
            .expect("lock should not be poisoned");
        measurements.push(PerformanceMeasurement {
            name,
            duration_ms,
            timestamp: std::time::SystemTime::now(),
            metadata: std::collections::HashMap::new(),
        });
    }

    /// Get all measurements
    pub fn measurements(&self) -> Vec<PerformanceMeasurement> {
        self.measurements
            .lock()
            .expect("lock should not be poisoned")
            .clone()
    }

    /// Clear measurements
    pub fn clear(&self) {
        self.measurements
            .lock()
            .expect("lock should not be poisoned")
            .clear();
    }

    /// Get average duration for a measurement name
    pub fn average_duration(&self, name: &str) -> Option<u128> {
        let measurements = self
            .measurements
            .lock()
            .expect("lock should not be poisoned");
        let matching: Vec<_> = measurements
            .iter()
            .filter(|m| m.name == name)
            .map(|m| m.duration_ms)
            .collect();

        if matching.is_empty() {
            None
        } else {
            Some(matching.iter().sum::<u128>() / matching.len() as u128)
        }
    }

    /// Print performance summary
    pub fn print_summary(&self) {
        let measurements = self.measurements();
        println!(
            "\n╔══════════════════════════════════════════════════════════════════════════════╗"
        );
        println!(
            "║                          Performance Summary                                  ║"
        );
        println!(
            "╚══════════════════════════════════════════════════════════════════════════════╝\n"
        );

        // Group by name
        let mut grouped: std::collections::HashMap<String, Vec<u128>> =
            std::collections::HashMap::new();

        for m in measurements {
            grouped.entry(m.name).or_default().push(m.duration_ms);
        }

        for (name, durations) in grouped {
            let count = durations.len();
            let total: u128 = durations.iter().sum();
            let avg = total / count as u128;
            let min = *durations
                .iter()
                .min()
                .expect("collection validated to be non-empty");
            let max = *durations
                .iter()
                .max()
                .expect("collection validated to be non-empty");

            println!("{}", name);
            println!("  Count: {}", count);
            println!("  Avg:   {} ms", avg);
            println!("  Min:   {} ms", min);
            println!("  Max:   {} ms", max);
            println!("  Total: {} ms", total);
            println!();
        }
    }
}

impl Clone for PerformanceProfiler {
    fn clone(&self) -> Self {
        Self {
            measurements: Arc::clone(&self.measurements),
        }
    }
}

impl Default for PerformanceProfiler {
    fn default() -> Self {
        Self::new()
    }
}

/// RAII guard for performance measurements
pub struct MeasurementGuard {
    name: String,
    start: std::time::Instant,
    profiler: PerformanceProfiler,
}

impl Drop for MeasurementGuard {
    fn drop(&mut self) {
        let duration_ms = self.start.elapsed().as_millis();
        self.profiler.record(self.name.clone(), duration_ms);
    }
}

/// Queue inspector for debugging broker queues
pub struct QueueInspector {
    snapshots: Arc<Mutex<Vec<QueueSnapshot>>>,
}

/// Snapshot of queue state
#[derive(Debug, Clone)]
pub struct QueueSnapshot {
    /// Number of tasks in the queue at the time of snapshot
    pub queue_size: usize,
    /// Timestamp when this snapshot was taken
    pub timestamp: std::time::SystemTime,
    /// Additional metadata about the queue state
    pub metadata: std::collections::HashMap<String, String>,
}

impl QueueInspector {
    /// Create a new queue inspector
    pub fn new() -> Self {
        Self {
            snapshots: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Take a snapshot of the queue
    pub async fn snapshot(&self, broker: &MockBroker) {
        let size = broker.queue_len();
        let mut snapshots = self.snapshots.lock().expect("lock should not be poisoned");
        snapshots.push(QueueSnapshot {
            queue_size: size,
            timestamp: std::time::SystemTime::now(),
            metadata: std::collections::HashMap::new(),
        });
    }

    /// Get all snapshots
    pub fn snapshots(&self) -> Vec<QueueSnapshot> {
        self.snapshots
            .lock()
            .expect("lock should not be poisoned")
            .clone()
    }

    /// Clear snapshots
    pub fn clear(&self) {
        self.snapshots
            .lock()
            .expect("lock should not be poisoned")
            .clear();
    }

    /// Print queue history
    pub fn print_history(&self) {
        let snapshots = self.snapshots();
        println!(
            "\n╔══════════════════════════════════════════════════════════════════════════════╗"
        );
        println!(
            "║                            Queue Size History                                 ║"
        );
        println!(
            "╚══════════════════════════════════════════════════════════════════════════════╝\n"
        );

        for (idx, snapshot) in snapshots.iter().enumerate() {
            println!("Snapshot #{}", idx + 1);
            println!("  Queue Size: {}", snapshot.queue_size);
            println!("  Timestamp:  {:?}", snapshot.timestamp);
            println!();
        }
    }
}

impl Default for QueueInspector {
    fn default() -> Self {
        Self::new()
    }
}