Skip to main content

Cron style scheduler for asynchronous Dramatiq tasks in Django.

Project description

Dramatiq Crontab

dramtiq-crontab logo: person in front of a schedule

Cron style scheduler for asynchronous Dramatiq tasks in Django.

  • setup recurring tasks via crontab syntax
  • lightweight helpers build on robust tools like Dramatiq and APScheduler
  • Sentry cron monitor support

PyPi Version Test Coverage GitHub License

Setup

You need to have Dramatiq installed and setup properly.

python3 -m pip install dramatiq-crontab
# or
python3 -m pip install dramatiq-crontab[sentry]  # with sentry cron monitor support

Add dramatiq_crontab to your INSTALLED_APPS in settings.py:

# settings.py
INSTALLED_APPS = [
    'dramatiq_crontab',
    # ...
]

Finally, you lauch the scheduler in a separate process:

python3 manage.py crontab

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
DRAMATIQ_CRONTAB = {
    "REDIS_URL": "redis://localhost:6379/0",
}

Usage

# tasks.py
import dramatiq
from dramatiq_crontab import cron


@cron("*/5 * * * *")  # every 5 minutes
@dramatiq.actor
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
import dramatiq
from dramatiq_crontab import interval


@interval(seconds=30)
@dramatiq.actor
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 dramatiq 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

dramatiq_crontab-1.0.12.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dramatiq_crontab-1.0.12-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file dramatiq_crontab-1.0.12.tar.gz.

File metadata

  • Download URL: dramatiq_crontab-1.0.12.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for dramatiq_crontab-1.0.12.tar.gz
Algorithm Hash digest
SHA256 6192289cb7fe16aa698cd6a78aa86e40ae4fb1fe043a55a028cfbf3c2299c8dd
MD5 7268b9bb7864d2984584906cc9514808
BLAKE2b-256 922713dd12312f7126f41d3c7787c8a07568a9026d5b546a2c237bb168a6bef2

See more details on using hashes here.

Provenance

The following attestation bundles were made for dramatiq_crontab-1.0.12.tar.gz:

Publisher: release.yml on voiio/dramatiq-crontab

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dramatiq_crontab-1.0.12-py3-none-any.whl.

File metadata

File hashes

Hashes for dramatiq_crontab-1.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 417d1da76fd423c02466db6e1091bb089cf76596331951447283fb9d6720ab8c
MD5 79bfb5bb537d87c247a452453a035051
BLAKE2b-256 1840802f5afda7f75b6ed61bd88fc71f96f94670ab5efd172e78ced3f82b67a0

See more details on using hashes here.

Provenance

The following attestation bundles were made for dramatiq_crontab-1.0.12-py3-none-any.whl:

Publisher: release.yml on voiio/dramatiq-crontab

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