Skip to content

Commit 0474311

Browse files
authored
Allow to opt-out from logging results (#352)
* Add attribute Worker.log_results * Do not log result when Worker.log_results is False * Add description for the new parameter
1 parent 1495be6 commit 0474311

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

arq/worker.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ class Worker:
170170
:param job_deserializer: a function that deserializes bytes into Python objects, defaults to pickle.loads
171171
:param expires_extra_ms: the default length of time from when a job is expected to start
172172
after which the job expires, defaults to 1 day in ms.
173+
:param log_results: when set to true (default) results for successful jobs
174+
will be logged
173175
"""
174176

175177
def __init__(
@@ -202,6 +204,7 @@ def __init__(
202204
job_serializer: Optional[Serializer] = None,
203205
job_deserializer: Optional[Deserializer] = None,
204206
expires_extra_ms: int = expires_extra_ms,
207+
log_results: bool = True,
205208
):
206209
self.functions: Dict[str, Union[Function, CronJob]] = {f.name: f for f in map(func, functions)}
207210
if queue_name is None:
@@ -266,6 +269,7 @@ def __init__(
266269
self.job_serializer = job_serializer
267270
self.job_deserializer = job_deserializer
268271
self.expires_extra_ms = expires_extra_ms
272+
self.log_results = log_results
269273

270274
def run(self) -> None:
271275
"""
@@ -553,7 +557,7 @@ async def job_failed(exc: BaseException) -> None:
553557
exc_extra = exc_extra()
554558
raise
555559
else:
556-
result_str = '' if result is None else truncate(repr(result))
560+
result_str = '' if result is None or not self.log_results else truncate(repr(result))
557561
finally:
558562
del self.job_tasks[job_id]
559563

tests/test_worker.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ async def test_job_successful(arq_redis: ArqRedis, worker, caplog):
126126
assert 'X.XXs → testing:foobar()\n X.XXs ← testing:foobar ● 42' in log
127127

128128

129+
async def test_job_successful_no_result_logging(arq_redis: ArqRedis, worker, caplog):
130+
caplog.set_level(logging.INFO)
131+
await arq_redis.enqueue_job('foobar', _job_id='testing')
132+
worker: Worker = worker(functions=[foobar], log_results=False)
133+
await worker.main()
134+
135+
log = re.sub(r'\d+.\d\ds', 'X.XXs', '\n'.join(r.message for r in caplog.records))
136+
assert log.endswith('X.XXs → testing:foobar()\n X.XXs ← testing:foobar ● ')
137+
assert '42' not in log
138+
139+
129140
async def test_job_retry(arq_redis: ArqRedis, worker, caplog):
130141
async def retry(ctx):
131142
if ctx['job_try'] <= 2:

0 commit comments

Comments
 (0)