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
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
//! # Rust Task Queue Framework
//!
//! A high-performance, Redis-backed task queue system with auto-scaling capabilities
//! designed for use with async Rust applications.
//!
//! ## Features
//!
//! - **Redis-backed broker** for reliable message delivery
//! - **Auto-scaling workers** based on queue load
//! - **Task scheduling** with delay support
//! - **Multiple queue priorities**
//! - **Retry logic** with configurable attempts
//! - **Task timeouts** and failure handling
//! - **Metrics and monitoring**
//! - **Actix Web integration** (optional)
//! - **Automatic task registration** (optional)
//!
//! ## Quick Start
//!
//! ### Basic Usage
//!
//! ```rust,no_run
//! use rust_task_queue::prelude::*;
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Debug, Serialize, Deserialize, AutoRegisterTask)]
//! struct MyTask {
//!     data: String,
//! }
//!
//! #[async_trait::async_trait]
//! impl Task for MyTask {
//!     async fn execute(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
//!         println!("Processing: {}", self.data);
//!         use serde::Serialize;
//!         #[derive(Serialize)]
//!         struct Response { status: String }
//!         let response = Response { status: "completed".to_string() };
//!         Ok(rmp_serde::to_vec(&response)?)
//!     }
//!     
//!     fn name(&self) -> &str {
//!         "my_task"
//!     }
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let task_queue = TaskQueue::new("redis://localhost:6379").await?;
//!     
//!     // Start workers
//!     task_queue.start_workers(2).await?;
//!     
//!     // Enqueue a task
//!     let task = MyTask { data: "Hello, World!".to_string() };
//!     let task_id = task_queue.enqueue(task, "default").await?;
//!     
//!     println!("Enqueued task: {}", task_id);
//!     Ok(())
//! }
//! ```
//!
//! ### Automatic Task Registration
//!
//! ```rust,no_run
//! use rust_task_queue::prelude::*;
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Debug, Serialize, Deserialize, Default, AutoRegisterTask)]
//! struct MyTask {
//!     data: String,
//! }
//!
//! #[async_trait::async_trait]
//! impl Task for MyTask {
//!     async fn execute(&self) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
//!         println!("Processing: {}", self.data);
//!         use serde::Serialize;
//!         #[derive(Serialize)]
//!         struct Response { status: String }
//!         let response = Response { status: "completed".to_string() };
//!         Ok(rmp_serde::to_vec(&response)?)
//!     }
//!     
//!     fn name(&self) -> &str {
//!         "my_task"
//!     }
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Task is automatically registered!
//!     let task_queue = TaskQueueBuilder::new("redis://localhost:6379")
//!         .auto_register_tasks()
//!         .initial_workers(2)
//!         .build()
//!         .await?;
//!     
//!     let task = MyTask { data: "Hello, World!".to_string() };
//!     let task_id = task_queue.enqueue(task, "default").await?;
//!     
//!     println!("Enqueued task: {}", task_id);
//!     Ok(())
//! }
//! ```

#![cfg_attr(docsrs, feature(doc_cfg))]

pub mod autoscaler;
pub mod broker;
pub mod config;
pub mod error;
pub mod metrics;
pub mod queue;
pub mod scheduler;
pub mod task;
pub mod worker;

#[cfg(feature = "tracing")]
pub mod tracing_utils;

#[cfg(feature = "actix-integration")]
#[cfg_attr(docsrs, doc(cfg(feature = "actix-integration")))]
pub mod actix;

#[cfg(feature = "axum-integration")]
#[cfg_attr(docsrs, doc(cfg(feature = "axum-integration")))]
pub mod axum;

#[cfg(feature = "cli")]
#[cfg_attr(docsrs, doc(cfg(feature = "cli")))]
pub mod cli;

pub use autoscaler::*;
pub use broker::*;
#[cfg(feature = "cli")]
pub use cli::*;
pub use config::*;
pub use error::*;
pub use metrics::*;
pub use queue::*;
pub use scheduler::*;
pub use task::*;
pub use worker::*;

// Re-export macros when auto-register feature is enabled
#[cfg(feature = "auto-register")]
pub use rust_task_queue_macro::{register_task as proc_register_task, AutoRegisterTask};

