This commit is contained in:
Untone 2021-08-19 07:48:25 +03:00
parent 12c3f6a006
commit 46975998e2
2 changed files with 0 additions and 35 deletions

View File

@ -1,34 +0,0 @@
from sqlalchemy.exc import OperationalError, StatementError
from sqlalchemy.orm.query import Query as _Query
from time import sleep
class RetryingQuery(_Query):
__max_retry_count__ = 3
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def __iter__(self):
attempts = 0
while True:
attempts += 1
try:
return super().__iter__()
except OperationalError as ex:
if "server closed the connection unexpectedly" not in str(ex):
raise
if attempts <= self.__max_retry_count__:
sleep_for = 2 ** (attempts - 1)
logging.error(
"/!\ Database connection error: retrying Strategy => sleeping for {}s"
" and will retry (attempt #{} of {}) \n Detailed query impacted: {}".format(
sleep_for, attempts, self.__max_retry_count__, ex)
)
sleep(sleep_for)
continue
else:
raise
except StatementError as ex:
if "reconnect until invalid transaction is rolled back" not in str(ex):
raise
self.session.rollback()

View File

@ -6,7 +6,6 @@ from sqlalchemy.orm import Session
from sqlalchemy.sql.schema import Table
from settings import DB_URL
from orm._retry import RetryingQuery
# engine = create_engine(DB_URL, convert_unicode=True, echo=False)
engine = create_engine(DB_URL,