Metadata-Version: 2.4
Name: django-crontask
Version: 2.0.0
Summary: Cron style scheduler for Django's task framework.
Keywords: Django,cron,tasks,scheduler
Author-email: Rust Saiargaliev <fly.amureki@gmail.com>, Johannes Maron <johannes@maron.family>, Mostafa Mohamed <mostafa.anm91@gmail.com>, Jacqueline Kraus <jacquelinekraus1992@gmail.com>
Requires-Python: >=3.12
Description-Content-Type: text/markdown
Classifier: Development Status :: 5 - Production/Stable
Classifier: Programming Language :: Python
Classifier: Environment :: Web Environment
Classifier: License :: OSI Approved :: BSD License
Classifier: Intended Audience :: Developers
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Operating System :: POSIX
Classifier: Topic :: Communications :: Email
Classifier: Topic :: Text Processing :: Markup :: Markdown
Classifier: Topic :: Software Development
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Framework :: Django
Classifier: Framework :: Django :: 6.0
License-File: LICENSE
Requires-Dist: apscheduler
Requires-Dist: django>=6.0
Requires-Dist: redis ; extra == "redis"
Requires-Dist: sentry-sdk ; extra == "sentry"
Project-URL: Changelog, https://github.com/codingjoe/django-crontask/releases
Project-URL: Documentation, https://django-crontask.rtfd.io/
Project-URL: Funding, https://github.com/sponsors/codingjoe
Project-URL: Homepage, https://github.com/codingjoe/django-crontask
Project-URL: Issues, https://github.com/codingjoe/django-crontask/issues
Project-URL: Releasenotes, https://github.com/codingjoe/django-crontask/releases/latest
Project-URL: Source, https://github.com/codingjoe/django-crontask
Provides-Extra: redis
Provides-Extra: sentry

# Django CronTask

<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://github.com/codingjoe/django-crontask/raw/main/images/logo-dark.svg">
    <source media="(prefers-color-scheme: light)" srcset="https://github.com/codingjoe/django-crontask/raw/main/images/logo-light.svg">
    <img alt="Django CronTask: Cron style scheduler for Django's task framework" src="https://github.com/codingjoe/django-crontask/raw/main/images/logo-light.svg">
  </picture>
</p>

**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](https://img.shields.io/pypi/v/django-crontask.svg)](https://pypi.python.org/pypi/django-crontask/)
[![Test Coverage](https://codecov.io/gh/codingjoe/django-crontask/branch/main/graph/badge.svg)](https://codecov.io/gh/codingjoe/django-crontask)
[![GitHub License](https://img.shields.io/github/license/codingjoe/django-crontask)](https://raw.githubusercontent.com/codingjoe/django-crontask/master/LICENSE)

## Setup

You need to have [Django's Task framework][django-tasks] setup properly.

```ShellSession
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`:

```python
# settings.py
INSTALLED_APPS = [
    "crontask",
    # ...
]
```

Finally, you launch the scheduler in a separate process:

```ShellSession
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.

```python
# settings.py
CRONTASK = {
    "REDIS_URL": "redis://localhost:6379/0",
}
```

## Usage

```python
# 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.

```python
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:

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

### The crontask command

```ShellSession
$ 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.
```

[apscheduler]: https://apscheduler.readthedocs.io/en/stable/
[django-tasks]: https://docs.djangoproject.com/en/6.0/topics/tasks/
[sentry]: https://docs.sentry.io/product/crons/

