retry_exceptions lambda
Opened this issue · 0 comments
p-frolov commented
Hello,
I need additional checks before retry code, for instance:
def is_sql_exception_for_retry(exc: t_sql_errors):
# sqlalchemy.exc.OperationalError
# (psycopg2.OperationalError) server closed the connection unexpectedly
# (psycopg2.OperationalError) could not connect to server: Connection refused
message = exc._message()
if "server closed the connection unexpectedly" in message:
return True
if "could not connect to server: Connection refused" in message:
return True
return False
In my case I can do only:
@redo.retriable(retry_exceptions=(sqlalchemy.exc.OperationalError, psycopg2.OperationalError), sleeptime=10, jitter=3)
def function():
pass
I will be great to add to functional callback for retry.
Also I can define specific retry exception, check my function is_sql_exception_for_retry in retriable function and raise created exception, like this:
class MyRetryException(Exception):
pass
@redo.retriable(retry_exceptions=(MyRetryException,), sleeptime=10, jitter=3)
def function():
try:
...
except (sqlalchemy.exc.OperationalError, psycopg2.OperationalError) as exc:
if is_sql_exception_for_retry(exc):
raise MyRetryException()
raise exc
but in this case I will lose convenience of decorator, and will have additional code in each retriable function.