Simple, extensible job queue processor with MongoDB backend
Project description
Job Queue Processor
A simple, extensible job queue processor with MongoDB backend. Perfect for distributed task processing, background jobs, and workflow automation.
Features
- ๐ Simple & Fast: Minimal setup, just set
MONGODB_URIand go - ๐ฆ Easy Installation: Install with
uv pip install job-queue-processor - ๐ง Configurable: Support for custom databases, collections, and metadata
- โก Multiple Executors: Built-in support for subprocess and
uv run - ๐ฅ Multi-Worker: Concurrent job processing with configurable workers
- ๐ก๏ธ Robust: Timeout handling, graceful shutdown, and error recovery
- ๐ Monitoring: Real-time queue statistics and job history
- ๐ Bulk Operations: Submit multiple jobs efficiently
- ๐ท๏ธ Metadata Support: Add custom fields to jobs for organization
Quick Start
Installation
# Using uv (recommended)
uv pip install job-queue-processor
# Using pip
pip install job-queue-processor
Setup
-
Start MongoDB (locally or use a cloud service)
-
Set environment variable:
export MONGODB_URI="mongodb://localhost:27017" # Or create .env file with MONGODB_URI=mongodb://localhost:27017
-
Start processing jobs:
# Start the processor job-process --workers 3
-
Submit jobs (in another terminal):
# Submit a simple job job-submit "python script.py" # Submit with arguments job-submit "script.py" --args --input data.csv --output results.json # Monitor progress job-monitor
Usage Examples
CLI Usage
# Basic job submission
job-submit "examples/basic_job.py"
# Job with arguments and timeout
job-submit "script.py" --args --input data.csv --timeout 600
# Job with custom metadata
job-submit "script.py" --metadata '{"priority": 1, "team": "data"}'
# Bulk submission from JSON
job-submit --bulk '[
{"command": "script1.py", "timeout": 300},
{"command": "script2.py", "args": ["--mode", "fast"]}
]'
# Process jobs with multiple workers
job-process --workers 5 --executor uv
# Monitor queue
job-monitor --refresh 3
job-monitor --failed # Show only failed jobs
job-submit --stats # Quick stats
Python API
from job_queue_processor import JobQueue, JobProcessor
# Submit jobs
queue = JobQueue()
# Basic job
job_id = queue.submit_job("script.py")
# Job with arguments and metadata
job_id = queue.submit_job(
"process_data.py",
args=["--input", "data.csv", "--output", "results.json"],
timeout=600,
metadata={"priority": 1, "team": "analytics"}
)
# Bulk submission
jobs = [
{
"command": "task1.py",
"args": ["--mode", "fast"],
"timeout": 300,
"metadata": {"batch": "morning_run"}
},
{
"command": "task2.py",
"timeout": 600,
"metadata": {"batch": "morning_run"}
}
]
job_ids = queue.submit_bulk_jobs(jobs)
# Check status
stats = queue.get_job_stats()
print(f"Pending: {stats['pending']}, Running: {stats['running']}")
queue.close()
Process Jobs
from job_queue_processor import JobProcessor
# Start processor
processor = JobProcessor(
workers=3,
executor="subprocess", # or "uv"
queue_config={
"db_name": "my_jobs",
"collection_name": "tasks"
}
)
processor.run() # Blocks and processes jobs
Configuration
Environment Variables
The only required configuration is the MongoDB connection:
# Local MongoDB
MONGODB_URI=mongodb://localhost:27017
# MongoDB Atlas
MONGODB_URI=mongodb+srv://user:pass@cluster.mongodb.net/
# MongoDB with auth
MONGODB_URI=mongodb://user:pass@localhost:27017
Custom Configuration
from job_queue_processor import JobQueue, JobProcessor
# Custom database and collection
queue = JobQueue(
mongo_uri="mongodb://localhost:27017",
db_name="my_project",
collection_name="background_jobs"
)
# Custom processor configuration
processor = JobProcessor(
workers=5,
executor="uv", # Use uv run instead of direct subprocess
command_prefix=["python", "-u"], # Custom command prefix
queue_config={
"db_name": "my_project",
"collection_name": "background_jobs"
}
)
Job Script Requirements
Jobs can be any executable that follows these simple rules:
#!/usr/bin/env python3
import sys
try:
# Do your work here
result = process_data()
print(f"Success: {result}") # stdout for results
sys.exit(0) # 0 = success
except Exception as e:
print(f"Error: {e}", file=sys.stderr) # stderr for errors
sys.exit(1) # non-zero = failure
That's it! No special imports or setup required.
Examples
The examples/ directory contains:
basic_job.py- Simple job templatejob_with_args.py- Job that accepts command line argumentscustom_handler.py- Advanced job with metadata and loggingbulk_submit.py- Bulk job submission patternssubmit_examples.py- Various submission examples
Run the examples:
# Try the examples
python examples/submit_examples.py
# Submit example jobs
job-submit "examples/basic_job.py"
job-submit "examples/job_with_args.py" --args --input test.csv --output out.json
# Process them
job-process --workers 2
Monitoring
Real-time Monitoring
# Continuous monitoring (refreshes every 5 seconds)
job-monitor
# Custom refresh rate
job-monitor --refresh 10
# Show status once and exit
job-monitor --once
# Show only failed jobs
job-monitor --failed
# Show only running jobs
job-monitor --running
Quick Stats
job-submit --stats
Advanced Usage
Custom Metadata
Add custom fields to organize and track jobs:
queue.submit_job(
"ml_training.py",
metadata={
"project": "recommendation_engine",
"priority": 1,
"owner": "data-team",
"experiment_id": "exp_001",
"gpu_required": True
}
)
Multiple Queues
Use different databases or collections for different purposes:
# High priority queue
priority_queue = JobQueue(db_name="priority_jobs")
# Background tasks queue
background_queue = JobQueue(db_name="background_jobs")
# Long-running jobs
long_queue = JobQueue(
db_name="long_jobs",
collection_name="training_jobs"
)
Custom Executors
# Use uv for Python package management
processor = JobProcessor(executor="uv")
# Custom command prefix
processor = JobProcessor(command_prefix=["docker", "run", "my-image"])
# No timeout (for very long jobs)
processor = JobProcessor(command_prefix=["python", "-u"])
Architecture
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ Client โ โ Processor โ โ MongoDB โ
โ โ โ โ โ โ
โ submit_job()โโโโโถโ get_next() โโโโโถโ jobs โ
โ โ โ โ โ collection โ
โ โ โ execute() โ โ โ
โ โ โ โ โ โ
โ monitor() โโโโโโ complete() โโโโโโ โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
Job Lifecycle
pending โ running โ completed/failed
MongoDB Schema
{
"job_id": "uuid",
"command": "script.py",
"args": ["--input", "data.csv"],
"timeout": 1800,
"status": "pending|running|completed|failed",
"created_at": "timestamp",
"started_at": "timestamp",
"completed_at": "timestamp",
"stdout": "process output",
"stderr": "error output",
"exit_code": 0,
"metadata": {
"priority": 1,
"team": "analytics"
}
}
Development
Setup Development Environment
git clone <repository>
cd job-queue-processor
# Install with development dependencies
uv pip install -e ".[dev]"
# Run tests
pytest
# Format code
black src/ examples/
ruff check src/ examples/
Building and Publishing
# Build package
python -m build
# Publish to PyPI
python -m twine upload dist/*
License
MIT License - see LICENSE file for details.
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
Support
- Documentation: This README and inline code comments
- Examples: See
examples/directory - Issues: Report bugs and request features on GitHub
- MongoDB: Any MongoDB-compatible database (local, Atlas, etc.)
Ready to get started? Just set MONGODB_URI and run job-process! ๐
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file job_queue_processor-0.1.0.tar.gz.
File metadata
- Download URL: job_queue_processor-0.1.0.tar.gz
- Upload date:
- Size: 15.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8633bc0b982af9aaba4dfb017e056e0eab725620f3b05e8407c622853d61ab63
|
|
| MD5 |
cedd7d65ac947f72332b2ba27d6f5a97
|
|
| BLAKE2b-256 |
ea3cc0c537e0f050ad644a500af6774b1f0a0322d5f95f2dfc479ede95e1a12a
|
File details
Details for the file job_queue_processor-0.1.0-py3-none-any.whl.
File metadata
- Download URL: job_queue_processor-0.1.0-py3-none-any.whl
- Upload date:
- Size: 16.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9db0b7096f15123b776d8ac48909799e4902066664b74604ad6f5492fd5ae2e8
|
|
| MD5 |
b582f95cb00c67fa9e6baa7b4aba2566
|
|
| BLAKE2b-256 |
4882b80d88194404d7d985bf2f0ce04216f9c5f01622b8938eb8d00e68c918e3
|