$ show rq/django-rq
django-rq is a Django integration library for RQ (Redis Queue), providing a seamless way to configure and manage background job queues within Django projects. It allows developers to define queues in Django's settings.py, enqueue jobs using a simple API or decorators, and monitor queues via an admin dashboard. The library supports multiple Redis connection configurations (including Sentinel, SSL, and URL-based), custom job/worker/queue classes, and various commit modes (auto, on_db_commit, request_finished) to control when jobs are enqueued relative to database transactions. It also includes management commands for running workers, schedulers, and cron jobs, as well as Prometheus metrics and JSON statistics endpoints for monitoring. Architecturally, django-rq extends RQ's core classes (Queue, Worker, Job) with Django-aware subclasses that integrate with Django's settings, signals, and admin interface. The key entry points are the `django_rq` module's public API (`enqueue`, `get_queue`, `get_worker`, `job` decorator), Django management commands (`rqworker`, `rqscheduler`, `rqcron`, `rqstats`, etc.), and the admin views automatically registered via Django's admin site. Data flows involve enqueuing jobs (which may be deferred via thread-local queue or database transaction hooks), processing by workers, and storing results/status in Redis. External dependencies are Django (4.2+), RQ (2.6.1+), and Redis (3.5+), with optional prometheus_client for metrics.
The LLM identified 3 files worth highlighting for reuse:
Implements a thread-local queue to defer job enqueuing until the end of a request/response cycle or database transaction commit. This pattern is cleanly isolated (uses only Python threading and a simple list) and can be extracted to any Django project needing deferred task submission. It demonstrates non-obvious skill in handling thread-safety and signal-based flushing.
Provides a factory function `get_redis_connection` that creates Redis connections from a configuration dictionary, supporting multiple backends (URL, HOST/PORT, Unix socket, Sentinel, django-redis cache). The `filter_connection_params` and `get_unique_connection_configs` functions are reusable utilities for deduplicating and extracting connection parameters. This module is self-contained and could be reused in any Python project needing Redis connection management from config.
Contains `DjangoCronScheduler`, a subclass of RQ's `CronScheduler` that integrates with Django's queue configuration. It validates that all registered jobs use the same Redis connection, provides a `connection_index` property for admin linking, and exposes helper functions for fetching cron job history. This component demonstrates careful design for bridging external library features with Django settings and could be extracted as a pattern for other Django-RQ integrations.