rust-task-queue 0.1.5

Production-ready Redis task queue with intelligent auto-scaling, Actix Web integration, and enterprise-grade observability for high-performance async Rust applications.
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
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use std::sync::Arc;
use uuid::Uuid;

pub type TaskId = Uuid;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskMetadata {
    pub id: TaskId,
    pub name: String,
    pub created_at: DateTime<Utc>,
    pub attempts: u32,
    pub max_retries: u32,
    pub timeout_seconds: u64,
}

impl Default for TaskMetadata {
    fn default() -> Self {
        Self {
            id: Uuid::new_v4(),
            name: "unknown".to_string(),
            created_at: Utc::now(),
            attempts: 0,
            max_retries: 3,
            timeout_seconds: 300,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskWrapper {
    pub metadata: TaskMetadata,
    pub payload: Vec<u8>,
}

/// Enhanced Task trait with improved type safety and async characteristics
#[async_trait]
pub trait Task: Send + Sync + Serialize + for<'de> Deserialize<'de> + Debug {
    /// Execute the task with comprehensive error handling
    async fn execute(&self) -> TaskResult;

    /// Task identifier for registration and execution
    fn name(&self) -> &str;

    /// Maximum retry attempts (default: 3)
    fn max_retries(&self) -> u32 {
        3
    }

    /// Task timeout in seconds (default: 300s/5min)
    fn timeout_seconds(&self) -> u64 {
        300
    }

    /// Task priority level (default: Normal)
    fn priority(&self) -> TaskPriority {
        TaskPriority::Normal
    }

    /// Estimate task resource requirements for better scheduling
    fn resource_requirements(&self) -> TaskResourceRequirements {
        TaskResourceRequirements::default()
    }

    /// Custom retry delay strategy
    fn retry_delay_strategy(&self) -> RetryStrategy {
        RetryStrategy::ExponentialBackoff {
            base_delay_ms: 1000,
            max_delay_ms: 60000,
            multiplier: 2.0,
        }
    }
}

/// Task priority levels for queue ordering
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum TaskPriority {
    Low = 0,
    Normal = 1,
    High = 2,
    Critical = 3,
}

/// Resource requirements for task execution planning
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskResourceRequirements {
    /// Expected memory usage in bytes
    pub memory_bytes: Option<u64>,
    /// Expected CPU intensity (0.0 to 1.0)
    pub cpu_intensity: Option<f32>,
    /// Expected I/O operations per second
    pub io_ops_per_second: Option<u32>,
    /// Network bandwidth requirements in bytes/sec
    pub network_bandwidth_bytes: Option<u64>,
}

impl Default for TaskResourceRequirements {
    fn default() -> Self {
        Self {
            memory_bytes: None,
            cpu_intensity: Some(0.1), // Low CPU by default
            io_ops_per_second: None,
            network_bandwidth_bytes: None,
        }
    }
}

/// Retry strategy configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RetryStrategy {
    /// Fixed delay between retries
    FixedDelay { delay_ms: u64 },
    /// Exponential backoff with jitter
    ExponentialBackoff {
        base_delay_ms: u64,
        max_delay_ms: u64,
        multiplier: f64,
    },
    /// Custom retry intervals
    CustomIntervals { intervals_ms: Vec<u64> },
    /// No retries
    NoRetry,
}

/// Result type for task execution
pub type TaskResult = Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>>;

/// Future type for task execution
pub type TaskFuture = std::pin::Pin<Box<dyn std::future::Future<Output = TaskResult> + Send>>;

/// Type-erased task executor function
pub type TaskExecutor = Arc<dyn Fn(Vec<u8>) -> TaskFuture + Send + Sync>;

/// Registration information for automatic task registration via inventory
#[cfg(feature = "auto-register")]
pub struct TaskRegistration {
    pub type_name: &'static str,
    pub register_fn: fn(&TaskRegistry) -> Result<(), Box<dyn std::error::Error + Send + Sync>>,
}

#[cfg(feature = "auto-register")]
inventory::collect!(TaskRegistration);

/// Task registry for mapping task names to executors
pub struct TaskRegistry {
    executors: DashMap<String, TaskExecutor>,
}

impl TaskRegistry {
    pub fn new() -> Self {
        Self {
            executors: DashMap::new(),
        }
    }

    /// Create a new registry with all automatically registered tasks
    #[cfg(feature = "auto-register")]
    pub fn with_auto_registered() -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
        let registry = Self::new();
        registry.auto_register_tasks()?;
        Ok(registry)
    }

    /// Create a new registry with all automatically registered tasks and configuration
    #[cfg(feature = "auto-register")]
    pub fn with_auto_registered_and_config(
        config: Option<&crate::config::AutoRegisterConfig>,
    ) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
        let registry = Self::new();
        registry.auto_register_tasks_with_config(config)?;
        Ok(registry)
    }