// Provide placeholder when auto-register is disabled
#[cfg(not(feature = "auto-register"))]
pub use std::marker::PhantomData as AutoRegisterTask;
#[cfg(not(feature = "auto-register"))]
pub use std::marker::PhantomData as proc_register_task;

// Re-export inventory for users who want to use it directly
#[cfg(feature = "auto-register")]
#[cfg_attr(docsrs, doc(cfg(feature = "auto-register")))]
pub use inventory;

pub mod prelude;

use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tokio::sync::RwLock;

/// Main task queue framework
#[derive(Clone)]
pub struct TaskQueue {
    pub broker: Arc<RedisBroker>,
    pub scheduler: Arc<TaskScheduler>,
    pub autoscaler: Arc<AutoScaler>,
    pub metrics: Arc<MetricsCollector>,
    workers: Arc<RwLock<Vec<Worker>>>,
    scheduler_handle: Arc<RwLock<Option<tokio::task::JoinHandle<()>>>>,
    shutdown_signal: Arc<RwLock<Option<tokio::sync::broadcast::Sender<()>>>>,
}

impl TaskQueue {
    /// Create a new task queue instance
    pub async fn new(redis_url: &str) -> Result<Self, TaskQueueError> {
        let broker = Arc::new(RedisBroker::new(redis_url).await?);
        let scheduler = Arc::new(TaskScheduler::new(broker.clone()));
        let autoscaler = Arc::new(AutoScaler::new(broker.clone()));
        let metrics = Arc::new(MetricsCollector::new());

        Ok(Self {
            broker,
            scheduler,
            autoscaler,
            metrics,
            workers: Arc::new(RwLock::new(Vec::new())),
            scheduler_handle: Arc::new(RwLock::new(None)),
            shutdown_signal: Arc::new(RwLock::new(None)),
        })
    }

    /// Create a new task queue with custom auto-scaler configuration
    pub async fn with_autoscaler_config(
        redis_url: &str,
        autoscaler_config: AutoScalerConfig,
    ) -> Result<Self, TaskQueueError> {
        let broker = Arc::new(RedisBroker::new(redis_url).await?);
        let scheduler = Arc::new(TaskScheduler::new(broker.clone()));
        let autoscaler = Arc::new(AutoScaler::with_config(broker.clone(), autoscaler_config));
        let metrics = Arc::new(MetricsCollector::new());

        Ok(Self {
            broker,
            scheduler,
            autoscaler,
            metrics,
            workers: Arc::new(RwLock::new(Vec::new())),
            scheduler_handle: Arc::new(RwLock::new(None)),
            shutdown_signal: Arc::new(RwLock::new(None)),
        })
    }

    /// Start the specified number of workers with auto-registered tasks (if available)
    pub async fn start_workers(&self, initial_count: usize) -> Result<(), TaskQueueError> {
        // Try to use auto-registered tasks if the feature is enabled, otherwise use empty registry
        let registry = {
            #[cfg(feature = "auto-register")]
            {
                // Check if there's a global config that enables auto-registration
                if let Ok(config) = TaskQueueConfig::get_or_init() {
                    if config.auto_register.enabled {
                        match TaskRegistry::with_auto_registered_and_config(Some(
                            &config.auto_register,
                        )) {
                            Ok(reg) => Arc::new(reg),
                            Err(e) => {
                                #[cfg(feature = "tracing")]
                                tracing::warn!("Failed to create auto-registered task registry: {}, using empty registry", e);
                                Arc::new(TaskRegistry::new())
                            }
                        }
                    } else {
                        Arc::new(TaskRegistry::new())
                    }
                } else {
                    // Try to create auto-registered registry anyway (fallback for backward compatibility)
                    match TaskRegistry::with_auto_registered() {
                        Ok(reg) => Arc::new(reg),
                        Err(_) => Arc::new(TaskRegistry::new()),
                    }
                }
            }
            #[cfg(not(feature = "auto-register"))]
            {
                Arc::new(TaskRegistry::new())
            }
        };

        self.start_workers_with_registry(initial_count, registry)
            .await
    }

