Skip to content

voiio/dramatiq-crontab

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 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 lauch 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
from django.tasks import task
from crontask import cron


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

Interval

If you want to run a task more frequently than once a minute, you can use the interval decorator.

# tasks.py
from django.tasks import task
from crontask import interval


@interval(seconds=30)
@task
def my_task():
    my_task.logger.info("Hello World")

Please note that the interval is relative to the time the scheduler is started. For example, if you start the scheduler at 12:00:00, the first run will be at 12:00:30. However, if you restart the scheduler at 12:00:15, the first run will be at 12:00:45.

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 crontab command

$ python3 manage.py crontab --help
usage: manage.py crontab [-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.

About

Cron style scheduler for asynchronous Dramatiq tasks in Django.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

  •  
  •  

Contributors 7

Languages