Skip to content

Commit

Permalink
Ensure the app required for the database backend is installed. (#53)
Browse files Browse the repository at this point in the history
Although the proper setup is documented in the README, it is easy to miss and (a little) hassle to debug without knowledge of the django_tasks code itself.
  • Loading branch information
dennisstritzke authored Jun 18, 2024
1 parent 082685f commit 44bca3e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
18 changes: 17 additions & 1 deletion django_tasks/backends/database/backend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from dataclasses import asdict, dataclass
from typing import TYPE_CHECKING, TypeVar
from typing import TYPE_CHECKING, Any, List, TypeVar

from django.apps import apps
from django.core.checks import ERROR, CheckMessage
from django.core.exceptions import ValidationError
from typing_extensions import ParamSpec

Expand Down Expand Up @@ -88,3 +90,17 @@ async def aget_result(self, result_id: str) -> TaskResult:
return (await DBTaskResult.objects.aget(id=result_id)).task_result
except (DBTaskResult.DoesNotExist, ValidationError) as e:
raise ResultDoesNotExist(result_id) from e

def check(self, **kwargs: Any) -> List[CheckMessage]:
if not apps.is_installed("django_tasks.backends.database"):
backend_name = self.__class__.__name__

return [
CheckMessage(
ERROR,
f"{backend_name} configured as django_tasks backend, but database app not installed",
"Insert 'django_tasks.backends.database' in INSTALLED_APPS",
)
]

return []
12 changes: 12 additions & 0 deletions tests/tests/test_database_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ def test_missing_task_path(self) -> None:
):
_ = db_task_result.task

def test_check(self) -> None:
errors = default_task_backend.check()

self.assertEqual(len(errors), 0)

@override_settings(INSTALLED_APPS=[])
def test_database_backend_app_missing(self) -> None:
errors = default_task_backend.check()

self.assertEqual(len(errors), 1)
self.assertIn("django_tasks.backends.database", errors[0].hint)


@override_settings(
TASKS={
Expand Down

0 comments on commit 44bca3e

Please sign in to comment.