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 False to disable Sentry monitoring for a single task:

@cron("* * * * *", sentry_monitor_config=False)
@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-1.3.0.tar.gz (10.0 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.3.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_crontask-1.3.0.tar.gz
  • Upload date:
  • Size: 10.0 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.3.0.tar.gz
Algorithm Hash digest
SHA256 3e091224d172b77e43292858ff9016fc3a46cae3042752720d571c1942cf7c05
MD5 fd621c6380c0797d865767f14b3d9d94
BLAKE2b-256 f1d98006fc7b17163ea59d9569f84352783844d323241ee0cf44b4a1c1a301e9

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: django_crontask-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 10.4 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b71926c90bf4a33d865c1b8e75a9a74517f4515c1da5e2787d7006c07025eb25
MD5 802d9d30a270c653fa68588f82255bfc
BLAKE2b-256 437ddc8f9dc30bef2f1bfb1890be99dccf1f6449143a0518842b5baef0807094

See more details on using hashes here.

Provenance

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