    /// Register all tasks that have been submitted via the inventory pattern
    #[cfg(feature = "auto-register")]
    pub fn auto_register_tasks(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        self.auto_register_tasks_with_config(None)
    }

    /// Register all tasks with optional configuration
    #[cfg(feature = "auto-register")]
    pub fn auto_register_tasks_with_config(
        &self,
        _config: Option<&crate::config::AutoRegisterConfig>,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        #[cfg(feature = "tracing")]
        tracing::info!("Auto-registering tasks...");

        let mut registered_count = 0;
        let mut errors = Vec::new();

        // Register tasks from the inventory pattern (compile-time registered)
        for registration in inventory::iter::<TaskRegistration> {
            #[cfg(feature = "tracing")]
            tracing::debug!("Auto-registering task type: {}", registration.type_name);

            match (registration.register_fn)(self) {
                Ok(()) => {
                    registered_count += 1;
                    #[cfg(feature = "tracing")]
                    tracing::debug!(
                        "Successfully registered task type: {}",
                        registration.type_name
                    );
                }
                Err(e) => {
                    #[cfg(feature = "tracing")]
                    tracing::error!(
                        "Failed to register task type {}: {}",
                        registration.type_name,
                        e
                    );
                    errors.push(format!(
                        "Failed to register {}: {}",
                        registration.type_name, e
                    ));
                }
            }
        }

        if !errors.is_empty() {
            return Err(format!("Task registration errors: {}", errors.join(", ")).into());
        }

        #[cfg(feature = "tracing")]
        tracing::info!("Auto-registered {} task types", registered_count);

        Ok(())
    }

    /// Register a task type with explicit name
    pub fn register_with_name<T>(
        &self,
        task_name: &str,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
    where
        T: Task + 'static,
    {
        let executor: TaskExecutor = Arc::new(move |payload| {
            Box::pin(async move {
                match rmp_serde::from_slice::<T>(&payload) {
                    Ok(task) => task.execute().await,
                    Err(e) => Err(format!("Failed to deserialize task: {}", e).into()),
                }
            })
        });

        self.executors.insert(task_name.to_string(), executor);

        Ok(())
    }

    /// Execute a task by name
    pub async fn execute(&self, task_name: &str, payload: Vec<u8>) -> TaskResult {
        let executor = self.executors.get(task_name).map(|e| e.clone());

        if let Some(executor) = executor {
            executor(payload).await
        } else {
            Err(format!("Unknown task type: {}", task_name).into())
        }
    }

    /// Get list of registered task names
    pub fn registered_tasks(&self) -> Vec<String> {
        self.executors
            .iter()
            .map(|entry| entry.key().clone())
            .collect()
    }
}

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

// Macro to make task registration easier
#[macro_export]
macro_rules! manual_register_task {
    ($registry:expr, $task_type:ty) => {{
        // We need a way to get the task name from the type
        // This requires the task to implement Default temporarily
        let temp_instance = <$task_type as Default>::default();
        let task_name = temp_instance.name().to_string();
        $registry.register_with_name::<$task_type>(&task_name)
    }};
}

// Helper macro for registering multiple tasks at once
#[macro_export]
macro_rules! register_tasks {
    ($registry:expr, $($task_type:ty),+ $(,)?) => {
        {
            let mut results = Vec::new();
            $(
                results.push($crate::manual_register_task!($registry, $task_type));
            )+

            // Return the first error if any, otherwise Ok
            for result in results {
                if let Err(e) = result {
                    return Err(e);
                }
            }
            Ok::<(), Box<dyn std::error::Error + Send + Sync>>(())
        }
    };
}

// Alternative macro that doesn't require Default trait
#[macro_export]
macro_rules! register_task_with_name {
    ($registry:expr, $task_type:ty, $name:expr) => {
        $registry.register_with_name::<$task_type>($name)
    };
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::{Deserialize, Serialize};

    #[derive(Debug, Serialize, Deserialize, Clone, Default)]
    struct TestTask {
        pub data: String,
        pub should_fail: bool,
    }

    #[async_trait]
    impl Task for TestTask {
        async fn execute(&self) -> TaskResult {
            if self.should_fail {
                return Err("Task intentionally failed".into());
            }

            // Simulate some work
            tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

            #[derive(Serialize)]
            struct Response {
                status: String,
                processed_data: String,
            }

            let response = Response {
                status: "completed".to_string(),
                processed_data: format!("Processed: {}", self.data),
            };

            Ok(rmp_serde::to_vec(&response)?)
        }

        fn name(&self) -> &str {
            "test_task"
        }

        fn max_retries(&self) -> u32 {
            2
        }

        fn timeout_seconds(&self) -> u64 {
            30
        }
    }

    #[tokio::test]
    async fn test_task_registry_creation() {
        let registry = TaskRegistry::new();
        assert_eq!(registry.registered_tasks().len(), 0);
    }

    #[tokio::test]
    async fn test_task_registration() {
        let registry = TaskRegistry::new();

        // Register a task
        registry
            .register_with_name::<TestTask>("test_task")
            .expect("Failed to register task");

        let tasks = registry.registered_tasks();
        assert_eq!(tasks.len(), 1);
        assert!(tasks.contains(&"test_task".to_string()));
    }

    #[tokio::test]
    async fn test_task_execution() {
        let registry = TaskRegistry::new();
        registry
            .register_with_name::<TestTask>("test_task")
            .expect("Failed to register task");

        let task = TestTask {
            data: "Hello, World!".to_string(),
            should_fail: false,
        };

        let payload = rmp_serde::to_vec(&task).expect("Failed to serialize task");
        let result = registry.execute("test_task", payload).await;

        assert!(result.is_ok());
        let response_data = result.unwrap();
        assert!(!response_data.is_empty());

        // Verify the response contains expected data
        // Since we're using MessagePack, we need to deserialize it properly
        #[derive(serde::Deserialize)]
        struct Response {
            status: String,
            processed_data: String,
        }

        let response: Response =
            rmp_serde::from_slice(&response_data).expect("Failed to deserialize response");
        assert_eq!(response.status, "completed");
        assert!(response.processed_data.contains("Hello, World!"));
    }

    #[tokio::test]
    async fn test_task_execution_failure() {
        let registry = TaskRegistry::new();
        registry
            .register_with_name::<TestTask>("test_task")
            .expect("Failed to register task");

        let task = TestTask {
            data: "This will fail".to_string(),
            should_fail: true,
        };

        let payload = rmp_serde::to_vec(&task).expect("Failed to serialize task");
        let result = registry.execute("test_task", payload).await;

        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("intentionally failed"));
    }

