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 suitable tasks will be automatically monitored. The monitor's slug will be the actor's name. Like my_task in the example above.

Certain triggers are not supported by Sentry as well as sub-minute intervals. In these cases, no monitor will be created and no telemetry will be sent.

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.2.0.tar.gz (9.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.2.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for django_crontask-1.2.0.tar.gz
Algorithm Hash digest
SHA256 49378661b8b8cd3b7d756585fd9203c23ceeb37e3489f38b30e4933867f24658
MD5 383058d60ff33ac0c04c101df511c503
BLAKE2b-256 c7fd98d8ddacb4c5c7cb48d0a479261abb74d1f5da7d455ae438ce9d5a52206f

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_crontask-1.2.0.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.2.0-py3-none-any.whl.

File metadata

  • Download URL: django_crontask-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_crontask-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a0660554d17220239a096f2858f95ca2c2e6ae31b4f919a7a2237917d76f34b7
MD5 742c8e4080ed09b5a1f12a9284f6474f
BLAKE2b-256 67703fe937ca481eae1adc2dd8a40dba38429245bad20ff82950167f8f59b323

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_crontask-1.2.0-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