Database task backend and runner for Django tasks.
Project description
django-dbtasks
Database backend and runner for Django tasks (new in 6.0).
Quickstart
Install the django-dbtasks package from PyPI, and configure your TASKS setting as follows:
TASKS = {
"default": {
"BACKEND": "dbtasks.backend.DatabaseBackend",
"OPTIONS": {
# Set this to True to execute tasks immediately (no need for a runner).
"immediate": False,
# How long to retain ScheduleTasks in the database. Forever if not set.
"retain": datetime.timedelta(days=7),
# Tasks to run periodically.
"periodic": {
# Runs at 3:30am every Monday through Friday.
"myproject.tasks.maintenance": "30 3 * * 1-5",
},
},
},
}
Runner
django-dbtasks includes a dedicated taskrunner management command:
usage: manage.py taskrunner [-h] [-w WORKERS] [-i WORKER_ID] [--backend BACKEND] [--delay DELAY]
It is also straightforward to run the runner in a thread of its own:
runner = Runner(workers=4, worker_id="in-process")
t = threading.Thread(target=runner.run)
t.start()
...
runner.stop()
t.join()
django-dbtasks itself is tested on free-threading builds of Python 3.13 and 3.14, but compatibility will depend on your database driver and other packages.
Periodic Tasks
As shown in the quickstart, periodic tasks are specified as a mapping in the backend OPTIONS under the periodic key. The keys of the mapping should be dotted paths to the tasks, and the values should either be a string in crontab format, or an instance of dbtasks.Periodic. Using a dbtasks.Periodic allows you to specify args and kwargs (as values or callables) that will be passed to the task. For example, the Runner registers a periodic task to remove old tasks past the retention window, in a manner similar to:
# Convert the `timedelta` to seconds, so as to be JSON-serializable.
retain_secs = int(self.backend.options["retain"].total_seconds())
# Clear old tasks every hour, on a random minute.
self.periodic["dbtasks.runner.cleanup"] = Periodic("~ * * * *", args=[retain_secs])
Note that this allows you to specify a custom Periodic for the dbtasks.runner.cleanup task in your TASKS setting if necessary.
Logging
Be sure to add a dbtasks logger to your LOGGING setting:
LOGGING = {
...
"loggers": {
"dbtasks": {
"handlers": ["console"],
"level": "INFO",
},
},
}
Testing
There is a RunnerTestCase that starts a runner for the duration of a test suite. See test_tasks.py for example usage.
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_dbtasks-0.1.0.tar.gz.
File metadata
- Download URL: django_dbtasks-0.1.0.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed5f2fe6112a8212d8e545599850255159b7bd643e9a246d34e9095d4e80e3b4
|
|
| MD5 |
f1ea571021876b4c8fbe229ed96cb12d
|
|
| BLAKE2b-256 |
ed3252567fc9a76e7c30a6811136d0b8c7df4cee28f0788cc07a9c71739c8e04
|
File details
Details for the file django_dbtasks-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_dbtasks-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c002048e21a9b27eb47fc91c3617d50581c8be9200402761389634ee9466013a
|
|
| MD5 |
d5bf7e4454837f5105c92718965ef577
|
|
| BLAKE2b-256 |
da51a4e0b6dc6306a289310ff1c6c264a015f301e825d05305a776cc733833ae
|