Skip to main content

Cron style scheduler for Django's task framework.

Project description

Django CronTask

Django CronTask: Cron style scheduler for Django's task framework

Cron style scheduler for asynchronous tasks in Django.

  • setup recurring tasks via crontab syntax
  • lightweight helpers build on top of APScheduler
  • Sentry cron monitor support

PyPi Version Test Coverage GitHub License

Setup

You need to have Django's Task framework setup properly.

python3 -m pip install django-crontask
# or
python3 -m pip install django-crontask[sentry]  # with sentry cron monitor support

Add crontask to your INSTALLED_APPS in settings.py:

# settings.py
INSTALLED_APPS = [
    "crontask",
    # ...
]

Finally, you launch the scheduler in a separate process:

python3 manage.py crontask

Setup Redis as a lock backend (optional)

If you use Redis as a broker, you can use Redis as a lock backend as well. The lock backend is used to prevent multiple instances of the scheduler from running at the same time. This is important if you have multiple instances of your application running.

# settings.py
CRONTASK = {
    "REDIS_URL": "redis://localhost:6379/0",
}

Usage

# tasks.py
import logging

from django.tasks import task
from crontask import cron

logger = logging.getLogger(__name__)


@cron("*/5 * * * *")  # every 5 minutes
@task
def my_task():
    logger.info("Hello World")

Advanced Trigger Usage

You can also use other trigger types from APScheduler. Just import the trigger and use it in the cron decorator.

import logging

from django.tasks import task
from django.utils import timezone
from apscheduler.triggers.interval import IntervalTrigger
from crontask import cron

logger = logging.getLogger(__name__)

every_ten_minutes = IntervalTrigger(
    minutes=10, timezone=timezone.get_default_timezone()
)


@cron(every_ten_minutes)
@task
def my_interval_task():
    logger.info("Hello from interval task")

Sentry Cron Monitors

If you use Sentry you can add cron monitors to your tasks. The monitor's slug will be the actor's name. Like my_task in the example above.

The crontask command

$ python3 manage.py crontask --help
usage: manage.py crontask [-h] [--no-task-loading] [--no-heartbeat] [--version] [-v {0,1,2,3}]
                         [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color]
                         [--force-color] [--skip-checks]

Run task scheduler for all tasks with the `cron` decorator.

options:
  -h, --help            show this help message and exit
  --no-task-loading     Don't load tasks from installed apps.
  --no-heartbeat        Don't start the heartbeat actor.

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_crontask-1.1.3.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

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

django_crontask-1.1.3-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file django_crontask-1.1.3.tar.gz.

File metadata

  • Download URL: django_crontask-1.1.3.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_crontask-1.1.3.tar.gz
Algorithm Hash digest
SHA256 0ac69ee9cf52d196112e54e92e19995ba2962c8f45a4c90b821b110facb4b043
MD5 3e15db317c89031604a775d1d34ac4f8
BLAKE2b-256 4b0d5cfa032e3fa03d4c17eb070735c26128866b522f869ed6e1fda880caff7e

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_crontask-1.1.3.tar.gz:

Publisher: release.yml on codingjoe/django-crontask

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_crontask-1.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_crontask-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 60fc5eceaa159509372de7c82d07ae4540e1ff78ef4a482c6b87198760bfe468
MD5 d7b24cebc99cef27666c8d1737a2abc0
BLAKE2b-256 eceae04f5959ad46c8fecac0012c23c90237d27d7406986872d0b9379259e0c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_crontask-1.1.3-py3-none-any.whl:

Publisher: release.yml on codingjoe/django-crontask

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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