Skip to content

Commit b3c6b34

Browse files
committed
@pcrespov reviews
1 parent b30e1d6 commit b3c6b34

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ def exclusive_periodic(
3333
"""
3434

3535
def _decorator(
36-
func: Callable[P, Coroutine[Any, Any, None]],
36+
coro: Callable[P, Coroutine[Any, Any, None]],
3737
) -> Callable[P, Coroutine[Any, Any, None]]:
3838
@periodic(interval=retry_after)
3939
@exclusive(
4040
redis_client,
41-
lock_key=f"lock:exclusive_periodic_task:{func.__name__}",
41+
lock_key=f"lock:exclusive_periodic_task:{coro.__module__}.{coro.__name__}",
4242
)
4343
@periodic(interval=task_interval)
44-
@functools.wraps(func)
44+
@functools.wraps(coro)
4545
async def _wrapper(*args: P.args, **kwargs: P.kwargs) -> None:
46-
return await func(*args, **kwargs)
46+
return await coro(*args, **kwargs)
4747

4848
return _wrapper
4949

packages/service-library/src/servicelib/redis/_decorators.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
P = ParamSpec("P")
2323
R = TypeVar("R")
2424

25-
_EXCLUSIVE_TASK_NAME: Final[str] = "exclusive/{func_name}"
25+
_EXCLUSIVE_TASK_NAME: Final[str] = "exclusive/{module_name}.{func_name}"
2626
_EXCLUSIVE_AUTO_EXTEND_TASK_NAME: Final[
2727
str
2828
] = "exclusive/autoextend_lock_{redis_lock_key}"
@@ -65,9 +65,9 @@ def exclusive(
6565
raise ValueError(msg)
6666

6767
def _decorator(
68-
func: Callable[P, Coroutine[Any, Any, R]],
68+
coro: Callable[P, Coroutine[Any, Any, R]],
6969
) -> Callable[P, Coroutine[Any, Any, R]]:
70-
@functools.wraps(func)
70+
@functools.wraps(coro)
7171
async def _wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
7272
redis_lock_key = (
7373
lock_key(*args, **kwargs) if callable(lock_key) else lock_key
@@ -109,10 +109,12 @@ async def _wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
109109
await started_event.wait()
110110

111111
# then the task that runs the user code
112-
assert asyncio.iscoroutinefunction(func) # nosec
112+
assert asyncio.iscoroutinefunction(coro) # nosec
113113
work_task = tg.create_task(
114-
func(*args, **kwargs),
115-
name=_EXCLUSIVE_TASK_NAME.format(func_name=func.__name__),
114+
coro(*args, **kwargs),
115+
name=_EXCLUSIVE_TASK_NAME.format(
116+
module_name=coro.__module__, func_name=coro.__name__
117+
),
116118
)
117119

118120
res = await work_task
@@ -130,12 +132,6 @@ async def _wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
130132

131133
assert lock_lost_errors is not None # nosec
132134
assert len(lock_lost_errors.exceptions) == 1 # nosec
133-
_logger.error( # noqa: TRY400
134-
"lock %s could not be auto-extended! "
135-
"TIP: check connection to Redis DBs or look for Synchronous "
136-
"code that might block the auto-extender task. Somehow the distributed lock disappeared!",
137-
lock.name,
138-
)
139135
raise lock_lost_errors.exceptions[0] from eg
140136
finally:
141137
with contextlib.suppress(redis.exceptions.LockNotOwnedError):

packages/service-library/src/servicelib/redis/_errors.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ class CouldNotConnectToRedisError(BaseRedisError):
1414

1515

1616
class LockLostError(BaseRedisError):
17-
msg_template: str = "Lock {lock.name} has been lost"
17+
msg_template: str = (
18+
"Lock {lock.name} has been lost (e.g. it could not be auto-extended!)"
19+
"TIP: check connection to Redis DBs or look for Synchronous "
20+
"code that might block the auto-extender task. Somehow the distributed lock disappeared!"
21+
)

packages/service-library/tests/redis/test_decorators.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ def _assert_exclusive_tasks_are_cancelled(lock_name: str, func: Callable) -> Non
3131
assert _EXCLUSIVE_AUTO_EXTEND_TASK_NAME.format(redis_lock_key=lock_name) not in [
3232
t.get_name() for t in asyncio.tasks.all_tasks()
3333
], "the auto extend lock task was not properly stopped!"
34-
assert _EXCLUSIVE_TASK_NAME.format(func_name=func.__name__) not in [
34+
assert _EXCLUSIVE_TASK_NAME.format(
35+
module_name=func.__module__, func_name=func.__name__
36+
) not in [
3537
t.get_name() for t in asyncio.tasks.all_tasks()
3638
], "the exclusive task was not properly stopped!"
3739

0 commit comments

Comments
 (0)