From c410b9a12074831f1b0746d22bccf32e7adad353 Mon Sep 17 00:00:00 2001 From: norohind <60548839+norohind@users.noreply.github.com> Date: Tue, 19 Apr 2022 16:02:51 +0300 Subject: [PATCH] HookSystem._last_records: add retry logic --- HookSystem.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/HookSystem.py b/HookSystem.py index db0f84e..6db8c6e 100644 --- a/HookSystem.py +++ b/HookSystem.py @@ -5,6 +5,8 @@ from Hook import Hook import importlib.machinery import HookUtils import copy +from loguru import logger +import threading def check_int(func: callable) -> callable: @@ -17,10 +19,21 @@ def check_int(func: callable) -> callable: def _last_records(operation_id: int, limit: int) -> list[dict]: - return Hook.get_db().execute( - HookUtils.SQL_REQUESTS.GET_HISTORICAL_INFO, - {'limit': limit, 'operation_id': operation_id} - ).fetchall() + last_exception: Exception | None = None + + for retry in range(0, 3): + try: + return Hook.get_db().execute( + HookUtils.SQL_REQUESTS.GET_HISTORICAL_INFO, + {'limit': limit, 'operation_id': operation_id} + ).fetchall() + + except Exception as e: + logger.opt(exception=True).warning(f'Exception in {threading.current_thread().name}, retry: {retry}') + last_exception = e + continue + + raise last_exception class HookSystem: