1pub 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
22pub 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 pub async fn start(&self) -> Result<()> {
38 self.executor.write().await.start().await
39 }
40
41 pub async fn stop(&self) -> Result<()> {
43 self.executor.write().await.stop().await
44 }
45
46 pub async fn enqueue(&self, job: Job) -> Result<String> {
48 self.queue.enqueue(job).await
49 }
50
51 pub async fn get_status(&self, job_id: &str) -> Result<Option<JobStatus>> {
53 self.queue.get_status(job_id).await
54 }
55
56 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}