Cron style scheduler for Django's task framework.
Project description
Django CronTask
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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ac69ee9cf52d196112e54e92e19995ba2962c8f45a4c90b821b110facb4b043
|
|
| MD5 |
3e15db317c89031604a775d1d34ac4f8
|
|
| BLAKE2b-256 |
4b0d5cfa032e3fa03d4c17eb070735c26128866b522f869ed6e1fda880caff7e
|
Provenance
The following attestation bundles were made for django_crontask-1.1.3.tar.gz:
Publisher:
release.yml on codingjoe/django-crontask
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_crontask-1.1.3.tar.gz -
Subject digest:
0ac69ee9cf52d196112e54e92e19995ba2962c8f45a4c90b821b110facb4b043 - Sigstore transparency entry: 831434517
- Sigstore integration time:
-
Permalink:
codingjoe/django-crontask@03b8d0b67295051b3bd3302faec88413d20d3d83 -
Branch / Tag:
refs/tags/1.1.3 - Owner: https://github.com/codingjoe
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@03b8d0b67295051b3bd3302faec88413d20d3d83 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_crontask-1.1.3-py3-none-any.whl.
File metadata
- Download URL: django_crontask-1.1.3-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60fc5eceaa159509372de7c82d07ae4540e1ff78ef4a482c6b87198760bfe468
|
|
| MD5 |
d7b24cebc99cef27666c8d1737a2abc0
|
|
| BLAKE2b-256 |
eceae04f5959ad46c8fecac0012c23c90237d27d7406986872d0b9379259e0c9
|
Provenance
The following attestation bundles were made for django_crontask-1.1.3-py3-none-any.whl:
Publisher:
release.yml on codingjoe/django-crontask
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_crontask-1.1.3-py3-none-any.whl -
Subject digest:
60fc5eceaa159509372de7c82d07ae4540e1ff78ef4a482c6b87198760bfe468 - Sigstore transparency entry: 831434546
- Sigstore integration time:
-
Permalink:
codingjoe/django-crontask@03b8d0b67295051b3bd3302faec88413d20d3d83 -
Branch / Tag:
refs/tags/1.1.3 - Owner: https://github.com/codingjoe
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@03b8d0b67295051b3bd3302faec88413d20d3d83 -
Trigger Event:
release
-
Statement type: