Skip to Content
FeaturesModulesBackground Tasks (RQ)Architecture

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_url from parent DjangoConfig
  • 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 = True

3. 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): when DjangoRQConfig.health.enabled is 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. Also python manage.py rq_health_check.
  • Cron status (health/status.py, cron_status()): the rqcron heartbeat, per-queue wait and busy workers; printed by python manage.py rq_cron_status and 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

  1. python manage.py rqcron --list prints every registered schedule as JSON. An entry whose queue is not in RQ_QUEUES, or whose function cannot be imported, is listed under not_registered with the reason and is never pushed.
  2. python manage.py rq_cron_status tells cron_dead (no rqcron heartbeat) from starved (workers busy; it names what they run).
  3. In the rqcron log, rq cron: skipped <func> (<name>): previous copy ... still queued|running means the previous run has not finished; the schedule is alive.
  4. rq cron: standby, leader is <name> means another rqcron holds djangocfg: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

Reference

Last updated on