Skip to main content

A Django app to register Django Q tasks.

Project description

django-q-registry

PyPI PyPI - Python Version Django Version

A Django app that provides a registry for Django Q2 periodic tasks.

Requirements

  • Python 3.8, 3.9, 3.10, 3.11, or 3.12
  • Django 3.2, 4.2, or 5.0
  • Django Q2 1.4.3 or later
    • This package has only been tested with the Django ORM broker. The other's may work, but they have not been tested.

Getting Started

  1. Install the package from PyPI:
python -m pip install django-q-registry
  1. Add the app to your Django project's INSTALLED_APPS:
INSTALLED_APPS = [
    ...,
    "django_q_registry",
    ...,
]

Usage

Registering Periodic Tasks

There are three supported ways to register periodic tasks:

  1. In a tasks.py file in a Django app, using the @register_task decorator:
# tasks.py
from django.core.mail import send_mail
from django_q.models import Schedule
from django_q_registry import register_task


@register_task(
    name="Send periodic test email",
    schedule_type=Schedule.CRON,
    # https://crontab.guru/#*/5_*_*_*_*
    cron="*/5 * * * *",
)
def send_test_email():
    send_mail(
        subject="Test email",
        message="This is a test email.",
        from_email="noreply@example.com",
        recipient_list=["johndoe@example.com"],
    )
  1. In a tasks.py file in a Django app, using the registry.register function directly:
# tasks.py
from django.core.mail import send_mail
from django_q.models import Schedule
from django_q_registry.registry import registry


registry.register(
    send_mail,
    name="Send periodic test email",
    kwargs={
        "subject": "Test email",
        "message": "This is a test email.",
        "from_email": "noreply@example.com",
        "recipient_list": ["janedoe@example.com"],
    },
    schedule_type=Schedule.CRON,
    # https://crontab.guru/#*/5_*_*_*_*
    cron="*/5 * * * *",
)
  1. In a Django project's settings.py file, using the Q_REGISTRY["TASKS"] setting:
# settings.py
from django_q.models import Schedule


Q_REGISTRY = {
    "TASKS": [
        {
            "name": "Send periodic test email",
            "func": "django.core.mail.send_mail",
            "kwargs": {
                "subject": "Test email",
                "message": "This is a test email.",
                "from_email": "noreply@example.com",
                "recipient_list": ["janedoe@example.com"],
            },
            "schedule_type": Schedule.CRON,
            # https://crontab.guru/#*/5_*_*_*_*
            "cron": "*/5 * * * *",
        },
    ],
}

Setting up Periodic Tasks in Production

At some point in your project's deployment process, run the setup_periodic_tasks management command:

python manage.py migrate
python manage.py setup_periodic_tasks

This command automatically registers periodic tasks from tasks.py files in Django apps, and from the Q_REGISTRY["TASKS"] setting. It also cleans up any periodic tasks that are no longer registered.

License

django-q-registry is licensed under the MIT license. See the LICENSE file for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_q_registry-0.1.0.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_q_registry-0.1.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file django_q_registry-0.1.0.tar.gz.

File metadata

  • Download URL: django_q_registry-0.1.0.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for django_q_registry-0.1.0.tar.gz
Algorithm Hash digest
SHA256 82760070644cdc5632383c3c5c3ac4555a1bff5ce32e0af66730f5d3118683ef
MD5 1ed632fb6bc1baa7b24bfa49dfcc7616
BLAKE2b-256 09527a1d4745f978880ad8526fd9902a54e94804415d172592a498fa870e9645

See more details on using hashes here.

File details

Details for the file django_q_registry-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_q_registry-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3d15973da36c0e2843f63d7d514919fabeee33d3a05c7a8e6fc2bd524915c057
MD5 7f70123ead2eeca3d58126d078046f56
BLAKE2b-256 91190d1eca7874ae195b90f96dab36770be9e986c6e9a3c0f60ca046879418d2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page