|
| 1 | +use redis::{AsyncCommands, Client}; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | +use std::sync::Arc; |
| 4 | +use tokio::time::{Duration, sleep}; |
| 5 | +use uuid::Uuid; |
| 6 | + |
| 7 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 8 | +pub struct BulkValidationJob { |
| 9 | + pub id: String, |
| 10 | + pub emails: Vec<String>, |
| 11 | + pub check_role_based: bool, |
| 12 | + pub status: JobStatus, |
| 13 | + pub created_at: i64, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 17 | +pub enum JobStatus { |
| 18 | + Pending, |
| 19 | + Processing, |
| 20 | + Completed, |
| 21 | + Failed, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Clone)] |
| 25 | +pub struct JobQueue { |
| 26 | + redis: Arc<Client>, |
| 27 | +} |
| 28 | + |
| 29 | +impl JobQueue { |
| 30 | + pub fn new(redis_url: &str) -> Result<Self, redis::RedisError> { |
| 31 | + let client = Client::open(redis_url)?; |
| 32 | + Ok(Self { |
| 33 | + redis: Arc::new(client), |
| 34 | + }) |
| 35 | + } |
| 36 | + |
| 37 | + pub async fn enqueue_bulk_validation( |
| 38 | + &self, |
| 39 | + emails: Vec<String>, |
| 40 | + check_role_based: bool, |
| 41 | + ) -> Result<String, redis::RedisError> { |
| 42 | + let job_id = Uuid::new_v4().to_string(); |
| 43 | + let job = BulkValidationJob { |
| 44 | + id: job_id.clone(), |
| 45 | + emails, |
| 46 | + check_role_based, |
| 47 | + status: JobStatus::Pending, |
| 48 | + created_at: chrono::Utc::now().timestamp(), |
| 49 | + }; |
| 50 | + |
| 51 | + let mut conn = self.redis.get_multiplexed_async_connection().await?; |
| 52 | + let job_json = serde_json::to_string(&job).unwrap(); |
| 53 | + |
| 54 | + let _: () = conn.lpush("bulk_validation_queue", &job_json).await?; |
| 55 | + let _: () = conn.set(format!("job:{}", job_id), &job_json).await?; |
| 56 | + let _: () = conn.expire(format!("job:{}", job_id), 3600).await?; // 1 hour TTL |
| 57 | + |
| 58 | + Ok(job_id) |
| 59 | + } |
| 60 | + |
| 61 | + pub async fn get_job_status( |
| 62 | + &self, |
| 63 | + job_id: &str, |
| 64 | + ) -> Result<Option<BulkValidationJob>, redis::RedisError> { |
| 65 | + let mut conn = self.redis.get_multiplexed_async_connection().await?; |
| 66 | + let job_json: Option<String> = conn.get(format!("job:{}", job_id)).await?; |
| 67 | + |
| 68 | + Ok(job_json.and_then(|json| serde_json::from_str(&json).ok())) |
| 69 | + } |
| 70 | + |
| 71 | + pub async fn update_job_status( |
| 72 | + &self, |
| 73 | + job_id: &str, |
| 74 | + status: JobStatus, |
| 75 | + ) -> Result<(), redis::RedisError> { |
| 76 | + let mut conn = self.redis.get_multiplexed_async_connection().await?; |
| 77 | + |
| 78 | + if let Some(mut job) = self.get_job_status(job_id).await? { |
| 79 | + job.status = status; |
| 80 | + let job_json = serde_json::to_string(&job).unwrap(); |
| 81 | + let _: () = conn.set(format!("job:{}", job_id), &job_json).await?; |
| 82 | + } |
| 83 | + |
| 84 | + Ok(()) |
| 85 | + } |
| 86 | + |
| 87 | + pub async fn process_jobs<F, Fut>(&self, processor: F) |
| 88 | + where |
| 89 | + F: Fn(BulkValidationJob) -> Fut + Send + Sync + 'static, |
| 90 | + Fut: std::future::Future<Output = ()> + Send + 'static, |
| 91 | + { |
| 92 | + loop { |
| 93 | + match self.get_next_job().await { |
| 94 | + Ok(Some(job)) => { |
| 95 | + let _ = self.update_job_status(&job.id, JobStatus::Processing).await; |
| 96 | + processor(job).await; |
| 97 | + } |
| 98 | + Ok(None) => { |
| 99 | + sleep(Duration::from_secs(1)).await; |
| 100 | + } |
| 101 | + Err(_) => { |
| 102 | + sleep(Duration::from_secs(5)).await; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + async fn get_next_job(&self) -> Result<Option<BulkValidationJob>, redis::RedisError> { |
| 109 | + let mut conn = self.redis.get_multiplexed_async_connection().await?; |
| 110 | + let result: Option<(String, String)> = conn.brpop("bulk_validation_queue", 1.0).await?; |
| 111 | + let job_json = result.map(|(_, value)| value); |
| 112 | + |
| 113 | + Ok(job_json.and_then(|json| serde_json::from_str(&json).ok())) |
| 114 | + } |
| 115 | +} |
0 commit comments