aurora_db/workers/
mod.rs

1// Durable Workers - Background job processing with persistence
2//
3// Provides a reliable background job system with:
4// - Job persistence across restarts
5// - Retry logic with exponential backoff
6// - Job scheduling and delayed execution
7// - Dead letter queue for failed jobs
8// - Job status tracking
9
10pub mod executor;
11pub mod job;
12pub mod queue;
13
14pub use executor::{WorkerConfig, WorkerExecutor};
15pub use job::{Job, JobPriority, JobStatus};
16pub use queue::JobQueue;
17
18use crate::error::Result;
19use std::sync::Arc;
20use tokio::sync::RwLock;
21
22/// Main worker system
23pub struct WorkerSystem {
24    queue: Arc<JobQueue>,
25    executor: Arc<RwLock<WorkerExecutor>>,
26}
27
28impl WorkerSystem {
29    pub fn new(config: WorkerConfig) -> Result<Self> {
30        let queue = Arc::new(JobQueue::new(config.storage_path.clone())?);
31        let executor = Arc::new(RwLock::new(WorkerExecutor::new(Arc::clone(&queue), config)));
32
33        Ok(Self { queue, executor })
34    }
35
36    /// Start the worker system
37    pub async fn start(&self) -> Result<()> {
38        self.executor.write().await.start().await
39    }
40
41    /// Stop the worker system gracefully
42    pub async fn stop(&self) -> Result<()> {
43        self.executor.write().await.stop().await
44    }
45
46    /// Enqueue a new job
47    pub async fn enqueue(&self, job: Job) -> Result<String> {
48        self.queue.enqueue(job).await
49    }
50
51    /// Get job status
52    pub async fn get_status(&self, job_id: &str) -> Result<Option<JobStatus>> {
53        self.queue.get_status(job_id).await
54    }
55
56    /// Get queue statistics
57    pub async fn stats(&self) -> Result<QueueStats> {
58        self.queue.stats().await
59    }
60}
61
62#[derive(Debug, Clone)]
63pub struct QueueStats {
64    pub pending: usize,
65    pub running: usize,
66    pub completed: usize,
67    pub failed: usize,
68    pub dead_letter: usize,
69}