django-rq

repository·master·Indexed 23 days ago

https://github.com/rq/django-rq

A Django integration for RQ (Redis Queue) that allows developers to manage background tasks and queues through Django settings and the Django Admin interface. It provides utilities for enqueuing jobs, a @job decorator, management commands for workers (rqworker, rqworker-pool), and support for Redis Sentinel, Prometheus metrics, and cron-style scheduling.

Tokens
10.8K
Snippets
17
Records
84
Agent score
34%

What's inside django-rq

  1. Install and configure Django-RQ

    master

    To use Django-RQ, install the package via pip and add django_rq to your INSTALLED_APPS. You must then define your queues in the RQ_QUEUES dictionary within your settings.py. Each queue configuration can specify connection details like HOST, PORT, DB, USERNAME, and PASSWORD, or use a URL string. You can also provide REDIS_CLIENT_KWARGS, CONNECTION_KWARGS, or SENTINEL_KWARGS for advanced Redis connection settings.

    Requirements:

    • Django (4.2+)
    • RQ
    pip install django-rq
    INSTALLED_APPS = (
        # other apps
        "django_rq",
    )
    
    RQ_QUEUES = {
        'default': {
            'HOST': 'localhost',
            'PORT': 6379,
            'DB': 0,
            'USERNAME': 'some-user',
            'PASSWORD': 'some-password',
            'DEFAULT_TIMEOUT': 360,
            'DEFAULT_RESULT_TTL': 800,
            'REDIS_CLIENT_KWARGS': {
                'ssl_cert_reqs': None,
            },
        },
        'high': {
            'URL': 'redis://localhost:6379/0',
            'DEFAULT_TIMEOUT': 500,
        },
    }
    
    RQ_EXCEPTION_HANDLERS = ['path.to.my.handler']
  2. Deploy django-rq workers on Ubuntu using systemd

    master

    To run rqworker as a background service on Ubuntu, create a systemd service file at /etc/systemd/system/rqworker.service.

    In the ExecStart directive, ensure you point to your virtual environment's python binary and use manage.py rqworker followed by the queues you wish to process (e.g., high, default, low).

    After creating the file, enable and start the service using systemctl.

    [Unit]
    Description=Django-RQ Worker
    After=network.target
    
    [Service]
    WorkingDirectory=<<path_to_your_project_folder>>
    ExecStart=/home/ubuntu/.virtualenv/<<your_virtualenv>>/bin/python \ <<path_to_your_project_folder>>/manage.py \ rqworker high default low
    
    [Install]
    WantedBy=multi-user.target
  3. Deploy django-rq workers on Heroku

    master

    To deploy django-rq on Heroku:

    1. Ensure django-rq is in your requirements.txt (use pip freeze > requirements.txt).
    2. Update your Procfile to include a worker process that runs python <your_app_name>/manage.py rqworker <queues>.
    3. Commit and deploy your changes.
    4. Scale the worker process using the Heroku CLI.
    # requirements.txt
    django-rq
    
    # Procfile
    web: gunicorn --pythonpath="$PWD/your_app_name" config.wsgi:application
    worker: python your_app_name/manage.py rqworker high default low
  4. Set up the django-rq sample project

    master

    This sample project is used to test rqworker and site interaction. To set up the environment, follow these steps:

    1. Install PostgreSQL: Ensure PostgreSQL is installed on your system.
    2. Configure Database: Create a PostgreSQL user and database. The sample expects a user named djangorqusr and a database named djangorqdb.
    3. Initialize Schema: Run Django migrations to set up the database schema.
    4. Install Dependencies: Install the required Python packages using pip.

    Database Setup Commands (PostgreSQL CLI):

    # Note: These commands may require dropping existing user/db if they already exist
    # drop database djangorqdb;
    # drop user djangorqusr;
    create user djangorqusr with createrole superuser password 'djangorqusr';
    create database djangorqdb owner djangorqusr;

    Local Setup Commands:

    # Initialize database schema
    ./manage.py migrate
    
    # Install required packages
    pip install -r requirements.txt
    ./manage.py migrate
    pip install -r requirements.txt
  5. Schedule jobs with built-in scheduler or CronScheduler

    master

    Built-in Scheduler

    To use RQ 1.2.0+ built-in scheduling, use enqueue_at and start your worker with the --with-scheduler flag.

    RQ's CronScheduler

    For cron-style scheduling, create a configuration file and run rqcron.

    Example Cron Config:

    from rq import cron
    from myapp.tasks import send_report
    
    cron.register(send_report, queue_name='default', cron='0 9 * * *')

    Commands:

    • Start scheduler worker: python manage.py rqworker --with-scheduler
    • Run cron: python manage.py rqcron cron_config.py
    from datetime import datetime
    from django_rq.queues import get_queue
    
    queue = get_queue('default')
    job = queue.enqueue_at(datetime(2020, 10, 10), func)
  6. Use the Django Admin integration for monitoring

    master

    Django-RQ automatically integrates with the Django admin interface. Once installed, you can access the dashboard at /admin/django_rq/dashboard/ to monitor queue statistics, browse job registries (scheduled, started, finished, failed, deferred), manage workers, and view Prometheus metrics (if prometheus_client is installed).

    To disable the dashboard link in the admin sidebar, set RQ_SHOW_ADMIN_LINK = False in your settings.py.

  7. Configure standalone Django-RQ URLs

    master

    If you prefer not to use the Django admin interface for monitoring, you can include the Django-RQ views at a custom URL prefix in your urls.py.

    # urls.py
    from django.urls import path, include
    
    urlpatterns += [
        path('django-rq/', include('django_rq.urls'))
    ]
  8. Configure Prometheus metrics

    master

    To expose Prometheus-compatible metrics at /django-rq/metrics/, install prometheus_client or the extra django-rq[prometheus].

    If you need to access the metrics endpoint via other HTTP clients, define RQ_API_TOKEN in your settings and use it as a Bearer token in the Authorization header.

  9. Configure job enqueuing behavior with `COMMIT_MODE`

    master

    Django-RQ allows you to control when a job is actually sent to Redis relative to Django's database transactions. This is controlled via the COMMIT_MODE setting in your RQ configuration dictionary.

    Supported modes:

    • on_db_commit (default): The job is enqueued only after the current database transaction successfully commits. This prevents jobs from running if the database transaction rolls back.
    • auto: The job is enqueued immediately when enqueue is called.
    • request_finished: The job is enqueued when the Django request/response cycle finishes (legacy behavior).

    Note: The AUTOCOMMIT setting is deprecated; use COMMIT_MODE instead.

  10. View Job Details and Results

    master

    The job_detail view provides a deep dive into a specific job's lifecycle. It exposes:

    • Job Metadata: Function name and serialized data.
    • Results: Access to job results via result_detail using a specific result_id.
    • Error Information: If a job failed, exc_info contains the exception details.
    • Dependency Graph: Lists of dependencies (jobs this job depends on) and dependents (jobs that depend on this job).