Skip to content

Commit

Permalink
Serialize arguments in task, rather than each backend
Browse files Browse the repository at this point in the history
This avoids duplication, and makes backend implementations simpler
  • Loading branch information
RealOrangeOne committed Nov 22, 2024
1 parent 26c0ec6 commit ae77a27
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
3 changes: 1 addition & 2 deletions django_tasks/backends/database/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from django_tasks.signals import task_enqueued
from django_tasks.task import Task
from django_tasks.task import TaskResult as BaseTaskResult
from django_tasks.utils import json_normalize

if TYPE_CHECKING:
from .models import DBTaskResult
Expand All @@ -38,7 +37,7 @@ def _task_to_db_task(
from .models import DBTaskResult

return DBTaskResult(
args_kwargs=json_normalize({"args": args, "kwargs": kwargs}),
args_kwargs={"args": args, "kwargs": kwargs},
priority=task.priority,
task_path=task.module_path,
queue_name=task.queue_name,
Expand Down
6 changes: 3 additions & 3 deletions django_tasks/backends/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django_tasks.exceptions import ResultDoesNotExist
from django_tasks.signals import task_enqueued
from django_tasks.task import ResultStatus, Task, TaskResult
from django_tasks.utils import get_random_id, json_normalize
from django_tasks.utils import get_random_id

from .base import BaseTaskBackend

Expand Down Expand Up @@ -44,8 +44,8 @@ def enqueue(
enqueued_at=None,
started_at=None,
finished_at=None,
args=json_normalize(args),
kwargs=json_normalize(kwargs),
args=args,
kwargs=kwargs,
backend=self.alias,
)

Expand Down
4 changes: 2 additions & 2 deletions django_tasks/backends/immediate.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def enqueue(
enqueued_at=None,
started_at=None,
finished_at=None,
args=json_normalize(args),
kwargs=json_normalize(kwargs),
args=args,
kwargs=kwargs,
backend=self.alias,
)

Expand Down
15 changes: 12 additions & 3 deletions django_tasks/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
from typing_extensions import ParamSpec, Self

from .exceptions import ResultDoesNotExist
from .utils import SerializedExceptionDict, exception_from_dict, get_module_path
from .utils import (
SerializedExceptionDict,
exception_from_dict,
get_module_path,
json_normalize,
)

if TYPE_CHECKING:
from .backends.base import BaseTaskBackend
Expand Down Expand Up @@ -118,13 +123,17 @@ def enqueue(self, *args: P.args, **kwargs: P.kwargs) -> "TaskResult[T]":
"""
Queue up the task to be executed
"""
return self.get_backend().enqueue(self, args, kwargs)
return self.get_backend().enqueue(
self, json_normalize(args), json_normalize(kwargs)
)

async def aenqueue(self, *args: P.args, **kwargs: P.kwargs) -> "TaskResult[T]":
"""
Queue up a task function (or coroutine) to be executed
"""
return await self.get_backend().aenqueue(self, args, kwargs)
return await self.get_backend().aenqueue(
self, json_normalize(args), json_normalize(kwargs)
)

def get_result(self, result_id: str) -> "TaskResult[T]":
"""
Expand Down
7 changes: 4 additions & 3 deletions tests/tasks.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import time
from typing import Any

from django_tasks import task


@task()
def noop_task(*args: tuple, **kwargs: dict) -> None:
def noop_task(*args: Any, **kwargs: Any) -> None:
return None


@task
def noop_task_from_bare_decorator(*args: tuple, **kwargs: dict) -> None:
def noop_task_from_bare_decorator(*args: Any, **kwargs: Any) -> None:
return None


@task()
async def noop_task_async(*args: tuple, **kwargs: dict) -> None:
async def noop_task_async(*args: Any, **kwargs: Any) -> None:
return None


Expand Down
12 changes: 12 additions & 0 deletions tests/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ async def test_enqueue_task_async(self) -> None:

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

def test_enqueue_with_invalid_argument(self) -> None:
with self.assertRaisesMessage(
TypeError, "Object of type datetime is not JSON serializable"
):
test_tasks.noop_task.enqueue(datetime.now())

async def test_aenqueue_with_invalid_argument(self) -> None:
with self.assertRaisesMessage(
TypeError, "Object of type datetime is not JSON serializable"
):
await test_tasks.noop_task.aenqueue(datetime.now())

def test_using_priority(self) -> None:
self.assertEqual(test_tasks.noop_task.priority, 0)
self.assertEqual(test_tasks.noop_task.using(priority=1).priority, 1)
Expand Down

0 comments on commit ae77a27

Please sign in to comment.