failover: store result in FailoverException
This commit is contained in:
parent
8a1b48dd8c
commit
d2909c0e4d
3 changed files with 11 additions and 9 deletions
|
@ -8,8 +8,9 @@ logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class FailoverException(Exception):
|
class FailoverException(Exception):
|
||||||
""" Exception raised when an operation should be retried by the failover decorator. """
|
""" Exception raised when an operation should be retried by the failover decorator. """
|
||||||
def __init__(self, message):
|
def __init__(self, return_value, message):
|
||||||
super(FailoverException, self).__init__()
|
super(FailoverException, self).__init__()
|
||||||
|
self.return_value = return_value
|
||||||
self.message = message
|
self.message = message
|
||||||
|
|
||||||
def failover(func):
|
def failover(func):
|
||||||
|
@ -41,6 +42,7 @@ def failover(func):
|
||||||
return func(*arg_set[0], **arg_set[1])
|
return func(*arg_set[0], **arg_set[1])
|
||||||
except FailoverException as ex:
|
except FailoverException as ex:
|
||||||
logger.debug('failing over: %s', ex.message)
|
logger.debug('failing over: %s', ex.message)
|
||||||
|
return_value = ex.return_value
|
||||||
continue
|
continue
|
||||||
raise FailoverException('exhausted all possible failovers')
|
return return_value
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
|
@ -334,7 +334,7 @@ class SecurityScannerAPI(object):
|
||||||
if self._config is None:
|
if self._config is None:
|
||||||
raise Exception('Cannot call unconfigured security system')
|
raise Exception('Cannot call unconfigured security system')
|
||||||
|
|
||||||
timeout = self._config['SECURITY_SCANNER_API_TIMEOUT_SECONDS']
|
timeout = self._config.get('SECURITY_SCANNER_API_TIMEOUT_SECONDS', 1)
|
||||||
endpoint = self._config['SECURITY_SCANNER_ENDPOINT']
|
endpoint = self._config['SECURITY_SCANNER_ENDPOINT']
|
||||||
|
|
||||||
with CloseForLongOperation(self._config):
|
with CloseForLongOperation(self._config):
|
||||||
|
@ -346,12 +346,12 @@ class SecurityScannerAPI(object):
|
||||||
return self._request(method, endpoint, path, body, params, timeout)
|
return self._request(method, endpoint, path, body, params, timeout)
|
||||||
|
|
||||||
# The request is read-only and can failover.
|
# The request is read-only and can failover.
|
||||||
all_endpoints = [endpoint] + self._config['SECURITY_SCANNER_READONLY_FAILOVER_ENDPOINTS']
|
all_endpoints = [endpoint] + self._config.get('SECURITY_SCANNER_READONLY_FAILOVER_ENDPOINTS', [])
|
||||||
try:
|
try:
|
||||||
return _failover_read_request(*[((self._request, endpoint, path, body, params, timeout), {})
|
return _failover_read_request(*[((self._request, endpoint, path, body, params, timeout), {})
|
||||||
for endpoint in all_endpoints])
|
for endpoint in all_endpoints])
|
||||||
except FailoverException:
|
except FailoverException as ex:
|
||||||
raise APIRequestFailure()
|
return ex.return_value
|
||||||
|
|
||||||
|
|
||||||
def _join_api_url(endpoint, api_version, path):
|
def _join_api_url(endpoint, api_version, path):
|
||||||
|
@ -364,5 +364,5 @@ def _failover_read_request(request_fn, endpoint, path, body, params, timeout):
|
||||||
""" This function auto-retries read-only requests until they return a 2xx status code. """
|
""" This function auto-retries read-only requests until they return a 2xx status code. """
|
||||||
resp = request_fn('GET', endpoint, path, body, params, timeout)
|
resp = request_fn('GET', endpoint, path, body, params, timeout)
|
||||||
if resp.status_code / 100 != 2:
|
if resp.status_code / 100 != 2:
|
||||||
raise FailoverException('status code was not 2xx')
|
raise FailoverException(resp, 'status code was not 2xx')
|
||||||
return resp
|
return resp
|
||||||
|
|
|
@ -18,7 +18,7 @@ def my_failover_func(i, should_raise=None):
|
||||||
i.increment()
|
i.increment()
|
||||||
if should_raise is not None:
|
if should_raise is not None:
|
||||||
raise should_raise()
|
raise should_raise()
|
||||||
raise FailoverException('incrementing')
|
raise FailoverException(None, 'incrementing')
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('stop_on,exception', [
|
@pytest.mark.parametrize('stop_on,exception', [
|
||||||
|
|
Reference in a new issue