Skip to content

Commit e03034c

Browse files
committed
job.task_kwargs is pre-json, treat it as such
1 parent 4e2e05d commit e03034c

3 files changed

Lines changed: 15 additions & 12 deletions

File tree

procrastinate/contrib/django/admin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django.utils.html import format_html
1010
from django.utils.safestring import mark_safe
1111

12-
from procrastinate.utils import format_arg
12+
from procrastinate.utils import ellipsize_middle
1313

1414
from . import models
1515

@@ -123,7 +123,7 @@ def pretty_args(self, instance: models.ProcrastinateJob) -> str:
123123
format_html(
124124
"<tr><td>{key}</td><td>{value}</td></tr>",
125125
key=key,
126-
value=format_arg(value),
126+
value=ellipsize_middle(value),
127127
)
128128
for key, value in instance.args.items()
129129
]

procrastinate/jobs.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from typing_extensions import Literal
1111

1212
from procrastinate import types
13-
from procrastinate.utils import format_arg
13+
from procrastinate.utils import ellipsize_middle
1414

1515
if TYPE_CHECKING:
1616
from procrastinate import manager
@@ -137,7 +137,8 @@ def evolve(self, **kwargs: Any) -> Job:
137137
@cached_property
138138
def call_string(self):
139139
kwargs_string = ", ".join(
140-
f"{key}={format_arg(value)}" for key, value in self.task_kwargs.items()
140+
f"{key}={ellipsize_middle(repr(value))}"
141+
for key, value in self.task_kwargs.items()
141142
)
142143
return f"{self.task_name}[{self.id}]({kwargs_string})"
143144

procrastinate/utils.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import datetime
66
import importlib
77
import inspect
8-
import json
98
import logging
109
import pathlib
1110
import sys
@@ -28,7 +27,7 @@
2827
from asgiref import sync
2928

3029
from procrastinate import exceptions
31-
from procrastinate.types import JSONValue, TimeDeltaParams
30+
from procrastinate.types import TimeDeltaParams
3231

3332
T = TypeVar("T")
3433
U = TypeVar("U")
@@ -332,12 +331,15 @@ def queues_display(queues: Iterable[str] | None) -> str:
332331
return "all queues"
333332

334333

335-
def format_arg(value: JSONValue) -> str:
334+
def ellipsize_middle(
335+
value: str, max_length: int = 100, suffix_length: int = 20, ellipsis: str = "..."
336+
) -> str:
336337
"""
337-
Format a JSON argument value for display in logs and the admin interface.
338+
Limits the length of a string to `max_length` by placing `ellipsis` in the middle, preserving `suffix_length` at the end.
338339
"""
340+
prefix_length = max_length - len(ellipsis) - suffix_length
339341

340-
formatted = json.dumps(value)
341-
if len(formatted) > 100:
342-
formatted = formatted[:77] + "..." + formatted[-20:]
343-
return formatted
342+
if len(value) > max_length:
343+
return value[:prefix_length] + ellipsis + value[-suffix_length:]
344+
else:
345+
return value

0 commit comments

Comments
 (0)