Skip to content

Commit

Permalink
Stringify arguments and results only when logging is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
isra17 committed Jun 7, 2024
1 parent 1315583 commit 78f51aa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 9 additions & 1 deletion arq/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datetime import datetime, timedelta, timezone
from functools import lru_cache
from time import time
from typing import TYPE_CHECKING, Any, AsyncGenerator, Dict, Optional, Sequence, overload
from typing import TYPE_CHECKING, Any, AsyncGenerator, Callable, Dict, Optional, Sequence, overload

from .constants import timezone_env_vars

Expand All @@ -19,6 +19,14 @@
from .typing import SecondsTimedelta


class LazyStr:
def __init__(self, fn: Callable[[], str]) -> None:
self.fn = fn

def __str__(self) -> str:
return self.fn()


def as_int(f: float) -> int:
return int(round(f))

Expand Down
5 changes: 3 additions & 2 deletions arq/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
retry_key_prefix,
)
from .utils import (
LazyStr,
args_to_string,
import_string,
ms_to_datetime,
Expand Down Expand Up @@ -580,7 +581,7 @@ async def job_failed(exc: BaseException) -> None:
start_ms = timestamp_ms()
success = False
try:
s = args_to_string(args, kwargs)
s = LazyStr(partial(args_to_string, args, kwargs))
extra = f' try={job_try}' if job_try > 1 else ''
if (start_ms - score) > 1200:
extra += f' delayed={(start_ms - score) / 1000:0.2f}s'
Expand All @@ -596,7 +597,7 @@ async def job_failed(exc: BaseException) -> None:
exc_extra = exc_extra()
raise
else:
result_str = '' if result is None or not self.log_results else truncate(repr(result))
result_str = LazyStr(lambda: '' if result is None or not self.log_results else truncate(repr(result)))
finally:
del self.job_tasks[job_id]

Expand Down

0 comments on commit 78f51aa

Please sign in to comment.