Django-RQ Architecture
Complete technical overview of Django-RQ integration in django-cfg, including system design, component interaction, and implementation details.
System Overview
Django-RQ in django-cfg consists of several interconnected components:
Core Components
1. Configuration Layer
Type-safe configuration using Pydantic v2:
Key Features:
- Validation: Pydantic ensures type safety and correct values
- Smart defaults: Uses
redis_urlfrom parentDjangoConfig - Flexibility: Support for Redis URL, Sentinel, SSL connections
- Declarative: Schedule tasks directly in config
2. Django-RQ Integration
Django-CFG seamlessly integrates with django-rq:
Generated Settings:
# Django settings.py (auto-generated)
RQ_QUEUES = {
'default': {
'URL': 'redis://localhost:6379/0',
'DEFAULT_TIMEOUT': 360,
'DEFAULT_RESULT_TTL': 500,
},
'high': {
'URL': 'redis://localhost:6379/0',
'DEFAULT_TIMEOUT': 180,
'DEFAULT_RESULT_TTL': 300,
},
}
RQ_SHOW_ADMIN_LINK = True3. Task Execution Flow
Complete lifecycle of a job from enqueue to completion:
4. Queue Management
Multiple queues with different priorities:
Queue Strategy:
- high: Critical tasks (payment processing, auth)
- default: Normal tasks (emails, notifications)
- low: Batch operations (reports, cleanup)
- knowledge: AI/ML tasks (document processing, embeddings)
5. Scheduler Architecture
Schedules run on RQ’s native cron (rq.cron). One python manage.py rqcron per project builds DjangoCfgCronScheduler from DjangoRQConfig.get_all_schedules(); the same process runs RQ’s delayed-job scheduler for every queue in RQ_QUEUES:
Schedule Registration:
One copy per recurring job: every run is a fresh job with its own id; a due run whose previous copy still waits in the queue or runs with a heartbeat younger than 120 s is skipped and the next run is booked. Cron strings are evaluated in UTC, and after a restart interval and cron schedules resume from their run history instead of all firing at once. Details, rq_cron_status and the dedicated-worker pattern: Scheduler and Workers.
Monitoring Architecture
django-cfg has no RQ REST API. Monitoring has three parts:
- Queue-health monitor (
modules/django_rq/health): whenDjangoRQConfig.health.enabledis on, a scheduled job collects per-queue metrics (depth, oldest-job age, failed count, worker liveness, scheduler lag), evaluates them against thresholds and sends Telegram alerts with a Redis cooldown. Alsopython manage.py rq_health_check. - Cron status (
health/status.py,cron_status()): therqcronheartbeat, per-queue wait and busy workers; printed bypython manage.py rq_cron_statusand shown in the Modules tab of the admin Overview. - django-rq’s own admin pages for queues, workers, jobs and cron.
Pydantic Models
Internal models (modules/django_rq/services/models) for type-safe data handling:
Integration Patterns
1. Enqueue from Anywhere
Tasks can be enqueued from any Django component:
2. Result Handling
Multiple ways to handle job results:
3. Error Handling
Comprehensive error handling and retry logic:
Performance Optimization
1. Worker Pool Architecture
Multiple workers for parallelism:
Benefits:
- Utilize multiple CPU cores
- Parallel job execution
- Better throughput
- Fault isolation
2. Redis Data Structures
Efficient use of Redis:
3. Connection Pooling
Redis connection management:
Deployment Architecture
Single-Server Setup
Multi-Server Setup
Troubleshooting: A Scheduled Task Does Not Run
python manage.py rqcron --listprints every registered schedule as JSON. An entry whose queue is not inRQ_QUEUES, or whose function cannot be imported, is listed undernot_registeredwith the reason and is never pushed.python manage.py rq_cron_statustellscron_dead(norqcronheartbeat) fromstarved(workers busy; it names what they run).- In the
rqcronlog,rq cron: skipped <func> (<name>): previous copy ... still queued|runningmeans the previous run has not finished; the schedule is alive. rq cron: standby, leader is <name>means anotherrqcronholdsdjangocfg:rq:cron:leader; only the leader enqueues.
Each schedule’s runs are listed in its history rq:cron_job:<name>:jobs, where <name> is job_id when set, else schedule_<func>_<hash>. django-rq’s admin shows the cron schedulers and each cron job’s run history.
See Also
Configuration
- Configuration Guide - Detailed setup guide
- Examples - Real-world examples
- Deployment - Production deployment
Reference
- Django-RQ Docs - Official documentation
- RQ Docs - Core RQ documentation
- Redis Docs - Redis documentation
Related
- Integration Patterns - Common patterns