elif-queue 0.3.0

Background job queue system for elif.rs framework
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
//! Worker implementation for processing jobs from the queue

use crate::{Queue, QueueBackend, QueueResult, QueueError, JobEntry, JobResult, QueueConfig};
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::{RwLock, Semaphore};
use tokio::time::{interval, Duration, timeout};
use tracing::{info, warn, error, debug};
use futures::future::BoxFuture;

/// Job handler function type
pub type JobHandler = Arc<dyn Fn(JobEntry) -> BoxFuture<'static, JobResult<()>> + Send + Sync>;

/// Worker registry for managing job handlers
pub struct WorkerRegistry {
    handlers: RwLock<HashMap<String, JobHandler>>,
}

impl WorkerRegistry {
    /// Create a new worker registry
    pub fn new() -> Self {
        Self {
            handlers: RwLock::new(HashMap::new()),
        }
    }
    
    /// Register a job handler for a specific job type
    pub async fn register<T: crate::Job + JobTypeProvider + 'static>(&self, handler: impl JobProcessor<T> + Send + Sync + 'static) {
        let job_type = T::default_job_type();
        let handler: JobHandler = Arc::new(move |entry: JobEntry| {
            let handler = handler.clone();
            Box::pin(async move {
                handler.process(entry).await
            })
        });
        
        self.handlers.write().await.insert(job_type.to_string(), handler);
        info!("Registered job handler for type: {}", job_type);
    }
    
    /// Get a handler for a job type
    pub async fn get_handler(&self, job_type: &str) -> Option<JobHandler> {
        self.handlers.read().await.get(job_type).cloned()
    }
    
    /// List all registered job types
    pub async fn list_job_types(&self) -> Vec<String> {
        self.handlers.read().await.keys().cloned().collect()
    }
}

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

/// Trait for processing specific job types
#[async_trait]
pub trait JobProcessor<T: crate::Job>: Clone + Send + Sync {
    /// Process a job entry
    async fn process(&self, entry: JobEntry) -> JobResult<()>;
}

/// Convenience implementation for closure-based processors
#[async_trait]
impl<T, F, Fut> JobProcessor<T> for F
where
    T: crate::Job + 'static,
    F: Fn(T) -> Fut + Clone + Send + Sync + 'static,
    Fut: std::future::Future<Output = JobResult<()>> + Send + 'static,
{
    async fn process(&self, entry: JobEntry) -> JobResult<()> {
        let job: T = serde_json::from_value(entry.payload.clone())?;
        self(job).await
    }
}

/// Worker for processing jobs from a queue
pub struct Worker<B: QueueBackend> {
    queue: Arc<Queue<B>>,
    registry: Arc<WorkerRegistry>,
    config: QueueConfig,
    concurrency_limiter: Arc<Semaphore>,
}

