Skip to content

retry_exceptions lambda #64

Open
Open
@p-frolov

Description

@p-frolov

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions