Skip to content

Commit 263a54d

Browse files
committed
Validate queue names
1 parent 698f231 commit 263a54d

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ The returned `TaskResult` can be interrogated to query the current state of the
8181

8282
If the task takes arguments, these can be passed as-is to `enqueue`.
8383

84+
### Queue names
85+
86+
By default, tasks are enqueued onto the "default" queue. When using multiple queues, it can be useful to constrain the allowed names, so tasks aren't missed.
87+
88+
```python
89+
TASKS = {
90+
"default": {
91+
"BACKEND": "django_tasks.backends.immediate.ImmediateBackend",
92+
"QUEUES": ["default", "special"]
93+
}
94+
}
95+
```
96+
97+
Enqueueing tasks to an unknown queue name raises `InvalidTaskError`.
98+
99+
To disable queue name validation, set `QUEUES` to `[]`.
100+
84101
### The database backend worker
85102

86103
First, you'll need to add `django_tasks.backends.database` to `INSTALLED_APPS`, and run `manage.py migrate`.

django_tasks/backends/base.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ class BaseTaskBackend(metaclass=ABCMeta):
2828
"""Can results be retrieved after the fact (from **any** thread / process)"""
2929

3030
def __init__(self, options: dict) -> None:
31+
from django_tasks import DEFAULT_QUEUE_NAME
32+
3133
self.alias = options["ALIAS"]
34+
self.queues = set(options.get("QUEUES", [DEFAULT_QUEUE_NAME]))
3235

3336
def validate_task(self, task: Task) -> None:
3437
"""
@@ -51,6 +54,11 @@ def validate_task(self, task: Task) -> None:
5154
if task.run_after is not None and not timezone.is_aware(task.run_after):
5255
raise InvalidTaskError("run_after must be an aware datetime")
5356

57+
if self.queues and task.queue_name not in self.queues:
58+
raise InvalidTaskError(
59+
f"Queue '{task.queue_name}' is not valid for backend"
60+
)
61+
5462
@abstractmethod
5563
def enqueue(
5664
self, task: Task[P, T], args: P.args, kwargs: P.kwargs

tests/tests/test_database_backend.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222

2323

2424
@override_settings(
25-
TASKS={"default": {"BACKEND": "django_tasks.backends.database.DatabaseBackend"}}
25+
TASKS={
26+
"default": {
27+
"BACKEND": "django_tasks.backends.database.DatabaseBackend",
28+
"QUEUES": ["default", "queue-1"],
29+
}
30+
}
2631
)
2732
class DatabaseBackendTestCase(TestCase):
2833
def test_using_correct_backend(self) -> None:
@@ -190,7 +195,10 @@ def test_database_backend_app_missing(self) -> None:
190195

191196
@override_settings(
192197
TASKS={
193-
"default": {"BACKEND": "django_tasks.backends.database.DatabaseBackend"},
198+
"default": {
199+
"BACKEND": "django_tasks.backends.database.DatabaseBackend",
200+
"QUEUES": ["default", "queue-1"],
201+
},
194202
"dummy": {"BACKEND": "django_tasks.backends.dummy.DummyBackend"},
195203
}
196204
)

0 commit comments

Comments
 (0)