django-redis-tasks 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_redis_tasks-1.0.0/LICENSE.md +21 -0
- django_redis_tasks-1.0.0/PKG-INFO +186 -0
- django_redis_tasks-1.0.0/README.md +163 -0
- django_redis_tasks-1.0.0/django_redis_tasks.egg-info/PKG-INFO +186 -0
- django_redis_tasks-1.0.0/django_redis_tasks.egg-info/SOURCES.txt +17 -0
- django_redis_tasks-1.0.0/django_redis_tasks.egg-info/dependency_links.txt +1 -0
- django_redis_tasks-1.0.0/django_redis_tasks.egg-info/requires.txt +2 -0
- django_redis_tasks-1.0.0/django_redis_tasks.egg-info/top_level.txt +1 -0
- django_redis_tasks-1.0.0/pyproject.toml +57 -0
- django_redis_tasks-1.0.0/redis_tasks/__init__.py +0 -0
- django_redis_tasks-1.0.0/redis_tasks/apps.py +6 -0
- django_redis_tasks-1.0.0/redis_tasks/backend.py +326 -0
- django_redis_tasks-1.0.0/redis_tasks/entries.py +10 -0
- django_redis_tasks-1.0.0/redis_tasks/handlers.py +34 -0
- django_redis_tasks-1.0.0/redis_tasks/worker.py +71 -0
- django_redis_tasks-1.0.0/setup.cfg +4 -0
- django_redis_tasks-1.0.0/tests/test_handlers.py +160 -0
- django_redis_tasks-1.0.0/tests/test_redis_backend.py +862 -0
- django_redis_tasks-1.0.0/tests/test_worker.py +161 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
### Copyright (c) 2021-2026 David L Nugent
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
> The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: django-redis-tasks
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Django tasks backend using Redis queues
|
|
5
|
+
Author-email: David Nugent <davidn@uniquode.io>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Source, https://github.com/deeprave/django-redis-tasks
|
|
8
|
+
Project-URL: Issues, https://github.com/deeprave/django-redis-tasks/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/deeprave/django-redis-tasks/releases
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
13
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
14
|
+
Classifier: Environment :: Web Environment
|
|
15
|
+
Classifier: Framework :: Django
|
|
16
|
+
Classifier: Framework :: Django :: 6.1
|
|
17
|
+
Requires-Python: >=3.14
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE.md
|
|
20
|
+
Requires-Dist: django>=6.1
|
|
21
|
+
Requires-Dist: django-queues[redis]<2.0.0,>=1.2.0
|
|
22
|
+
Dynamic: license-file
|
|
23
|
+
|
|
24
|
+
# Django Redis Tasks
|
|
25
|
+
|
|
26
|
+
[](https://github.com/deeprave/django-redis-tasks/actions/workflows/python-test-and-build.yml)
|
|
27
|
+
[](https://github.com/deeprave/django-redis-tasks/security/code-scanning)
|
|
28
|
+
[](https://github.com/deeprave/django-redis-tasks)
|
|
29
|
+
[](https://pypi.org/project/django-redis-tasks/)
|
|
30
|
+
[](https://pypi.org/project/django-redis-tasks/)
|
|
31
|
+
[](https://pypi.org/project/django-redis-tasks/)
|
|
32
|
+
|
|
33
|
+
This is a Redis-backed implementation of [Django's `django.tasks`](https://docs.djangoproject.com/en/6.1/topics/tasks/)
|
|
34
|
+
framework.
|
|
35
|
+
|
|
36
|
+
## django-queues
|
|
37
|
+
|
|
38
|
+
Queue storage and worker execution are provided by [`django-queues`](https://pypi.org/project/django-queues/), a
|
|
39
|
+
generic Django queue package. `django-redis-tasks` is a thin translation layer on top of it: it maps Django's task
|
|
40
|
+
vocabulary (`Task`, `TaskResult`, `TaskResultStatus`) onto `django-queues`' generic queue vocabulary (`QueueEntry`,
|
|
41
|
+
`QueueEntryStatus`), and provides the handler function that executes queued task calls.
|
|
42
|
+
|
|
43
|
+
This package does not manage Redis connections, run a worker loop, or start anything during Django's application
|
|
44
|
+
startup — `django-queues` owns all of that, including its `manage.py runqueues` management command.
|
|
45
|
+
|
|
46
|
+
## Deferred tasks
|
|
47
|
+
|
|
48
|
+
`run_after` is supported. The backend normalizes the datetime to UTC at enqueue: that UTC instant is stored on the task payload and passed to django-queues as `available_at`. Reconstruction therefore does not depend on the reader's `USE_TZ` or `TIME_ZONE`. Redis TIME decides when the entry becomes claimable; workers do not record an attempt or emit started/finished signals while the task is only waiting.
|
|
49
|
+
|
|
50
|
+
- **Aware datetimes** are converted to UTC.
|
|
51
|
+
- **Naive datetimes** are accepted only when `USE_TZ` is `False`; they are interpreted in Django's current timezone, then converted to UTC. When `USE_TZ` is `True`, Django rejects a naive `run_after` at enqueue. Legacy naive payload values are still normalized to UTC on read.
|
|
52
|
+
- A past or current `run_after` is eligible for dispatch immediately.
|
|
53
|
+
- A worker that has no due work waits until a deferred task becomes due; it does not busy-poll the application clock.
|
|
54
|
+
|
|
55
|
+
## Redis runtime
|
|
56
|
+
|
|
57
|
+
django-queues 1.2.0 requires **Redis 7+** and a deployed Function library before the application or a worker starts:
|
|
58
|
+
|
|
59
|
+
```shell
|
|
60
|
+
python manage.py redis_lua_lib --deploy
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Queue aliases must match `[A-Za-z0-9_-]`. Redis keys from django-queues before 1.2.0 are not compatible; start from empty queue keys or a new Redis logical database.
|
|
64
|
+
|
|
65
|
+
## Installation
|
|
66
|
+
|
|
67
|
+
```shell
|
|
68
|
+
uv add django-redis-tasks
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Configuration
|
|
72
|
+
|
|
73
|
+
`django_queue` must be added to `INSTALLED_APPS`. `redis_tasks` does not require
|
|
74
|
+
app registration today, but adding it is recommended for forward compatibility
|
|
75
|
+
with future Django integration points such as models, observer registrations,
|
|
76
|
+
templates, and static assets:
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
INSTALLED_APPS = [
|
|
80
|
+
...
|
|
81
|
+
"django_queue",
|
|
82
|
+
"redis_tasks",
|
|
83
|
+
...
|
|
84
|
+
]
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Configuration is two settings working together: `QUEUES` (owned by `django-queues`) configures the underlying Redis
|
|
88
|
+
queue and registers this package's handler; `TASKS` (owned by Django) configures the task backend and points it at
|
|
89
|
+
that queue.
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
QUEUES = {
|
|
93
|
+
"default": {
|
|
94
|
+
"BACKEND": "django_queue.backends.redis.RedisAsyncPriorityQueueJson",
|
|
95
|
+
"LOCATION": "redis://localhost:6379/12",
|
|
96
|
+
"HANDLER": "redis_tasks.handlers.handle_task_entry",
|
|
97
|
+
"ENTRY_CLASS": "redis_tasks.entries.TaskQueueEntry",
|
|
98
|
+
"WORKER": "redis_tasks.worker.RedisTaskWorker",
|
|
99
|
+
},
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
TASKS = {
|
|
103
|
+
"default": {
|
|
104
|
+
"BACKEND": "redis_tasks.backend.RedisBackend",
|
|
105
|
+
"OPTIONS": {
|
|
106
|
+
"queue_alias": "default", # the QUEUES alias to delegate to; defaults to this TASKS alias
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
A Django project may configure multiple `QUEUES`/`TASKS` alias pairs, each backed by its own Redis queue.
|
|
113
|
+
|
|
114
|
+
`TaskQueueEntry` records `worker_ids` as the ordered history of worker-process UUIDs that began dispatching a task;
|
|
115
|
+
`TaskResult.attempts` is the length of that list. Redis automatically recovers an entry whose worker lease expires, so a
|
|
116
|
+
task can execute more than once. Make handlers with side effects idempotent.
|
|
117
|
+
|
|
118
|
+
## Running a worker
|
|
119
|
+
|
|
120
|
+
Task execution happens out-of-process, via `django-queues`' own management command:
|
|
121
|
+
|
|
122
|
+
```shell
|
|
123
|
+
python manage.py runqueues
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
This discovers every `QUEUES` alias with a `HANDLER` configured (including `redis_tasks.handlers.handle_task_entry`)
|
|
127
|
+
and runs its worker until the process receives a termination signal. Queue connections are initialized lazily when
|
|
128
|
+
their aliases are first used, but no task worker or handler execution starts in a Django web process — run `runqueues`
|
|
129
|
+
as a separate, standalone service.
|
|
130
|
+
|
|
131
|
+
## Async applications
|
|
132
|
+
|
|
133
|
+
`django-redis-tasks` supports Django's native asynchronous task API. In an
|
|
134
|
+
ASGI view, consumer, or other coroutine, use `await task.aenqueue(...)` to
|
|
135
|
+
write directly through django-queues' asynchronous Redis API. Refresh an
|
|
136
|
+
existing result with `await result.arefresh()`. Neither operation needs this
|
|
137
|
+
backend to cross Django's synchronous task-backend bridge, so queue I/O stays
|
|
138
|
+
on the caller's event loop.
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
from django.http import JsonResponse
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
async def start_report(request):
|
|
145
|
+
result = await build_report.aenqueue(request.user.pk)
|
|
146
|
+
return JsonResponse({"task_id": result.id})
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
async def refresh_report(result):
|
|
150
|
+
await result.arefresh()
|
|
151
|
+
return result.status
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Tasks declared with `async def` are also awaited directly by this package's
|
|
155
|
+
handler inside django-queues' worker event loop. A synchronous task function
|
|
156
|
+
is still safe, but Django runs it through a thread bridge to avoid blocking
|
|
157
|
+
that loop.
|
|
158
|
+
|
|
159
|
+
Queue work is deliberately outside the ASGI request runtime. The separate
|
|
160
|
+
`runqueues` service owns task dispatch and recovery, while django-queues'
|
|
161
|
+
process-local queue runtime owns lifecycle-observer delivery. This lets an
|
|
162
|
+
application use task status and best-effort queue observers without starting
|
|
163
|
+
workers from views or competing with request handling for an event loop.
|
|
164
|
+
|
|
165
|
+
An entirely asynchronous application should also use async middleware, views,
|
|
166
|
+
task functions, and task-signal receivers. Synchronous middleware, ORM work,
|
|
167
|
+
task functions, and signal receivers remain supported, but Django adapts those
|
|
168
|
+
specific boundaries through its thread bridge.
|
|
169
|
+
|
|
170
|
+
## Usage
|
|
171
|
+
|
|
172
|
+
A task is created using Django's `@task` decorator:
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
from django.tasks import task
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
@task()
|
|
179
|
+
def calculate_meaning_of_life() -> int:
|
|
180
|
+
return 42
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
@task()
|
|
184
|
+
async def nudge_nudge_wink_wink() -> list[str]:
|
|
185
|
+
return ["say", "no", "more"]
|
|
186
|
+
```
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# Django Redis Tasks
|
|
2
|
+
|
|
3
|
+
[](https://github.com/deeprave/django-redis-tasks/actions/workflows/python-test-and-build.yml)
|
|
4
|
+
[](https://github.com/deeprave/django-redis-tasks/security/code-scanning)
|
|
5
|
+
[](https://github.com/deeprave/django-redis-tasks)
|
|
6
|
+
[](https://pypi.org/project/django-redis-tasks/)
|
|
7
|
+
[](https://pypi.org/project/django-redis-tasks/)
|
|
8
|
+
[](https://pypi.org/project/django-redis-tasks/)
|
|
9
|
+
|
|
10
|
+
This is a Redis-backed implementation of [Django's `django.tasks`](https://docs.djangoproject.com/en/6.1/topics/tasks/)
|
|
11
|
+
framework.
|
|
12
|
+
|
|
13
|
+
## django-queues
|
|
14
|
+
|
|
15
|
+
Queue storage and worker execution are provided by [`django-queues`](https://pypi.org/project/django-queues/), a
|
|
16
|
+
generic Django queue package. `django-redis-tasks` is a thin translation layer on top of it: it maps Django's task
|
|
17
|
+
vocabulary (`Task`, `TaskResult`, `TaskResultStatus`) onto `django-queues`' generic queue vocabulary (`QueueEntry`,
|
|
18
|
+
`QueueEntryStatus`), and provides the handler function that executes queued task calls.
|
|
19
|
+
|
|
20
|
+
This package does not manage Redis connections, run a worker loop, or start anything during Django's application
|
|
21
|
+
startup — `django-queues` owns all of that, including its `manage.py runqueues` management command.
|
|
22
|
+
|
|
23
|
+
## Deferred tasks
|
|
24
|
+
|
|
25
|
+
`run_after` is supported. The backend normalizes the datetime to UTC at enqueue: that UTC instant is stored on the task payload and passed to django-queues as `available_at`. Reconstruction therefore does not depend on the reader's `USE_TZ` or `TIME_ZONE`. Redis TIME decides when the entry becomes claimable; workers do not record an attempt or emit started/finished signals while the task is only waiting.
|
|
26
|
+
|
|
27
|
+
- **Aware datetimes** are converted to UTC.
|
|
28
|
+
- **Naive datetimes** are accepted only when `USE_TZ` is `False`; they are interpreted in Django's current timezone, then converted to UTC. When `USE_TZ` is `True`, Django rejects a naive `run_after` at enqueue. Legacy naive payload values are still normalized to UTC on read.
|
|
29
|
+
- A past or current `run_after` is eligible for dispatch immediately.
|
|
30
|
+
- A worker that has no due work waits until a deferred task becomes due; it does not busy-poll the application clock.
|
|
31
|
+
|
|
32
|
+
## Redis runtime
|
|
33
|
+
|
|
34
|
+
django-queues 1.2.0 requires **Redis 7+** and a deployed Function library before the application or a worker starts:
|
|
35
|
+
|
|
36
|
+
```shell
|
|
37
|
+
python manage.py redis_lua_lib --deploy
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Queue aliases must match `[A-Za-z0-9_-]`. Redis keys from django-queues before 1.2.0 are not compatible; start from empty queue keys or a new Redis logical database.
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
```shell
|
|
45
|
+
uv add django-redis-tasks
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Configuration
|
|
49
|
+
|
|
50
|
+
`django_queue` must be added to `INSTALLED_APPS`. `redis_tasks` does not require
|
|
51
|
+
app registration today, but adding it is recommended for forward compatibility
|
|
52
|
+
with future Django integration points such as models, observer registrations,
|
|
53
|
+
templates, and static assets:
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
INSTALLED_APPS = [
|
|
57
|
+
...
|
|
58
|
+
"django_queue",
|
|
59
|
+
"redis_tasks",
|
|
60
|
+
...
|
|
61
|
+
]
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Configuration is two settings working together: `QUEUES` (owned by `django-queues`) configures the underlying Redis
|
|
65
|
+
queue and registers this package's handler; `TASKS` (owned by Django) configures the task backend and points it at
|
|
66
|
+
that queue.
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
QUEUES = {
|
|
70
|
+
"default": {
|
|
71
|
+
"BACKEND": "django_queue.backends.redis.RedisAsyncPriorityQueueJson",
|
|
72
|
+
"LOCATION": "redis://localhost:6379/12",
|
|
73
|
+
"HANDLER": "redis_tasks.handlers.handle_task_entry",
|
|
74
|
+
"ENTRY_CLASS": "redis_tasks.entries.TaskQueueEntry",
|
|
75
|
+
"WORKER": "redis_tasks.worker.RedisTaskWorker",
|
|
76
|
+
},
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
TASKS = {
|
|
80
|
+
"default": {
|
|
81
|
+
"BACKEND": "redis_tasks.backend.RedisBackend",
|
|
82
|
+
"OPTIONS": {
|
|
83
|
+
"queue_alias": "default", # the QUEUES alias to delegate to; defaults to this TASKS alias
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
A Django project may configure multiple `QUEUES`/`TASKS` alias pairs, each backed by its own Redis queue.
|
|
90
|
+
|
|
91
|
+
`TaskQueueEntry` records `worker_ids` as the ordered history of worker-process UUIDs that began dispatching a task;
|
|
92
|
+
`TaskResult.attempts` is the length of that list. Redis automatically recovers an entry whose worker lease expires, so a
|
|
93
|
+
task can execute more than once. Make handlers with side effects idempotent.
|
|
94
|
+
|
|
95
|
+
## Running a worker
|
|
96
|
+
|
|
97
|
+
Task execution happens out-of-process, via `django-queues`' own management command:
|
|
98
|
+
|
|
99
|
+
```shell
|
|
100
|
+
python manage.py runqueues
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
This discovers every `QUEUES` alias with a `HANDLER` configured (including `redis_tasks.handlers.handle_task_entry`)
|
|
104
|
+
and runs its worker until the process receives a termination signal. Queue connections are initialized lazily when
|
|
105
|
+
their aliases are first used, but no task worker or handler execution starts in a Django web process — run `runqueues`
|
|
106
|
+
as a separate, standalone service.
|
|
107
|
+
|
|
108
|
+
## Async applications
|
|
109
|
+
|
|
110
|
+
`django-redis-tasks` supports Django's native asynchronous task API. In an
|
|
111
|
+
ASGI view, consumer, or other coroutine, use `await task.aenqueue(...)` to
|
|
112
|
+
write directly through django-queues' asynchronous Redis API. Refresh an
|
|
113
|
+
existing result with `await result.arefresh()`. Neither operation needs this
|
|
114
|
+
backend to cross Django's synchronous task-backend bridge, so queue I/O stays
|
|
115
|
+
on the caller's event loop.
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
from django.http import JsonResponse
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
async def start_report(request):
|
|
122
|
+
result = await build_report.aenqueue(request.user.pk)
|
|
123
|
+
return JsonResponse({"task_id": result.id})
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
async def refresh_report(result):
|
|
127
|
+
await result.arefresh()
|
|
128
|
+
return result.status
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Tasks declared with `async def` are also awaited directly by this package's
|
|
132
|
+
handler inside django-queues' worker event loop. A synchronous task function
|
|
133
|
+
is still safe, but Django runs it through a thread bridge to avoid blocking
|
|
134
|
+
that loop.
|
|
135
|
+
|
|
136
|
+
Queue work is deliberately outside the ASGI request runtime. The separate
|
|
137
|
+
`runqueues` service owns task dispatch and recovery, while django-queues'
|
|
138
|
+
process-local queue runtime owns lifecycle-observer delivery. This lets an
|
|
139
|
+
application use task status and best-effort queue observers without starting
|
|
140
|
+
workers from views or competing with request handling for an event loop.
|
|
141
|
+
|
|
142
|
+
An entirely asynchronous application should also use async middleware, views,
|
|
143
|
+
task functions, and task-signal receivers. Synchronous middleware, ORM work,
|
|
144
|
+
task functions, and signal receivers remain supported, but Django adapts those
|
|
145
|
+
specific boundaries through its thread bridge.
|
|
146
|
+
|
|
147
|
+
## Usage
|
|
148
|
+
|
|
149
|
+
A task is created using Django's `@task` decorator:
|
|
150
|
+
|
|
151
|
+
```python
|
|
152
|
+
from django.tasks import task
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
@task()
|
|
156
|
+
def calculate_meaning_of_life() -> int:
|
|
157
|
+
return 42
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
@task()
|
|
161
|
+
async def nudge_nudge_wink_wink() -> list[str]:
|
|
162
|
+
return ["say", "no", "more"]
|
|
163
|
+
```
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: django-redis-tasks
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Django tasks backend using Redis queues
|
|
5
|
+
Author-email: David Nugent <davidn@uniquode.io>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Source, https://github.com/deeprave/django-redis-tasks
|
|
8
|
+
Project-URL: Issues, https://github.com/deeprave/django-redis-tasks/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/deeprave/django-redis-tasks/releases
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
13
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
14
|
+
Classifier: Environment :: Web Environment
|
|
15
|
+
Classifier: Framework :: Django
|
|
16
|
+
Classifier: Framework :: Django :: 6.1
|
|
17
|
+
Requires-Python: >=3.14
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE.md
|
|
20
|
+
Requires-Dist: django>=6.1
|
|
21
|
+
Requires-Dist: django-queues[redis]<2.0.0,>=1.2.0
|
|
22
|
+
Dynamic: license-file
|
|
23
|
+
|
|
24
|
+
# Django Redis Tasks
|
|
25
|
+
|
|
26
|
+
[](https://github.com/deeprave/django-redis-tasks/actions/workflows/python-test-and-build.yml)
|
|
27
|
+
[](https://github.com/deeprave/django-redis-tasks/security/code-scanning)
|
|
28
|
+
[](https://github.com/deeprave/django-redis-tasks)
|
|
29
|
+
[](https://pypi.org/project/django-redis-tasks/)
|
|
30
|
+
[](https://pypi.org/project/django-redis-tasks/)
|
|
31
|
+
[](https://pypi.org/project/django-redis-tasks/)
|
|
32
|
+
|
|
33
|
+
This is a Redis-backed implementation of [Django's `django.tasks`](https://docs.djangoproject.com/en/6.1/topics/tasks/)
|
|
34
|
+
framework.
|
|
35
|
+
|
|
36
|
+
## django-queues
|
|
37
|
+
|
|
38
|
+
Queue storage and worker execution are provided by [`django-queues`](https://pypi.org/project/django-queues/), a
|
|
39
|
+
generic Django queue package. `django-redis-tasks` is a thin translation layer on top of it: it maps Django's task
|
|
40
|
+
vocabulary (`Task`, `TaskResult`, `TaskResultStatus`) onto `django-queues`' generic queue vocabulary (`QueueEntry`,
|
|
41
|
+
`QueueEntryStatus`), and provides the handler function that executes queued task calls.
|
|
42
|
+
|
|
43
|
+
This package does not manage Redis connections, run a worker loop, or start anything during Django's application
|
|
44
|
+
startup — `django-queues` owns all of that, including its `manage.py runqueues` management command.
|
|
45
|
+
|
|
46
|
+
## Deferred tasks
|
|
47
|
+
|
|
48
|
+
`run_after` is supported. The backend normalizes the datetime to UTC at enqueue: that UTC instant is stored on the task payload and passed to django-queues as `available_at`. Reconstruction therefore does not depend on the reader's `USE_TZ` or `TIME_ZONE`. Redis TIME decides when the entry becomes claimable; workers do not record an attempt or emit started/finished signals while the task is only waiting.
|
|
49
|
+
|
|
50
|
+
- **Aware datetimes** are converted to UTC.
|
|
51
|
+
- **Naive datetimes** are accepted only when `USE_TZ` is `False`; they are interpreted in Django's current timezone, then converted to UTC. When `USE_TZ` is `True`, Django rejects a naive `run_after` at enqueue. Legacy naive payload values are still normalized to UTC on read.
|
|
52
|
+
- A past or current `run_after` is eligible for dispatch immediately.
|
|
53
|
+
- A worker that has no due work waits until a deferred task becomes due; it does not busy-poll the application clock.
|
|
54
|
+
|
|
55
|
+
## Redis runtime
|
|
56
|
+
|
|
57
|
+
django-queues 1.2.0 requires **Redis 7+** and a deployed Function library before the application or a worker starts:
|
|
58
|
+
|
|
59
|
+
```shell
|
|
60
|
+
python manage.py redis_lua_lib --deploy
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Queue aliases must match `[A-Za-z0-9_-]`. Redis keys from django-queues before 1.2.0 are not compatible; start from empty queue keys or a new Redis logical database.
|
|
64
|
+
|
|
65
|
+
## Installation
|
|
66
|
+
|
|
67
|
+
```shell
|
|
68
|
+
uv add django-redis-tasks
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Configuration
|
|
72
|
+
|
|
73
|
+
`django_queue` must be added to `INSTALLED_APPS`. `redis_tasks` does not require
|
|
74
|
+
app registration today, but adding it is recommended for forward compatibility
|
|
75
|
+
with future Django integration points such as models, observer registrations,
|
|
76
|
+
templates, and static assets:
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
INSTALLED_APPS = [
|
|
80
|
+
...
|
|
81
|
+
"django_queue",
|
|
82
|
+
"redis_tasks",
|
|
83
|
+
...
|
|
84
|
+
]
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Configuration is two settings working together: `QUEUES` (owned by `django-queues`) configures the underlying Redis
|
|
88
|
+
queue and registers this package's handler; `TASKS` (owned by Django) configures the task backend and points it at
|
|
89
|
+
that queue.
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
QUEUES = {
|
|
93
|
+
"default": {
|
|
94
|
+
"BACKEND": "django_queue.backends.redis.RedisAsyncPriorityQueueJson",
|
|
95
|
+
"LOCATION": "redis://localhost:6379/12",
|
|
96
|
+
"HANDLER": "redis_tasks.handlers.handle_task_entry",
|
|
97
|
+
"ENTRY_CLASS": "redis_tasks.entries.TaskQueueEntry",
|
|
98
|
+
"WORKER": "redis_tasks.worker.RedisTaskWorker",
|
|
99
|
+
},
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
TASKS = {
|
|
103
|
+
"default": {
|
|
104
|
+
"BACKEND": "redis_tasks.backend.RedisBackend",
|
|
105
|
+
"OPTIONS": {
|
|
106
|
+
"queue_alias": "default", # the QUEUES alias to delegate to; defaults to this TASKS alias
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
A Django project may configure multiple `QUEUES`/`TASKS` alias pairs, each backed by its own Redis queue.
|
|
113
|
+
|
|
114
|
+
`TaskQueueEntry` records `worker_ids` as the ordered history of worker-process UUIDs that began dispatching a task;
|
|
115
|
+
`TaskResult.attempts` is the length of that list. Redis automatically recovers an entry whose worker lease expires, so a
|
|
116
|
+
task can execute more than once. Make handlers with side effects idempotent.
|
|
117
|
+
|
|
118
|
+
## Running a worker
|
|
119
|
+
|
|
120
|
+
Task execution happens out-of-process, via `django-queues`' own management command:
|
|
121
|
+
|
|
122
|
+
```shell
|
|
123
|
+
python manage.py runqueues
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
This discovers every `QUEUES` alias with a `HANDLER` configured (including `redis_tasks.handlers.handle_task_entry`)
|
|
127
|
+
and runs its worker until the process receives a termination signal. Queue connections are initialized lazily when
|
|
128
|
+
their aliases are first used, but no task worker or handler execution starts in a Django web process — run `runqueues`
|
|
129
|
+
as a separate, standalone service.
|
|
130
|
+
|
|
131
|
+
## Async applications
|
|
132
|
+
|
|
133
|
+
`django-redis-tasks` supports Django's native asynchronous task API. In an
|
|
134
|
+
ASGI view, consumer, or other coroutine, use `await task.aenqueue(...)` to
|
|
135
|
+
write directly through django-queues' asynchronous Redis API. Refresh an
|
|
136
|
+
existing result with `await result.arefresh()`. Neither operation needs this
|
|
137
|
+
backend to cross Django's synchronous task-backend bridge, so queue I/O stays
|
|
138
|
+
on the caller's event loop.
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
from django.http import JsonResponse
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
async def start_report(request):
|
|
145
|
+
result = await build_report.aenqueue(request.user.pk)
|
|
146
|
+
return JsonResponse({"task_id": result.id})
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
async def refresh_report(result):
|
|
150
|
+
await result.arefresh()
|
|
151
|
+
return result.status
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Tasks declared with `async def` are also awaited directly by this package's
|
|
155
|
+
handler inside django-queues' worker event loop. A synchronous task function
|
|
156
|
+
is still safe, but Django runs it through a thread bridge to avoid blocking
|
|
157
|
+
that loop.
|
|
158
|
+
|
|
159
|
+
Queue work is deliberately outside the ASGI request runtime. The separate
|
|
160
|
+
`runqueues` service owns task dispatch and recovery, while django-queues'
|
|
161
|
+
process-local queue runtime owns lifecycle-observer delivery. This lets an
|
|
162
|
+
application use task status and best-effort queue observers without starting
|
|
163
|
+
workers from views or competing with request handling for an event loop.
|
|
164
|
+
|
|
165
|
+
An entirely asynchronous application should also use async middleware, views,
|
|
166
|
+
task functions, and task-signal receivers. Synchronous middleware, ORM work,
|
|
167
|
+
task functions, and signal receivers remain supported, but Django adapts those
|
|
168
|
+
specific boundaries through its thread bridge.
|
|
169
|
+
|
|
170
|
+
## Usage
|
|
171
|
+
|
|
172
|
+
A task is created using Django's `@task` decorator:
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
from django.tasks import task
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
@task()
|
|
179
|
+
def calculate_meaning_of_life() -> int:
|
|
180
|
+
return 42
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
@task()
|
|
184
|
+
async def nudge_nudge_wink_wink() -> list[str]:
|
|
185
|
+
return ["say", "no", "more"]
|
|
186
|
+
```
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
LICENSE.md
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
django_redis_tasks.egg-info/PKG-INFO
|
|
5
|
+
django_redis_tasks.egg-info/SOURCES.txt
|
|
6
|
+
django_redis_tasks.egg-info/dependency_links.txt
|
|
7
|
+
django_redis_tasks.egg-info/requires.txt
|
|
8
|
+
django_redis_tasks.egg-info/top_level.txt
|
|
9
|
+
redis_tasks/__init__.py
|
|
10
|
+
redis_tasks/apps.py
|
|
11
|
+
redis_tasks/backend.py
|
|
12
|
+
redis_tasks/entries.py
|
|
13
|
+
redis_tasks/handlers.py
|
|
14
|
+
redis_tasks/worker.py
|
|
15
|
+
tests/test_handlers.py
|
|
16
|
+
tests/test_redis_backend.py
|
|
17
|
+
tests/test_worker.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
redis_tasks
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "django-redis-tasks"
|
|
3
|
+
version = "1.0.0"
|
|
4
|
+
description = "Django tasks backend using Redis queues"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE.md"]
|
|
8
|
+
requires-python = ">=3.14"
|
|
9
|
+
authors = [
|
|
10
|
+
{ name = "David Nugent", email = "davidn@uniquode.io" }
|
|
11
|
+
]
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Development Status :: 5 - Production/Stable",
|
|
14
|
+
"Programming Language :: Python :: 3",
|
|
15
|
+
"Programming Language :: Python :: 3.14",
|
|
16
|
+
"Programming Language :: Python :: Implementation :: CPython",
|
|
17
|
+
"Environment :: Web Environment",
|
|
18
|
+
"Framework :: Django",
|
|
19
|
+
"Framework :: Django :: 6.1",
|
|
20
|
+
]
|
|
21
|
+
dependencies = [
|
|
22
|
+
"django>=6.1",
|
|
23
|
+
"django-queues[redis]>=1.2.0,<2.0.0",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[project.urls]
|
|
27
|
+
Source = "https://github.com/deeprave/django-redis-tasks"
|
|
28
|
+
Issues = "https://github.com/deeprave/django-redis-tasks/issues"
|
|
29
|
+
Changelog = "https://github.com/deeprave/django-redis-tasks/releases"
|
|
30
|
+
|
|
31
|
+
[tool.pytest.ini_options]
|
|
32
|
+
minversion = "8.0"
|
|
33
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
34
|
+
pythonpath = ["."]
|
|
35
|
+
testpaths = ["tests"]
|
|
36
|
+
|
|
37
|
+
[dependency-groups]
|
|
38
|
+
dev = [
|
|
39
|
+
"pytest-mock>=3.14.0",
|
|
40
|
+
"pytest>=8.3.4",
|
|
41
|
+
"ruff>=0.15.14",
|
|
42
|
+
"testcontainers>=4.9.0",
|
|
43
|
+
"ty>=0.0.69",
|
|
44
|
+
"pytest-asyncio>=0.25.0",
|
|
45
|
+
]
|
|
46
|
+
[tool.uv]
|
|
47
|
+
default-groups = ["dev"]
|
|
48
|
+
|
|
49
|
+
[tool.setuptools.packages.find]
|
|
50
|
+
include = ["redis_tasks", "redis_tasks.*"]
|
|
51
|
+
|
|
52
|
+
[tool.ty.src]
|
|
53
|
+
include = ["redis_tasks"]
|
|
54
|
+
|
|
55
|
+
[build-system]
|
|
56
|
+
requires = ["setuptools"]
|
|
57
|
+
build-backend = "setuptools.build_meta"
|
|
File without changes
|