This library is a modular BullMQ-inspired wrapper in Rust, allowing queue, job, and worker management via Redis. It provides advanced features such as job prioritization, delays, retries, and queue management.
/src
βββ config_service.rs # Centralized Redis configuration management
βββ queue_service.rs # Queue and job management
βββ worker_service.rs # Workers for job execution
βββ job_model.rs # Job model with advanced options
βββ log_service.rs # Logging service for job events
βββ lib.rs # Library module declarations
βββ main.rs # Application entry point
/tests
βββ config_service_tests.rs # Tests for ConfigService
βββ mocks # Mock services for testing
Ensure you have Rust installed and Redis running locally or in the cloud.
cargo buildCreate a .env file with Redis parameters:
REDIS_URL=redis://127.0.0.1:6379- start redis
docker compose up -d
- Create a queue trigger service :
cargo run --bin queue_trigger
- Push message to queue :
cargo run --bin push_message
use bullmq_rust::queue_service::QueueService;
use bullmq_rust::job_model::JobData;
use bullmq_rust::config_service::ConfigService;
use chrono::Utc;
#[tokio::main]
async fn main() {
let config = ConfigService::new();
let mut queue_service = QueueService::new(config.get_client().unwrap());
let job = JobData {
id: "1".to_string(),
message: "Hello, Rust!".to_string(),
timestamp: Utc::now().to_rfc3339(),
priority: Some(1),
delay: Some(5),
retries: Some(3),
expires_in: None,
progress: Some(0),
};
queue_service.add_job("testQueue", job).await.unwrap();
}use std::sync::Arc;
use bullmq_rust::worker_service::WorkerService;
use bullmq_rust::queue_service::QueueService;
use bullmq_rust::config_service::ConfigService;
#[tokio::main]
async fn main() {
let config = ConfigService::new();
let queue_service = Arc::new(QueueService::new(config.get_client().unwrap()));
let worker = WorkerService::new("testQueue".to_string(), Arc::clone(&queue_service));
worker.start().await;
}use std::sync::Arc;
use bullmq_rust::worker_service::WorkerService;
use bullmq_rust::queue_service::QueueService;
use bullmq_rust::config_service::ConfigService;
#[tokio::main]
async fn main() {
let config = ConfigService::new();
let queue_service = Arc::new(QueueService::new(config.get_client().unwrap()));
let worker = WorkerService::new("testQueue".to_string(), Arc::clone(&queue_service));
worker.retry_failed_jobs().await;
}Manages Redis configuration.
new() -> Self: Creates a newConfigServiceinstance.get_client(&self) -> RedisResult<Client>: Returns a Redis client.
Manages queues and jobs in Redis.
new(conn: redis::Connection) -> Self: Creates a newQueueServiceinstance.add_job(&mut self, queue_name: &str, job: JobData) -> RedisResult<()>: Adds a job to the specified queue.get_next_job(&mut self, queue_name: &str) -> RedisResult<Option<String>>: Retrieves the next job from the specified queue.count_jobs(&mut self, queue_name: &str) -> RedisResult<u64>: Counts the number of jobs in the specified queue.move_to_failed(&mut self, queue_name: &str, job: JobData) -> RedisResult<()>: Moves a job to the failed queue.log_job_status(&mut self, queue_name: &str, job: &JobData, status: &str) -> RedisResult<()>: Logs the status of a job.update_job_progress(&mut self, queue_name: &str, job_id: &str, progress: u32) -> RedisResult<()>: Updates the progress of a job.get_job_progress(&mut self, queue_name: &str, job_id: &str) -> RedisResult<u32>: Retrieves the progress of a job.
Manages workers that process jobs from a queue.
new(queue_name: String, queue_service: Arc<QueueService>) -> Self: Creates a newWorkerServiceinstance.start(&self): Starts the worker to process jobs from the queue.retry_failed_jobs(&self): Retries failed jobs from the failed queue.
Logs job events to Redis.
new(client: Arc<Mutex<Client>>) -> Self: Creates a newLogServiceinstance.log(&self, queue_name: &str, message: &str) -> RedisResult<()>: Logs a message to the specified queue's log.
Represents the data of a job.
id: String: The unique identifier of the job.message: String: The message of the job.timestamp: String: The timestamp when the job was created.priority: Option<i32>: The priority of the job.delay: Option<i64>: The delay before the job can be processed.retries: Option<u32>: The number of retries allowed for the job.expires_in: Option<i64>: The expiration time of the job.progress: Option<u32>: The progress of the job.
REDIS_URL=redis://localhost:6379This project is licensed under the MIT License.