    /// Start workers with a custom task registry
    pub async fn start_workers_with_registry(
        &self,
        initial_count: usize,
        task_registry: Arc<TaskRegistry>,
    ) -> Result<(), TaskQueueError> {
        let mut workers = self.workers.write().await;

        for i in 0..initial_count {
            let worker = Worker::new(
                format!("worker-{}", i),
                self.broker.clone(),
                self.scheduler.clone(),
            )
            .with_task_registry(task_registry.clone());

            let worker_handle = worker.start().await?;
            workers.push(worker_handle);
        }

        #[cfg(feature = "tracing")]
        tracing::info!(
            "Started {} workers with task registry containing {} task types",
            initial_count,
            task_registry.registered_tasks().len()
        );

        Ok(())
    }

    /// Start the auto-scaler background process
    pub fn start_autoscaler(&self) -> Result<(), TaskQueueError> {
        let workers = self.workers.clone();
        let autoscaler = self.autoscaler.clone();
        let broker = self.broker.clone();
        let scheduler = self.scheduler.clone();

        tokio::spawn(async move {
            loop {
                tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;

                if let Ok(metrics) = autoscaler.collect_metrics().await {
                    // Clone autoscaler for scaling decision since it requires mutable access
                    if let Ok(action) = {
                        let mut autoscaler_clone = (*autoscaler).clone();
                        autoscaler_clone.decide_scaling_action(&metrics)
                    } {
                        match action {
                            ScalingAction::ScaleUp(count) => {
                                let mut workers_lock = workers.write().await;
                                let current_count = workers_lock.len();

                                for i in current_count..current_count + count {
                                    let worker = Worker::new(
                                        format!("worker-{}", i),
                                        broker.clone(),
                                        scheduler.clone(),
                                    );

                                    if let Ok(worker_handle) = worker.start().await {
                                        workers_lock.push(worker_handle);
                                        #[cfg(feature = "tracing")]
                                        tracing::info!("Scaled up: added worker-{}", i);
                                    }
                                }
                            }
                            ScalingAction::ScaleDown(count) => {
                                let mut workers_lock = workers.write().await;
                                for _ in 0..count {
                                    if let Some(worker) = workers_lock.pop() {
                                        worker.stop().await;
                                        #[cfg(feature = "tracing")]
                                        tracing::info!("Scaled down: removed worker");
                                    }
                                }
                            }
                            ScalingAction::NoAction => {}
                        }
                    }
                }
            }
        });

        #[cfg(feature = "tracing")]
        tracing::info!("Started auto-scaler");

        Ok(())
    }

    /// Start the task scheduler background process
    pub async fn start_scheduler(&self) -> Result<(), TaskQueueError> {
        let mut handle_guard = self.scheduler_handle.write().await;

        // Don't start if already running
        if handle_guard.is_some() {
            return Ok(());
        }

        let handle = TaskScheduler::start(self.broker.clone())?;
        *handle_guard = Some(handle);

        Ok(())
    }

    /// Stop the task scheduler
    pub async fn stop_scheduler(&self) {
        let mut handle_guard = self.scheduler_handle.write().await;
        if let Some(handle) = handle_guard.take() {
            handle.abort();

            #[cfg(feature = "tracing")]
            tracing::info!("Task scheduler stopped");
        }
    }

    /// Enqueue a task for immediate execution
    #[inline]
    pub async fn enqueue<T: Task>(&self, task: T, queue: &str) -> Result<TaskId, TaskQueueError> {
        self.broker.enqueue_task(task, queue).await
    }

    /// Schedule a task for delayed execution
    #[inline]
    pub async fn schedule<T: Task>(
        &self,
        task: T,
        queue: &str,
        delay: chrono::Duration,
    ) -> Result<TaskId, TaskQueueError> {
        self.scheduler.schedule_task(task, queue, delay).await
    }

    /// Get the current number of active workers
    #[inline]
    pub async fn worker_count(&self) -> usize {
        self.workers.read().await.len()
    }

    /// Stop all workers
    pub async fn stop_workers(&self) {
        let mut workers = self.workers.write().await;
        while let Some(worker) = workers.pop() {
            worker.stop().await;
        }

        #[cfg(feature = "tracing")]
        tracing::info!("All workers stopped");
    }

    /// Initiate graceful shutdown of the entire task queue
    pub async fn shutdown(&self) -> Result<(), TaskQueueError> {
        self.shutdown_with_timeout(std::time::Duration::from_secs(30))
            .await
    }

