|
3 | 3 | import datetime |
4 | 4 | import functools |
5 | 5 | import logging |
| 6 | +import socket |
6 | 7 | from collections.abc import AsyncIterator, Awaitable, Callable, Coroutine |
7 | 8 | from typing import Any, Final, ParamSpec, TypeVar |
8 | 9 |
|
| 10 | +import arrow |
9 | 11 | from tenacity import TryAgain, before_sleep_log, retry, retry_if_exception_type |
10 | 12 | from tenacity.wait import wait_fixed |
11 | 13 |
|
12 | 14 | from .async_utils import cancel_wait_task, with_delay |
13 | 15 | from .logging_utils import log_context |
| 16 | +from .redis import RedisClientSDK, exclusive |
14 | 17 |
|
15 | 18 | _logger = logging.getLogger(__name__) |
16 | 19 |
|
@@ -70,9 +73,11 @@ def _decorator( |
70 | 73 | sleep=nap, |
71 | 74 | wait=wait_fixed(interval.total_seconds()), |
72 | 75 | reraise=True, |
73 | | - retry=retry_if_exception_type(TryAgain) |
74 | | - if raise_on_error |
75 | | - else retry_if_exception_type(), |
| 76 | + retry=( |
| 77 | + retry_if_exception_type(TryAgain) |
| 78 | + if raise_on_error |
| 79 | + else retry_if_exception_type() |
| 80 | + ), |
76 | 81 | before_sleep=before_sleep_log(_logger, logging.DEBUG), |
77 | 82 | ) |
78 | 83 | @functools.wraps(func) |
@@ -135,3 +140,41 @@ async def periodic_task( |
135 | 140 | # NOTE: this stopping is shielded to prevent the cancellation to propagate |
136 | 141 | # into the stopping procedure |
137 | 142 | await asyncio.shield(cancel_wait_task(asyncio_task, max_delay=stop_timeout)) |
| 143 | + |
| 144 | + |
| 145 | +def exclusive_periodic( |
| 146 | + client: RedisClientSDK, |
| 147 | + *, |
| 148 | + task_interval: datetime.timedelta, |
| 149 | + retry_after: datetime.timedelta, |
| 150 | +) -> Callable[ |
| 151 | + [Callable[P, Coroutine[Any, Any, None]]], Callable[P, Coroutine[Any, Any, None]] |
| 152 | +]: |
| 153 | + """decorates a function to become exclusive and periodic. |
| 154 | +
|
| 155 | + Arguments: |
| 156 | + client -- The Redis client |
| 157 | + task_interval -- the task periodicity |
| 158 | + retry_after -- in case the exclusive lock cannot be acquired or is lost, this is the retry interval |
| 159 | +
|
| 160 | + Returns: |
| 161 | + Nothing, a periodic method does not return anything as it runs forever. |
| 162 | + """ |
| 163 | + |
| 164 | + def _decorator( |
| 165 | + func: Callable[P, Coroutine[Any, Any, None]], |
| 166 | + ) -> Callable[P, Coroutine[Any, Any, None]]: |
| 167 | + @periodic(interval=retry_after) |
| 168 | + @exclusive( |
| 169 | + client, |
| 170 | + lock_key=f"lock:exclusive_periodic_task:{func.__name__}", |
| 171 | + lock_value=f"locked since {arrow.utcnow().format()} by {client.client_name} on {socket.gethostname()}", |
| 172 | + ) |
| 173 | + @periodic(interval=task_interval) |
| 174 | + @functools.wraps(func) |
| 175 | + async def _wrapper(*args: P.args, **kwargs: P.kwargs) -> None: |
| 176 | + return await func(*args, **kwargs) |
| 177 | + |
| 178 | + return _wrapper |
| 179 | + |
| 180 | + return _decorator |
0 commit comments