    #[tokio::test]
    async fn test_unknown_task_execution() {
        let registry = TaskRegistry::new();

        let result = registry.execute("unknown_task", vec![1, 2, 3]).await;

        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Unknown task type"));
    }

    #[tokio::test]
    async fn test_task_metadata_default() {
        let metadata = TaskMetadata::default();

        assert_eq!(metadata.name, "unknown");
        assert_eq!(metadata.attempts, 0);
        assert_eq!(metadata.max_retries, 3);
        assert_eq!(metadata.timeout_seconds, 300);
    }

    #[tokio::test]
    async fn test_task_wrapper_serialization() {
        let metadata = TaskMetadata {
            id: TaskId::new_v4(),
            name: "test_task".to_string(),
            created_at: chrono::Utc::now(),
            attempts: 1,
            max_retries: 3,
            timeout_seconds: 300,
        };

        let wrapper = TaskWrapper {
            metadata: metadata.clone(),
            payload: vec![1, 2, 3, 4],
        };

        // Test serialization
        let serialized = rmp_serde::to_vec(&wrapper).expect("Failed to serialize wrapper");
        assert!(!serialized.is_empty());

        // Test deserialization
        let deserialized: TaskWrapper =
            rmp_serde::from_slice(&serialized).expect("Failed to deserialize wrapper");

        assert_eq!(deserialized.metadata.id, metadata.id);
        assert_eq!(deserialized.metadata.name, metadata.name);
        assert_eq!(deserialized.payload, vec![1, 2, 3, 4]);
    }

    #[tokio::test]
    async fn test_multiple_task_registration() {
        let registry = TaskRegistry::new();

        // Register multiple tasks
        registry
            .register_with_name::<TestTask>("task1")
            .expect("Failed to register task1");
        registry
            .register_with_name::<TestTask>("task2")
            .expect("Failed to register task2");

        let tasks = registry.registered_tasks();
        assert_eq!(tasks.len(), 2);
        assert!(tasks.contains(&"task1".to_string()));
        assert!(tasks.contains(&"task2".to_string()));
    }

    #[tokio::test]
    async fn test_task_registry_concurrent_access() {
        let registry = Arc::new(TaskRegistry::new());
        registry
            .register_with_name::<TestTask>("test_task")
            .expect("Failed to register task");

        let task = TestTask {
            data: "Concurrent test".to_string(),
            should_fail: false,
        };
        let payload = rmp_serde::to_vec(&task).expect("Failed to serialize task");

        // Execute multiple tasks concurrently
        let mut handles = Vec::new();
        for i in 0..10 {
            let registry_clone = Arc::clone(&registry);
            let payload_clone = payload.clone();
            let handle = tokio::spawn(async move {
                let result = registry_clone.execute("test_task", payload_clone).await;
                assert!(result.is_ok(), "Task {} failed", i);
            });
            handles.push(handle);
        }

        // Wait for all tasks to complete
        for handle in handles {
            handle.await.expect("Task execution failed");
        }
    }
}