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

Simplified retries logging #58

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sw-utils"
version = "0.3.21"
version = "0.3.22"
description = "StakeWise Python utils"
authors = ["StakeWise Labs <[email protected]>"]
license = "GPL-3.0-or-later"
Expand Down
7 changes: 1 addition & 6 deletions sw_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
ValidatorStatus,
get_consensus_client,
)
from .decorators import (
custom_before_log,
retry_aiohttp_errors,
retry_ipfs_exception,
safe,
)
from .decorators import retry_aiohttp_errors, retry_ipfs_exception, safe
from .event_scanner import EventProcessor, EventScanner
from .execution import get_execution_client
from .ipfs import (
Expand Down
17 changes: 7 additions & 10 deletions sw_utils/consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,16 @@ async def get_consensus_fork(self, state_id: str = 'head') -> ConsensusFork:
async def _async_make_get_request(self, endpoint_uri: str) -> dict[str, Any]:
if self.retry_timeout:

def custom_before_log(retry_logger, log_level):
def custom_log_it(retry_state: 'RetryCallState') -> None:
if retry_state.attempt_number <= 1:
return
msg = 'Retrying consensus uri %s(), attempt %s'
args = (endpoint_uri, retry_state.attempt_number)
retry_logger.log(log_level, msg, *args)

return custom_log_it
def custom_before_log(retry_state: 'RetryCallState') -> None:
if retry_state.attempt_number <= 1:
return
msg = 'Retrying consensus uri %s, attempt %s'
args = (endpoint_uri, retry_state.attempt_number)
logger.log(logging.INFO, msg, *args)

retry_decorator = retry_aiohttp_errors(
self.retry_timeout,
log_func=custom_before_log,
before=custom_before_log,
)
return await retry_decorator(self._async_make_get_request_inner)(endpoint_uri)

Expand Down
31 changes: 12 additions & 19 deletions sw_utils/decorators.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import asyncio
import logging
import typing
from functools import wraps
from typing import Callable
from typing import Callable, Optional

import aiohttp
from tenacity import (
RetryCallState,
retry,
retry_if_exception,
retry_if_exception_type,
Expand All @@ -18,10 +18,6 @@
default_logger = logging.getLogger(__name__)


if typing.TYPE_CHECKING:
from tenacity import RetryCallState


def safe(func: Callable) -> Callable:
if asyncio.iscoroutinefunction(func):

Expand All @@ -46,15 +42,12 @@ def wrapper(*args, **kwargs):
return wrapper


def custom_before_log(logger, log_level):
def custom_log_it(retry_state: 'RetryCallState') -> None:
if retry_state.attempt_number <= 1:
return
msg = 'Retrying %s, attempt %s'
args = (retry_state.fn.__name__, retry_state.attempt_number) # type: ignore
logger.log(log_level, msg, *args)

return custom_log_it
def default_log_before(retry_state: 'RetryCallState') -> None:
if retry_state.attempt_number <= 1:
return
msg = 'Retrying %s, attempt %s'
args = (retry_state.fn.__name__, retry_state.attempt_number) # type: ignore
default_logger.log(logging.INFO, msg, *args)


def can_be_retried_aiohttp_error(e: BaseException) -> bool:
Expand All @@ -67,19 +60,19 @@ def can_be_retried_aiohttp_error(e: BaseException) -> bool:
return False


def retry_aiohttp_errors(delay: int = 60, log_func=custom_before_log):
def retry_aiohttp_errors(delay: int = 60, before: Optional[Callable] = None):
return retry(
retry=retry_if_exception(can_be_retried_aiohttp_error),
wait=wait_exponential(multiplier=1, min=1, max=delay // 2),
stop=stop_after_delay(delay),
before=log_func(default_logger, logging.INFO),
before=before or default_log_before,
)


def retry_ipfs_exception(delay: int):
def retry_ipfs_exception(delay: int, before: Optional[Callable] = None):
return retry(
retry=retry_if_exception_type(IpfsException),
wait=wait_exponential(multiplier=1, min=1, max=delay // 2),
stop=stop_after_delay(delay),
before=custom_before_log(default_logger, logging.INFO),
before=before or default_log_before,
)
17 changes: 7 additions & 10 deletions sw_utils/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,16 @@ async def make_request(self, method: RPCEndpoint, params: Any) -> RPCResponse:

if self.retry_timeout:

def custom_before_log(retry_logger, log_level):
def custom_log_it(retry_state: 'RetryCallState') -> None:
if retry_state.attempt_number <= 1:
return
msg = 'Retrying execution method %s, attempt %s'
args = (method, retry_state.attempt_number)
retry_logger.log(log_level, msg, *args)

return custom_log_it
def custom_before_log(retry_state: 'RetryCallState') -> None:
if retry_state.attempt_number <= 1:
return
msg = 'Retrying execution method %s, attempt %s'
args = (method, retry_state.attempt_number)
logger.log(logging.INFO, msg, *args)

retry_decorator = retry_aiohttp_errors(
self.retry_timeout,
log_func=custom_before_log,
before=custom_before_log,
)
return await retry_decorator(self.make_request_inner)(method, params)

Expand Down
Loading