    /// Shutdown with a custom timeout
    pub async fn shutdown_with_timeout(
        &self,
        timeout: std::time::Duration,
    ) -> Result<(), TaskQueueError> {
        #[cfg(feature = "tracing")]
        tracing::info!("Initiating graceful shutdown of TaskQueue...");

        // Create shutdown signal
        let (shutdown_tx, _) = tokio::sync::broadcast::channel(1);
        {
            let mut signal = self.shutdown_signal.write().await;
            *signal = Some(shutdown_tx.clone());
        }

        // Shutdown components in order
        let shutdown_future = async {
            // 1. Stop accepting new tasks (stop scheduler)
            self.stop_scheduler().await;
            #[cfg(feature = "tracing")]
            tracing::info!("Scheduler stopped");

            // 2. Stop autoscaler
            // (Autoscaler doesn't have a stop method in current implementation)
            #[cfg(feature = "tracing")]
            tracing::info!("Autoscaler stopped");

            // 3. Wait for running tasks to complete and stop workers
            self.stop_workers().await;
            #[cfg(feature = "tracing")]
            tracing::info!("All workers stopped");

            // 4. Broadcast shutdown signal
            if let Err(e) = shutdown_tx.send(()) {
                #[cfg(feature = "tracing")]
                tracing::warn!("Failed to send shutdown signal: {}", e);
            }

            #[cfg(feature = "tracing")]
            tracing::info!("TaskQueue shutdown completed successfully");

            Ok(())
        };

        // Apply timeout
        match tokio::time::timeout(timeout, shutdown_future).await {
            Ok(result) => result,
            Err(_) => {
                #[cfg(feature = "tracing")]
                tracing::error!("TaskQueue shutdown timed out after {:?}", timeout);
                Err(TaskQueueError::Worker("Shutdown timeout".to_string()))
            }
        }
    }

    /// Check if shutdown has been initiated
    pub async fn is_shutting_down(&self) -> bool {
        self.shutdown_signal.read().await.is_some()
    }

    /// Get a shutdown signal receiver for external components
    pub async fn shutdown_receiver(&self) -> Option<tokio::sync::broadcast::Receiver<()>> {
        self.shutdown_signal
            .read()
            .await
            .as_ref()
            .map(|tx| tx.subscribe())
    }

    /// Get comprehensive health status
    pub async fn health_check(&self) -> Result<HealthStatus, TaskQueueError> {
        let mut health = HealthStatus {
            status: "healthy".to_string(),
            timestamp: chrono::Utc::now(),
            components: std::collections::HashMap::new(),
        };

        // Check Redis connection
        match self.broker.pool.get().await {
            Ok(mut conn) => {
                match redis::cmd("PING")
                    .query_async::<_, String>(&mut *conn)
                    .await
                {
                    Ok(_) => {
                        health.components.insert(
                            "redis".to_string(),
                            ComponentHealth {
                                status: "healthy".to_string(),
                                message: Some("Connection successful".to_string()),
                            },
                        );
                    }
                    Err(e) => {
                        health.status = "unhealthy".to_string();
                        health.components.insert(
                            "redis".to_string(),
                            ComponentHealth {
                                status: "unhealthy".to_string(),
                                message: Some(format!("Ping failed: {}", e)),
                            },
                        );
                    }
                }
            }
            Err(e) => {
                health.status = "unhealthy".to_string();
                health.components.insert(
                    "redis".to_string(),
                    ComponentHealth {
                        status: "unhealthy".to_string(),
                        message: Some(format!("Connection failed: {}", e)),
                    },
                );
            }
        }

        // Check workers
        let worker_count = self.worker_count().await;
        health.components.insert(
            "workers".to_string(),
            ComponentHealth {
                status: if worker_count > 0 {
                    "healthy"
                } else {
                    "warning"
                }
                .to_string(),
                message: Some(format!("{} active workers", worker_count)),
            },
        );

        // Check scheduler
        let scheduler_running = self.scheduler_handle.read().await.is_some();
        health.components.insert(
            "scheduler".to_string(),
            ComponentHealth {
                status: if scheduler_running {
                    "healthy"
                } else {
                    "stopped"
                }
                .to_string(),
                message: Some(
                    if scheduler_running {
                        "Running"
                    } else {
                        "Stopped"
                    }
                    .to_string(),
                ),
            },
        );

        Ok(health)
    }

