Skip to content

Commit 3190766

Browse files
committed
Remove backend-specific commit configuration
1 parent 5add6c4 commit 3190766

File tree

5 files changed

+15
-19
lines changed

5 files changed

+15
-19
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ The task decorator accepts a few arguments to customize the task:
6767
- `priority`: The priority of the task (between -100 and 100. Larger numbers are higher priority. 0 by default)
6868
- `queue_name`: Whether to run the task on a specific queue
6969
- `backend`: Name of the backend for this task to use (as defined in `TASKS`)
70+
- `enqueue_on_commit`: Whether the task is enqueued when the current transaction commits successfully, or enqueued immediately. By default, this is handled by the backend (see below). `enqueue_on_commit` may not be modified with `.using`.
7071

71-
These attributes can also be modified at run-time with `.using`:
72+
These attributes (besides `enqueue_on_commit`) can also be modified at run-time with `.using`:
7273

7374
```python
7475
modified_task = calculate_meaning_of_life.using(priority=10)
@@ -90,9 +91,9 @@ If the task takes arguments, these can be passed as-is to `enqueue`.
9091

9192
#### Transactions
9293

93-
By default, it's up to the backend to determine whether the task should be enqueued immediately (after calling `.enqueue`) or wait until the end of the current database transaction (if there is one).
94+
By default, tasks are enqueued after the current transaction (if there is one) commits successfully (using Django's `transaction.on_commit` method), rather than enqueueing immediately.
9495

95-
This can be configured using the `ENQUEUE_ON_COMMIT` setting. `True` and `False` force the behaviour, and `None` is used to let the backend decide.
96+
This can be configured using the `ENQUEUE_ON_COMMIT` setting. `True` and `False` force the behaviour.
9697

9798
```python
9899
TASKS = {
@@ -103,7 +104,7 @@ TASKS = {
103104
}
104105
```
105106

106-
All built-in backends default to waiting for the end of the transaction (`"ENQUEUE_ON_COMMIT": True`).
107+
This can also be configured per-task by passing `enqueue_on_commit` to the `task` decorator.
107108

108109
### Queue names
109110

django_tasks/backends/base.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from abc import ABCMeta, abstractmethod
22
from inspect import iscoroutinefunction
3-
from typing import Any, Iterable, Optional, TypeVar
3+
from typing import Any, Iterable, TypeVar
44

55
from asgiref.sync import sync_to_async
66
from django.core.checks import messages
@@ -19,7 +19,7 @@
1919

2020
class BaseTaskBackend(metaclass=ABCMeta):
2121
alias: str
22-
enqueue_on_commit: Optional[bool]
22+
enqueue_on_commit: bool
2323

2424
task_class = Task
2525

@@ -37,9 +37,9 @@ def __init__(self, options: dict) -> None:
3737

3838
self.alias = options["ALIAS"]
3939
self.queues = set(options.get("QUEUES", [DEFAULT_QUEUE_NAME]))
40-
self.enqueue_on_commit = options.get("ENQUEUE_ON_COMMIT", None)
40+
self.enqueue_on_commit = bool(options.get("ENQUEUE_ON_COMMIT", True))
4141

42-
def _get_enqueue_on_commit_for_task(self, task: Task) -> Optional[bool]:
42+
def _get_enqueue_on_commit_for_task(self, task: Task) -> bool:
4343
"""
4444
Determine the correct `enqueue_on_commit` setting to use for a given task.
4545
@@ -128,11 +128,6 @@ async def aget_result(self, result_id: str) -> TaskResult:
128128
)
129129

130130
def check(self, **kwargs: Any) -> Iterable[messages.CheckMessage]:
131-
if self.enqueue_on_commit not in {True, False, None}:
132-
yield messages.CheckMessage(
133-
messages.ERROR, "`ENQUEUE_ON_COMMIT` must be a bool or None"
134-
)
135-
136131
if self.enqueue_on_commit and not connections.settings:
137132
yield messages.CheckMessage(
138133
messages.ERROR,

tests/tests/test_database_backend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ def test_doesnt_wait_until_transaction_commit(self) -> None:
302302
}
303303
)
304304
def test_wait_until_transaction_by_default(self) -> None:
305-
self.assertIsNone(default_task_backend.enqueue_on_commit)
306-
self.assertIsNone(
305+
self.assertTrue(default_task_backend.enqueue_on_commit)
306+
self.assertTrue(
307307
default_task_backend._get_enqueue_on_commit_for_task(test_tasks.noop_task)
308308
)
309309

tests/tests/test_dummy_backend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ def test_doesnt_wait_until_transaction_commit(self) -> None:
187187
}
188188
)
189189
def test_wait_until_transaction_by_default(self) -> None:
190-
self.assertIsNone(default_task_backend.enqueue_on_commit)
191-
self.assertIsNone(
190+
self.assertTrue(default_task_backend.enqueue_on_commit)
191+
self.assertTrue(
192192
default_task_backend._get_enqueue_on_commit_for_task(test_tasks.noop_task)
193193
)
194194

tests/tests/test_immediate_backend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ def test_doesnt_wait_until_transaction_commit(self) -> None:
262262
}
263263
)
264264
def test_wait_until_transaction_by_default(self) -> None:
265-
self.assertIsNone(default_task_backend.enqueue_on_commit)
266-
self.assertIsNone(
265+
self.assertTrue(default_task_backend.enqueue_on_commit)
266+
self.assertTrue(
267267
default_task_backend._get_enqueue_on_commit_for_task(test_tasks.noop_task)
268268
)
269269

0 commit comments

Comments
 (0)