Skip to content

Commit

Permalink
Remove backend-specific commit configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
RealOrangeOne committed Jul 26, 2024
1 parent 5add6c4 commit 0976edb
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 18 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ If the task takes arguments, these can be passed as-is to `enqueue`.

#### Transactions

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).
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.

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.
This can be configured using the `ENQUEUE_ON_COMMIT` setting. `True` and `False` force the behaviour.

```python
TASKS = {
Expand All @@ -103,7 +103,7 @@ TASKS = {
}
```

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

### Queue names

Expand Down
13 changes: 4 additions & 9 deletions django_tasks/backends/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABCMeta, abstractmethod
from inspect import iscoroutinefunction
from typing import Any, Iterable, Optional, TypeVar
from typing import Any, Iterable, TypeVar

from asgiref.sync import sync_to_async
from django.core.checks import messages
Expand All @@ -19,7 +19,7 @@

class BaseTaskBackend(metaclass=ABCMeta):
alias: str
enqueue_on_commit: Optional[bool]
enqueue_on_commit: bool

task_class = Task

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

self.alias = options["ALIAS"]
self.queues = set(options.get("QUEUES", [DEFAULT_QUEUE_NAME]))
self.enqueue_on_commit = options.get("ENQUEUE_ON_COMMIT", None)
self.enqueue_on_commit = bool(options.get("ENQUEUE_ON_COMMIT", True))

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

def check(self, **kwargs: Any) -> Iterable[messages.CheckMessage]:
if self.enqueue_on_commit not in {True, False, None}:
yield messages.CheckMessage(
messages.ERROR, "`ENQUEUE_ON_COMMIT` must be a bool or None"
)

if self.enqueue_on_commit and not connections.settings:
yield messages.CheckMessage(
messages.ERROR,
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/test_database_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ def test_doesnt_wait_until_transaction_commit(self) -> None:
}
)
def test_wait_until_transaction_by_default(self) -> None:
self.assertIsNone(default_task_backend.enqueue_on_commit)
self.assertIsNone(
self.assertTrue(default_task_backend.enqueue_on_commit)
self.assertTrue(
default_task_backend._get_enqueue_on_commit_for_task(test_tasks.noop_task)
)

Expand Down
4 changes: 2 additions & 2 deletions tests/tests/test_dummy_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ def test_doesnt_wait_until_transaction_commit(self) -> None:
}
)
def test_wait_until_transaction_by_default(self) -> None:
self.assertIsNone(default_task_backend.enqueue_on_commit)
self.assertIsNone(
self.assertTrue(default_task_backend.enqueue_on_commit)
self.assertTrue(
default_task_backend._get_enqueue_on_commit_for_task(test_tasks.noop_task)
)

Expand Down
4 changes: 2 additions & 2 deletions tests/tests/test_immediate_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ def test_doesnt_wait_until_transaction_commit(self) -> None:
}
)
def test_wait_until_transaction_by_default(self) -> None:
self.assertIsNone(default_task_backend.enqueue_on_commit)
self.assertIsNone(
self.assertTrue(default_task_backend.enqueue_on_commit)
self.assertTrue(
default_task_backend._get_enqueue_on_commit_for_task(test_tasks.noop_task)
)

Expand Down

0 comments on commit 0976edb

Please sign in to comment.