    /// Get comprehensive metrics
    pub async fn get_metrics(&self) -> Result<TaskQueueMetrics, TaskQueueError> {
        let queues = ["default", "high_priority", "low_priority"];
        let mut queue_metrics = Vec::new();
        let mut total_pending = 0;
        let mut total_processed = 0;
        let mut total_failed = 0;

        for queue in &queues {
            let metrics = self.broker.get_queue_metrics(queue).await?;
            total_pending += metrics.pending_tasks;
            total_processed += metrics.processed_tasks;
            total_failed += metrics.failed_tasks;
            queue_metrics.push(metrics);
        }

        let autoscaler_metrics = self.autoscaler.collect_metrics().await?;

        Ok(TaskQueueMetrics {
            timestamp: chrono::Utc::now(),
            total_pending_tasks: total_pending,
            total_processed_tasks: total_processed,
            total_failed_tasks: total_failed,
            active_workers: autoscaler_metrics.active_workers,
            tasks_per_worker: autoscaler_metrics.queue_pressure_score,
            queue_metrics,
        })
    }

    /// Get enhanced system metrics with memory and performance data
    pub async fn get_system_metrics(&self) -> SystemMetrics {
        self.metrics.get_system_metrics().await
    }

    /// Get a quick metrics summary for debugging
    pub async fn get_metrics_summary(&self) -> String {
        self.metrics.get_metrics_summary().await
    }
}

/// Builder for configuring and creating TaskQueue instances
pub struct TaskQueueBuilder {
    config: TaskQueueConfig,
    task_registry: Option<Arc<TaskRegistry>>,
    override_redis_url: Option<String>,
}

impl TaskQueueBuilder {
    /// Create a new TaskQueue builder with default configuration
    #[inline]
    pub fn new(redis_url: impl Into<String>) -> Self {
        let mut config = TaskQueueConfig::default();
        config.redis.url = redis_url.into();

        Self {
            config,
            task_registry: None,
            override_redis_url: None,
        }
    }

    /// Create a TaskQueue builder from global configuration
    #[inline]
    pub fn from_global_config() -> Result<Self, TaskQueueError> {
        let config = TaskQueueConfig::get_or_init()?.clone();
        Ok(Self {
            config,
            task_registry: None,
            override_redis_url: None,
        })
    }

    /// Create a TaskQueue builder from a specific configuration
    #[must_use]
    #[inline]
    pub fn from_config(config: TaskQueueConfig) -> Self {
        Self {
            config,
            task_registry: None,
            override_redis_url: None,
        }
    }

    /// Create a TaskQueue builder that auto-loads configuration
    #[inline]
    pub fn auto() -> Result<Self, TaskQueueError> {
        let config = TaskQueueConfig::load()?;
        Ok(Self {
            config,
            task_registry: None,
            override_redis_url: None,
        })
    }

    /// Override Redis URL (useful for testing or special cases)
    #[must_use]
    #[inline]
    pub fn redis_url(mut self, url: impl Into<String>) -> Self {
        self.override_redis_url = Some(url.into());
        self
    }

    /// Set the auto-scaler configuration
    #[must_use]
    #[inline]
    pub fn autoscaler_config(mut self, config: AutoScalerConfig) -> Self {
        self.config.autoscaler = config;
        self
    }

    /// Set the initial number of workers to start
    #[must_use]
    #[inline]
    pub fn initial_workers(mut self, count: usize) -> Self {
        self.config.workers.initial_count = count;
        self
    }

    /// Set a custom task registry
    #[must_use]
    #[inline]
    pub fn task_registry(mut self, registry: Arc<TaskRegistry>) -> Self {
        self.task_registry = Some(registry);
        self
    }

    /// Enable automatic task registration using inventory pattern
    #[cfg(feature = "auto-register")]
    #[must_use]
    #[inline]
    pub fn auto_register_tasks(mut self) -> Self {
        self.config.auto_register.enabled = true;
        self
    }

    /// Disable automatic task registration
    #[cfg(feature = "auto-register")]
    #[must_use]
    #[inline]
    pub fn disable_auto_register(mut self) -> Self {
        self.config.auto_register.enabled = false;
        self
    }

    /// Start the task scheduler automatically
    #[must_use]
    #[inline]
    pub fn with_scheduler(mut self) -> Self {
        self.config.scheduler.enabled = true;
        self
    }

