Skip to content

Commit ae77a27

Browse files
committed
Serialize arguments in task, rather than each backend
This avoids duplication, and makes backend implementations simpler
1 parent 26c0ec6 commit ae77a27

File tree

6 files changed

+34
-13
lines changed

6 files changed

+34
-13
lines changed

django_tasks/backends/database/backend.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from django_tasks.signals import task_enqueued
1414
from django_tasks.task import Task
1515
from django_tasks.task import TaskResult as BaseTaskResult
16-
from django_tasks.utils import json_normalize
1716

1817
if TYPE_CHECKING:
1918
from .models import DBTaskResult
@@ -38,7 +37,7 @@ def _task_to_db_task(
3837
from .models import DBTaskResult
3938

4039
return DBTaskResult(
41-
args_kwargs=json_normalize({"args": args, "kwargs": kwargs}),
40+
args_kwargs={"args": args, "kwargs": kwargs},
4241
priority=task.priority,
4342
task_path=task.module_path,
4443
queue_name=task.queue_name,

django_tasks/backends/dummy.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django_tasks.exceptions import ResultDoesNotExist
1010
from django_tasks.signals import task_enqueued
1111
from django_tasks.task import ResultStatus, Task, TaskResult
12-
from django_tasks.utils import get_random_id, json_normalize
12+
from django_tasks.utils import get_random_id
1313

1414
from .base import BaseTaskBackend
1515

@@ -44,8 +44,8 @@ def enqueue(
4444
enqueued_at=None,
4545
started_at=None,
4646
finished_at=None,
47-
args=json_normalize(args),
48-
kwargs=json_normalize(kwargs),
47+
args=args,
48+
kwargs=kwargs,
4949
backend=self.alias,
5050
)
5151

django_tasks/backends/immediate.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ def enqueue(
7878
enqueued_at=None,
7979
started_at=None,
8080
finished_at=None,
81-
args=json_normalize(args),
82-
kwargs=json_normalize(kwargs),
81+
args=args,
82+
kwargs=kwargs,
8383
backend=self.alias,
8484
)
8585

django_tasks/task.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121
from typing_extensions import ParamSpec, Self
2222

2323
from .exceptions import ResultDoesNotExist
24-
from .utils import SerializedExceptionDict, exception_from_dict, get_module_path
24+
from .utils import (
25+
SerializedExceptionDict,
26+
exception_from_dict,
27+
get_module_path,
28+
json_normalize,
29+
)
2530

2631
if TYPE_CHECKING:
2732
from .backends.base import BaseTaskBackend
@@ -118,13 +123,17 @@ def enqueue(self, *args: P.args, **kwargs: P.kwargs) -> "TaskResult[T]":
118123
"""
119124
Queue up the task to be executed
120125
"""
121-
return self.get_backend().enqueue(self, args, kwargs)
126+
return self.get_backend().enqueue(
127+
self, json_normalize(args), json_normalize(kwargs)
128+
)
122129

123130
async def aenqueue(self, *args: P.args, **kwargs: P.kwargs) -> "TaskResult[T]":
124131
"""
125132
Queue up a task function (or coroutine) to be executed
126133
"""
127-
return await self.get_backend().aenqueue(self, args, kwargs)
134+
return await self.get_backend().aenqueue(
135+
self, json_normalize(args), json_normalize(kwargs)
136+
)
128137

129138
def get_result(self, result_id: str) -> "TaskResult[T]":
130139
"""

tests/tasks.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import time
2+
from typing import Any
23

34
from django_tasks import task
45

56

67
@task()
7-
def noop_task(*args: tuple, **kwargs: dict) -> None:
8+
def noop_task(*args: Any, **kwargs: Any) -> None:
89
return None
910

1011

1112
@task
12-
def noop_task_from_bare_decorator(*args: tuple, **kwargs: dict) -> None:
13+
def noop_task_from_bare_decorator(*args: Any, **kwargs: Any) -> None:
1314
return None
1415

1516

1617
@task()
17-
async def noop_task_async(*args: tuple, **kwargs: dict) -> None:
18+
async def noop_task_async(*args: Any, **kwargs: Any) -> None:
1819
return None
1920

2021

tests/tests/test_tasks.py

+12
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ async def test_enqueue_task_async(self) -> None:
6868

6969
self.assertEqual(default_task_backend.results, [result]) # type:ignore[attr-defined]
7070

71+
def test_enqueue_with_invalid_argument(self) -> None:
72+
with self.assertRaisesMessage(
73+
TypeError, "Object of type datetime is not JSON serializable"
74+
):
75+
test_tasks.noop_task.enqueue(datetime.now())
76+
77+
async def test_aenqueue_with_invalid_argument(self) -> None:
78+
with self.assertRaisesMessage(
79+
TypeError, "Object of type datetime is not JSON serializable"
80+
):
81+
await test_tasks.noop_task.aenqueue(datetime.now())
82+
7183
def test_using_priority(self) -> None:
7284
self.assertEqual(test_tasks.noop_task.priority, 0)
7385
self.assertEqual(test_tasks.noop_task.using(priority=1).priority, 1)

0 commit comments

Comments
 (0)