Queue Workers
⚠️ Work in Progress: This crate is under active development and is not yet ready for production use. The API is unstable and may undergo significant changes. Feel free to experiment and provide feedback, but please do not use in production environments.
A Redis-backed job queue system for Rust applications with support for retries and concurrent workers.
Features
- Redis-backed persistent job queue
- Automatic job retries with configurable attempts and delay
- Concurrent worker support
- Async/await based API
- Serializable jobs using Serde
- Type-safe job definitions
Prerequisites
- Rust 1.86.0 or later
- Redis server (local or remote)
- Docker and Docker Compose (for development)
Quick Start
- Add the dependency to your
Cargo.toml
:
[]
= "0.1.0"
- Define your job:
use ;
use async_trait;
use Job;
- Create a queue and worker:
use ;
use Duration;
async
Development Setup
- Clone the repository:
- Install development dependencies:
- Set up git hooks:
- Start Redis using Docker Compose:
- Run the tests:
Code Quality
This project enforces code quality through:
- Formatting using
rustfmt
- Linting using
clippy
To manually run the checks:
# Check formatting
# Run clippy
These checks run automatically:
- As a pre-commit hook when committing changes
- In CI/CD pipeline for all pull requests
Configuration
Worker Configuration
The WorkerConfig
struct allows you to customize worker behavior:
let config = WorkerConfig ;
Redis Configuration
The Redis queue can be configured with a Redis URL and queue name:
let queue = new.expect;
Queue Types
The queue supports both FIFO (First In, First Out) and LIFO (Last In, First Out) behaviors:
use ;
// Create a FIFO queue (default behavior)
let fifo_queue = new?;
// Create a LIFO queue
let lifo_queue = with_type?;
- FIFO: Jobs are processed in the order they were added (oldest first)
- LIFO: Jobs are processed in reverse order (newest first)
Running Multiple Workers
You can run multiple workers processing the same queue:
let queue = new?;
// Spawn multiple workers
for _ in 0..3
Error Handling
The library provides a custom error type QueueWorkerError
that covers various failure scenarios:
- Redis connection issues
- Serialization errors
- Job not found
- Worker errors
- Connection timeouts
Worker Types
The library provides two types of workers:
Sequential Worker
Processes jobs one at a time, with retry support:
use ;
let config = WorkerConfig ;
let worker = new;
worker.start.await?;
Concurrent Worker
Processes multiple jobs in parallel:
use ;
let config = ConcurrentWorkerConfig ;
let worker = new;
worker.start.await?;
// Or with shutdown support:
let = channel;
let worker = new;
worker.start_with_shutdown.await?;
Logging
This library uses the log
crate as a logging facade, allowing you to choose your preferred logging implementation. For basic usage:
// Using env_logger
init;
// Or using tracing
fmt
.with_env_filter
.init;
For detailed logging configuration, including production setups and testing configurations, see LOGGING.md.
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.