util.failover: re-raise exceptions on failure
This commit is contained in:
parent
21b09a7451
commit
cba7816caf
2 changed files with 19 additions and 13 deletions
|
@ -7,11 +7,12 @@ logger = logging.getLogger(__name__)
|
|||
|
||||
|
||||
class FailoverException(Exception):
|
||||
""" Exception raised when an operation should be retried by the failover decorator. """
|
||||
def __init__(self, return_value, message):
|
||||
""" Exception raised when an operation should be retried by the failover decorator.
|
||||
Wraps the exception of the initial failure.
|
||||
"""
|
||||
def __init__(self, exception):
|
||||
super(FailoverException, self).__init__()
|
||||
self.return_value = return_value
|
||||
self.message = message
|
||||
self.exception = exception
|
||||
|
||||
def failover(func):
|
||||
""" Wraps a function such that it can be retried on specified failures.
|
||||
|
@ -21,9 +22,10 @@ def failover(func):
|
|||
@failover
|
||||
def get_google(scheme, use_www=False):
|
||||
www = 'www.' if use_www else ''
|
||||
r = requests.get(scheme + '://' + www + 'google.com')
|
||||
if r.status_code != 200:
|
||||
raise FailoverException('non 200 response from Google' )
|
||||
try:
|
||||
r = requests.get(scheme + '://' + www + 'google.com')
|
||||
except requests.RequestException as ex:
|
||||
raise FailoverException(ex)
|
||||
return r
|
||||
|
||||
def GooglePingTest():
|
||||
|
@ -41,8 +43,8 @@ def failover(func):
|
|||
try:
|
||||
return func(*arg_set[0], **arg_set[1])
|
||||
except FailoverException as ex:
|
||||
logger.debug('failing over: %s', ex.message)
|
||||
return_value = ex.return_value
|
||||
logger.debug('failing over')
|
||||
exception = ex.exception
|
||||
continue
|
||||
return return_value
|
||||
raise exception
|
||||
return wrapper
|
||||
|
|
Reference in a new issue