impl<B: QueueBackend + 'static> Worker<B> {
    /// Create a new worker
    pub fn new(queue: Queue<B>, registry: WorkerRegistry, config: QueueConfig) -> Self {
        let concurrency_limiter = Arc::new(Semaphore::new(*config.get_max_workers()));
        
        Self {
            queue: Arc::new(queue),
            registry: Arc::new(registry),
            config,
            concurrency_limiter,
        }
    }
    
    /// Start processing jobs
    pub async fn start(&self) -> QueueResult<()> {
        info!("Starting worker with {} max concurrent jobs", self.config.get_max_workers());
        
        let mut poll_interval = interval(*self.config.get_poll_interval());
        
        loop {
            poll_interval.tick().await;
            
            // Try to get a job from the queue
            match self.queue.dequeue().await {
                Ok(Some(job_entry)) => {
                    let permit = match self.concurrency_limiter.clone().try_acquire_owned() {
                        Ok(permit) => permit,
                        Err(_) => {
                            // No available worker slots, continue polling
                            debug!("No available worker slots, skipping job processing");
                            continue;
                        }
                    };
                    
                    let queue = self.queue.clone();
                    let registry = self.registry.clone();
                    let job_timeout = *self.config.get_default_timeout();
                    
                    // Process job in background
                    tokio::spawn(async move {
                        let _permit = permit; // Hold permit until job is done
                        
                        let job_id = job_entry.id();
                        let job_type = job_entry.job_type().to_string();
                        
                        debug!("Processing job {} of type {}", job_id, job_type);
                        
                        let result = if let Some(handler) = registry.get_handler(&job_type).await {
                            // Execute with timeout
                            match timeout(job_timeout, handler(job_entry)).await {
                                Ok(result) => result,
                                Err(_) => {
                                    error!("Job {} timed out after {:?}", job_id, job_timeout);
                                    Err(Box::new(QueueError::Timeout) as Box<dyn std::error::Error + Send + Sync>)
                                }
                            }
                        } else {
                            error!("No handler registered for job type: {}", job_type);
                            Err(Box::new(QueueError::Configuration(
                                format!("No handler for job type: {}", job_type)
                            )) as Box<dyn std::error::Error + Send + Sync>)
                        };
                        
                        // Complete the job and log outcome
                        match &result {
                            Ok(_) => {
                                info!("Job {} completed successfully", job_id);
                                if let Err(e) = queue.complete(job_id, result).await {
                                    error!("Failed to mark job {} as completed: {}", job_id, e);
                                }
                            }
                            Err(e) => {
                                warn!("Job {} failed: {}", job_id, e);
                                if let Err(e2) = queue.complete(job_id, result).await {
                                    error!("Failed to mark job {} as completed: {}", job_id, e2);
                                }
                            }
                        }
                    });
                }
                Ok(None) => {
                    // No jobs available, continue polling
                    continue;
                }
                Err(e) => {
                    error!("Failed to dequeue job: {}", e);
                    // Brief pause before retrying
                    tokio::time::sleep(Duration::from_secs(1)).await;
                }
            }
        }
    }
    
    /// Start processing with graceful shutdown
    pub async fn start_with_shutdown(&self, mut shutdown: tokio::sync::mpsc::Receiver<()>) -> QueueResult<()> {
        info!("Starting worker with graceful shutdown support");
        
        let mut poll_interval = interval(*self.config.get_poll_interval());
        let mut shutting_down = false;
        
        loop {
            tokio::select! {
                _ = poll_interval.tick(), if !shutting_down => {
                    // Try to get a job from the queue
                    match self.queue.dequeue().await {
                        Ok(Some(job_entry)) => {
                            let permit = match self.concurrency_limiter.clone().try_acquire_owned() {
                                Ok(permit) => permit,
                                Err(_) => {
                                    debug!("No available worker slots, skipping job processing");
                                    continue;
                                }
                            };
                            
                            let queue = self.queue.clone();
                            let registry = self.registry.clone();
                            let job_timeout = *self.config.get_default_timeout();
                            
                            // Process job in background
                            tokio::spawn(async move {
                                let _permit = permit;
                                
                                let job_id = job_entry.id();
                                let job_type = job_entry.job_type().to_string();
                                
                                debug!("Processing job {} of type {}", job_id, job_type);
                                
                                let result = if let Some(handler) = registry.get_handler(&job_type).await {
                                    match timeout(job_timeout, handler(job_entry)).await {
                                        Ok(result) => result,
                                        Err(_) => {
                                            error!("Job {} timed out after {:?}", job_id, job_timeout);
                                            Err(Box::new(QueueError::Timeout) as Box<dyn std::error::Error + Send + Sync>)
                                        }
                                    }
                                } else {
                                    error!("No handler registered for job type: {}", job_type);
                                    Err(Box::new(QueueError::Configuration(
                                        format!("No handler for job type: {}", job_type)
                                    )) as Box<dyn std::error::Error + Send + Sync>)
                                };
                                
                                // Complete the job and log outcome
                                match &result {
                                    Ok(_) => {
                                        info!("Job {} completed successfully", job_id);
                                        if let Err(e) = queue.complete(job_id, result).await {
                                            error!("Failed to mark job {} as completed: {}", job_id, e);
                                        }
                                    }
                                    Err(e) => {
                                        warn!("Job {} failed: {}", job_id, e);
                                        if let Err(e2) = queue.complete(job_id, result).await {
                                            error!("Failed to mark job {} as completed: {}", job_id, e2);
                                        }
                                    }
                                }
                            });
                        }
                        Ok(None) => {
                            // No jobs available
                            continue;
                        }
                        Err(e) => {
                            error!("Failed to dequeue job: {}", e);
                            tokio::time::sleep(Duration::from_secs(1)).await;
                        }
                    }
                }
                
                _ = shutdown.recv() => {
                    info!("Shutdown signal received, stopping new job processing");
                    shutting_down = true;
                    
                    // Wait for active jobs to complete
                    let active_jobs = *self.config.get_max_workers() - self.concurrency_limiter.available_permits();
                    info!("Waiting for {} active jobs to complete", active_jobs);
                    while self.concurrency_limiter.available_permits() < *self.config.get_max_workers() {
                        tokio::time::sleep(Duration::from_millis(100)).await;
                    }
                    
                    info!("Worker shutdown complete");
                    break;
                }
            }
        }
        
        Ok(())
    }
    
    /// Get worker statistics
    pub async fn stats(&self) -> QueueResult<WorkerStats> {
        let queue_stats = self.queue.stats().await?;
        let available_slots = self.concurrency_limiter.available_permits();
        let active_jobs = *self.config.get_max_workers() - available_slots;
        let job_types = self.registry.list_job_types().await;
        
        Ok(WorkerStats {
            queue_stats,
            max_workers: *self.config.get_max_workers(),
            active_workers: active_jobs,
            available_workers: available_slots,
            registered_job_types: job_types,
        })
    }
}

