Skip to content

Commit 15ca6ed

Browse files
committed
fixes invisible issues
1 parent 12ff7cc commit 15ca6ed

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

packages/service-library/src/servicelib/background_task.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from tenacity.wait import wait_fixed
1111

1212
from .async_utils import cancel_wait_task, delayed_start
13-
from .logging_utils import log_context
13+
from .logging_utils import log_catch, log_context
1414

1515
_logger = logging.getLogger(__name__)
1616

@@ -84,7 +84,8 @@ class _InternalTryAgain(TryAgain):
8484
)
8585
@functools.wraps(func)
8686
async def _wrapper(*args: P.args, **kwargs: P.kwargs) -> None:
87-
await func(*args, **kwargs)
87+
with log_catch(_logger, reraise=True):
88+
await func(*args, **kwargs)
8889
raise _InternalTryAgain
8990

9091
return _wrapper

packages/service-library/tests/test_background_task.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import asyncio
88
import datetime
9+
import logging
910
from collections.abc import AsyncIterator, Awaitable, Callable
1011
from typing import Final
1112
from unittest import mock
@@ -204,3 +205,19 @@ async def _func() -> None:
204205
await task
205206

206207
assert mock_func.call_count > 1
208+
209+
210+
async def test_periodic_task_logs_error(
211+
mock_background_task: mock.AsyncMock,
212+
task_interval: datetime.timedelta,
213+
caplog: pytest.LogCaptureFixture,
214+
):
215+
mock_background_task.side_effect = RuntimeError("Test error")
216+
217+
with caplog.at_level(logging.ERROR):
218+
async with periodic_task(
219+
mock_background_task, interval=task_interval, task_name="test_task"
220+
):
221+
await asyncio.sleep(2 * task_interval.total_seconds())
222+
223+
assert "Test error" in caplog.text

0 commit comments

Comments
 (0)