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.

Pass a custom config via sentry_monitor_config or set it to None to disable Sentry monitoring for a single task:

@cron("* * * * *", sentry_monitor_config=None)
@task
def my_task_without_sentry(): ...

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-2.0.0.tar.gz (9.8 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-2.0.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for django_crontask-2.0.0.tar.gz
Algorithm Hash digest
SHA256 1367aaa5be6f401c5fa4516f484208ae2c8c6b722fb56bb88e1383f37f9b459d
MD5 e7037df5de6cf6b67b1084b2d4f0e2a8
BLAKE2b-256 265828859776c9c5df7adc1c985342e44813bb271401015043a44429e69eac8f

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: django_crontask-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 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-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 434c1e2dacd97d2d39b2d85db9082573a4ab7bce8a72be5c5a7f5808ac5003e3
MD5 a3f9477647d4d8c7415fd1d7078a8e99
BLAKE2b-256 1e6cb87d197ac4beaebf967206181273c3015f9476ac70f57a22d7bf8d42147e

See more details on using hashes here.

Provenance

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