/// Worker statistics
#[derive(Debug, Clone)]
pub struct WorkerStats {
    pub queue_stats: crate::QueueStats,
    pub max_workers: usize,
    pub active_workers: usize,
    pub available_workers: usize,
    pub registered_job_types: Vec<String>,
}

/// Extension trait for Job to provide default job type
pub trait JobTypeProvider {
    fn default_job_type() -> &'static str;
}

// Blanket implementation for jobs that implement the main Job trait
impl<T> JobTypeProvider for T 
where 
    T: crate::Job + Default,
{
    fn default_job_type() -> &'static str {
        T::default().job_type()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{backends::MemoryBackend, Priority};
    use crate::config::QueueConfigBuilder;
    use serde::{Deserialize, Serialize};
    use std::sync::atomic::{AtomicU32, Ordering};
    
    #[derive(Debug, Clone, Serialize, Deserialize, Default)]
    struct TestJob {
        id: u32,
        message: String,
    }
    
    #[async_trait]
    impl crate::Job for TestJob {
        async fn execute(&self) -> JobResult<()> {
            Ok(())
        }
        
        fn job_type(&self) -> &'static str {
            "test"
        }
    }
    
    #[derive(Clone)]
    struct TestJobProcessor {
        counter: Arc<AtomicU32>,
    }
    
    impl TestJobProcessor {
        fn new() -> Self {
            Self {
                counter: Arc::new(AtomicU32::new(0)),
            }
        }
        
        fn get_count(&self) -> u32 {
            self.counter.load(Ordering::Relaxed)
        }
    }
    
    #[async_trait]
    impl JobProcessor<TestJob> for TestJobProcessor {
        async fn process(&self, _entry: JobEntry) -> JobResult<()> {
            self.counter.fetch_add(1, Ordering::Relaxed);
            Ok(())
        }
    }
    
    #[tokio::test]
    async fn test_worker_registry() {
        let registry = WorkerRegistry::new();
        let processor = TestJobProcessor::new();
        
        registry.register::<TestJob>(processor.clone()).await;
        
        let job_types = registry.list_job_types().await;
        assert_eq!(job_types, vec!["test"]);
        
        let handler = registry.get_handler("test").await;
        assert!(handler.is_some());
        
        let no_handler = registry.get_handler("nonexistent").await;
        assert!(no_handler.is_none());
    }
    
    #[tokio::test]
    async fn test_job_processing() {
        let backend = MemoryBackend::new(QueueConfigBuilder::testing().build().expect("Failed to build config"));
        let queue = Queue::new(backend);
        let registry = WorkerRegistry::new();
        let processor = TestJobProcessor::new();
        
        registry.register::<TestJob>(processor.clone()).await;
        
        // Enqueue a test job
        let job = TestJob {
            id: 1,
            message: "test message".to_string(),
        };
        let job_id = queue.enqueue(job, Some(Priority::Normal)).await.unwrap();
        
        // Process job manually
        let job_entry = queue.dequeue().await.unwrap().unwrap();
        let handler = registry.get_handler("test").await.unwrap();
        let result = handler(job_entry).await;
        
        assert!(result.is_ok());
        assert_eq!(processor.get_count(), 1);
        
        // Complete the job
        queue.complete(job_id, result).await.unwrap();
        
        // Verify job was completed
        let stats = queue.stats().await.unwrap();
        assert_eq!(stats.completed_jobs, 1);
    }
    
    #[tokio::test]
    async fn test_worker_stats() {
        let backend = MemoryBackend::new(QueueConfigBuilder::testing().build().expect("Failed to build config"));
        let queue = Queue::new(backend);
        let registry = WorkerRegistry::new();
        let processor = TestJobProcessor::new();
        let config = QueueConfigBuilder::testing().build().expect("Failed to build config");
        
        registry.register::<TestJob>(processor).await;
        let worker = Worker::new(queue, registry, config);
        
        let stats = worker.stats().await.unwrap();
        assert_eq!(stats.max_workers, 1); // testing config
        assert_eq!(stats.active_workers, 0);
        assert_eq!(stats.available_workers, 1);
        assert_eq!(stats.registered_job_types, vec!["test"]);
    }
}