Skip to content

Commit

Permalink
Adding a check that ensures the app required for the database backend…
Browse files Browse the repository at this point in the history
… is installed.

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 committed Jun 14, 2024
1 parent 1db35c1 commit b1f2ef0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
20 changes: 19 additions & 1 deletion django_tasks/backends/database/backend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import sys
from dataclasses import asdict, dataclass
from typing import TYPE_CHECKING, TypeVar
from typing import TYPE_CHECKING, Any, List, TypeVar

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

Expand Down Expand Up @@ -88,3 +91,18 @@ 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 "django_tasks.backends.database" not in settings.INSTALLED_APPS:
backend_name = self.__class__.__name__
package_name = sys.modules[__name__].__package__

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

return []
14 changes: 13 additions & 1 deletion tests/tests/test_database_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


@override_settings(
TASKS={"default": {"BACKEND": "django_tasks.backends.database.DatabaseBackend"}}
TASKS={"default": {"BACKEND": "django_tasks.backends.database.DatabaseBackend"}},
)
class DatabaseBackendTestCase(TestCase):
def test_using_correct_backend(self) -> None:
Expand Down Expand Up @@ -165,6 +165,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 b1f2ef0

Please sign in to comment.