Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stringify arguments and results only when logging is enabled #463

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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