A Django Tasks backend which uses Django's ORM to store tasks in the database.
python -m pip install django-tasks-db
First, add django_tasks_db to your INSTALLED_APPS:
INSTALLED_APPS = [
# ...
"django_tasks_db",
]Finally, add it to your TASKS configuration:
TASKS = {
"default": {
"BACKEND": "django_tasks_db.DatabaseBackend",
"QUEUES": ["default"]
}
}You can run the db_worker command to run tasks as they're created. Check the --help for more options.
./manage.py db_workerIn DEBUG, the worker will automatically reload when code is changed (or by using --reload). This is not recommended in production environments as tasks may not be stopped cleanly.
After a while, tasks may start to build up in your database. This can be managed using the prune_db_task_results management command, which deletes completed tasks according to the given retention policy. Check the --help for the available options.
By default, the database worker uses uuid.uuid4 to generate a task id. This can be customized using the id_function option:
TASKS = {
"default": {
"BACKEND": "django_tasks_db.DatabaseBackend",
"OPTIONS": {
"id_function": "uuid.uuid7"
}
}
}The id_function must return a UUID (either uuid.UUID or string representation). Additionally, the PostgreSQL-specific RandomUUID or other database expressions are supported on Django 6.0+.
See CONTRIBUTING.md for information on how to contribute.
Note: Prior to 0.12.0, this backend was included in django-tasks. Whilst the commit history was cleaned up, it's still quite messy. Don't look too closely.