Skip to content

Commit 228a942

Browse files
committed
Define a default queue name
1 parent 07a3933 commit 228a942

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

django_tasks/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
from .backends.base import BaseTaskBackend
88
from .exceptions import InvalidTaskBackendError
9-
from .task import ResultStatus, Task, task
9+
from .task import DEFAULT_QUEUE_NAME, ResultStatus, Task, task
1010

1111
__all__ = [
1212
"tasks",
1313
"DEFAULT_TASK_BACKEND_ALIAS",
14+
"DEFAULT_QUEUE_NAME",
1415
"task",
1516
"ResultStatus",
1617
"Task",

django_tasks/mail.py

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class SMTPEmailBackend(BaseEmailBackend):
2323
"""
2424
An email backend to send emails as background tasks
2525
"""
26+
2627
@staticmethod
2728
def serialize_message(message: EmailMessage) -> dict:
2829
data = {

django_tasks/task.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
if TYPE_CHECKING:
1313
from .backends.base import BaseTaskBackend
1414

15+
DEFAULT_QUEUE_NAME = "default"
16+
1517

1618
class ResultStatus(TextChoices):
1719
NEW = "NEW"
@@ -32,12 +34,12 @@ class Task(Generic[P, T]):
3234
func: Callable[P, T]
3335
"""The task function"""
3436

35-
queue_name: Optional[str]
36-
"""The name of the queue the task will run on """
37-
3837
backend: str
3938
"""The name of the backend the task will run on"""
4039

40+
queue_name: str = DEFAULT_QUEUE_NAME
41+
"""The name of the queue the task will run on """
42+
4143
run_after: Optional[datetime] = None
4244
"""The earliest this task will run"""
4345

@@ -126,7 +128,7 @@ def get_backend(self) -> "BaseTaskBackend":
126128

127129
def task(
128130
priority: Optional[int] = None,
129-
queue_name: Optional[str] = None,
131+
queue_name: str = DEFAULT_QUEUE_NAME,
130132
backend: str = "default",
131133
) -> Callable[[Callable[P, T]], Task[P, T]]:
132134
"""

tests/tests/test_tasks.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
from django.test import SimpleTestCase, override_settings
55
from django.utils import timezone
66

7-
from django_tasks import ResultStatus, default_task_backend, task, tasks
7+
from django_tasks import (
8+
DEFAULT_QUEUE_NAME,
9+
ResultStatus,
10+
default_task_backend,
11+
task,
12+
tasks,
13+
)
814
from django_tasks.backends.dummy import DummyBackend
915
from django_tasks.backends.immediate import ImmediateBackend
1016
from django_tasks.exceptions import (
@@ -56,11 +62,11 @@ def test_using_priority(self) -> None:
5662
self.assertIsNone(test_tasks.noop_task.priority)
5763

5864
def test_using_queue_name(self) -> None:
59-
self.assertIsNone(test_tasks.noop_task.queue_name)
65+
self.assertEqual(test_tasks.noop_task.queue_name, DEFAULT_QUEUE_NAME)
6066
self.assertEqual(
6167
test_tasks.noop_task.using(queue_name="queue_1").queue_name, "queue_1"
6268
)
63-
self.assertIsNone(test_tasks.noop_task.queue_name)
69+
self.assertEqual(test_tasks.noop_task.queue_name, DEFAULT_QUEUE_NAME)
6470

6571
def test_using_run_after(self) -> None:
6672
now = timezone.now()

0 commit comments

Comments
 (0)