    /// Start the auto-scaler automatically
    #[must_use]
    #[inline]
    pub fn with_autoscaler(self) -> Self {
        // Auto-scaler is always available, we just don't start it by default
        // This is a no-op now since we determine if to start it during build
        self
    }

    /// Update the full configuration
    #[must_use]
    #[inline]
    pub fn config(mut self, config: TaskQueueConfig) -> Self {
        self.config = config;
        self
    }

    /// Build the `TaskQueue` instance
    ///
    /// # Errors
    /// Returns `TaskQueueError` if Redis connection fails or configuration is invalid
    #[inline]
    pub async fn build(self) -> Result<TaskQueue, TaskQueueError> {
        // Use override Redis URL if provided, otherwise use config
        let redis_url = self
            .override_redis_url
            .as_ref()
            .unwrap_or(&self.config.redis.url);

        // Create TaskQueue with autoscaler config
        let task_queue =
            TaskQueue::with_autoscaler_config(redis_url, self.config.autoscaler.clone()).await?;

        // Handle task registry setup
        let registry = if self.config.auto_register.enabled {
            #[cfg(feature = "auto-register")]
            {
                if let Some(custom_registry) = self.task_registry {
                    // Auto-register tasks into the provided registry using configuration
                    custom_registry
                        .auto_register_tasks_with_config(Some(&self.config.auto_register))
                        .map_err(|e| {
                            TaskQueueError::Configuration(format!("Auto-registration failed: {e}"))
                        })?;
                    custom_registry
                } else {
                    // Create a new registry with auto-registered tasks using configuration
                    Arc::new(
                        TaskRegistry::with_auto_registered_and_config(Some(
                            &self.config.auto_register,
                        ))
                        .map_err(|e| {
                            TaskQueueError::Configuration(format!("Auto-registration failed: {e}"))
                        })?,
                    )
                }
            }
            #[cfg(not(feature = "auto-register"))]
            {
                return Err(TaskQueueError::Configuration(
                    "Auto-registration requested but 'auto-register' feature is not enabled"
                        .to_string(),
                ));
            }
        } else {
            self.task_registry
                .unwrap_or_else(|| Arc::new(TaskRegistry::new()))
        };

        // Start workers
        let worker_count = self.config.workers.initial_count;
        if worker_count > 0 {
            task_queue
                .start_workers_with_registry(worker_count, registry)
                .await?;
        }

        // Start scheduler if enabled
        if self.config.scheduler.enabled {
            task_queue.start_scheduler().await?;
        }

        // Auto-scaler is always available but we start it based on configuration
        // For now, we'll start it if there are specific auto-scaling settings
        if self.config.autoscaler.max_workers > self.config.autoscaler.min_workers {
            task_queue.start_autoscaler()?;
        }

        Ok(task_queue)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    // use tokio_test;

    #[test]
    fn test_autoscaler_config() {
        let config = AutoScalerConfig::default();
        assert_eq!(config.min_workers, 1);
        assert_eq!(config.max_workers, 20);
        assert_eq!(config.scaling_triggers.queue_pressure_threshold, 0.75);
    }

    #[test]
    fn test_queue_manager() {
        let manager = QueueManager::new();
        let queues = manager.get_queue_names();

        assert!(queues.contains(&"default".to_owned()));
        assert!(queues.contains(&"high_priority".to_owned()));
        assert!(queues.contains(&"low_priority".to_owned()));
    }

    #[test]
    fn test_builder_pattern() {
        let builder = TaskQueueBuilder::new("redis://localhost:6379")
            .initial_workers(4)
            .with_scheduler()
            .with_autoscaler();

        assert_eq!(builder.config.redis.url, "redis://localhost:6379");
        assert_eq!(builder.config.workers.initial_count, 4);
        assert!(builder.config.scheduler.enabled);
    }
}

/// Health check status for monitoring
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthStatus {
    pub status: String,
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub components: std::collections::HashMap<String, ComponentHealth>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComponentHealth {
    pub status: String,
    pub message: Option<String>,
}

/// Comprehensive metrics for monitoring
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskQueueMetrics {
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub total_pending_tasks: i64,
    pub total_processed_tasks: i64,
    pub total_failed_tasks: i64,
    pub active_workers: i64,
    pub tasks_per_worker: f64,
    pub queue_metrics: Vec<QueueMetrics>,
}