Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
Add @recoverable decorator to retry operations upon connection error
Browse files Browse the repository at this point in the history
Similar to the `@run` decorator, this decorator causes a wrapped
method to be retried in the case that the method throws an exception
which appears to be caused by a lost connection.

While the `@run` decorator is limited to wrapping methods with a
particular signature (including `query` and `bindings` params, the
`@recoverable` decorator is more generally applicable.
  • Loading branch information
onlywade committed May 20, 2019
1 parent bd90bf1 commit b2c7928
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions orator/connections/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
connection_logger = logging.getLogger("orator.connection")


def recoverable(wrapped):
@wraps(wrapped)
def _recoverable(self, *args, **kwargs):
self._reconnect_if_missing_connection()
try:
result = wrapped(self, *args, **kwargs)
except Exception as e:
result = self._recover_if_caused_by_lost_connection(e, wrapped, *args, **kwargs)
return result
return _recoverable

def run(wrapped):
"""
Special decorator encapsulating query method.
Expand Down Expand Up @@ -356,6 +367,14 @@ def _try_again_if_caused_by_lost_connection(

raise QueryException(query, bindings, e)

def _recover_if_caused_by_lost_connection(self, e, callback, *args, **kwargs):
if self._caused_by_lost_connection(e):
self.reconnect()

return callback(self, *args, **kwargs)

raise e

def _caused_by_lost_connection(self, e):
message = str(e).lower()

Expand Down

0 comments on commit b2c7928

Please sign in to comment.