Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions awsretry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def found(response_code):
pass

@classmethod
def backoff(cls, tries=10, delay=3, backoff=1.1, added_exceptions=list()):
def backoff(cls, tries=10, delay=3, backoff=1.1, added_exceptions=list(), excluded_exceptions=list()):
""" Retry calling the Cloud decorated function using an exponential
backoff.
Kwargs:
Expand All @@ -60,6 +60,8 @@ def backoff(cls, tries=10, delay=3, backoff=1.1, added_exceptions=list()):
default=2
added_exceptions (list): Other exceptions to retry on.
default=[]
excluded_exceptions (list): Exceptions not to retry on.
default=[]

"""
def deco(f):
Expand All @@ -73,7 +75,7 @@ def retry_func(*args, **kwargs):
base_exception_class = cls.base_class(e)
if isinstance(e, base_exception_class):
response_code = cls.status_code_from_exception(e)
if cls.found(response_code, added_exceptions):
if cls.found(response_code, added_exceptions, excluded_exceptions):
logging.info("{0}: Retrying in {1} seconds...".format(str(e), max_delay))
time.sleep(max_delay)
max_tries -= 1
Expand Down Expand Up @@ -119,7 +121,11 @@ def status_code_from_exception(error):
return error.__class__.__name__

@staticmethod
def found(response_code, added_exceptions):
def found(response_code, added_exceptions, excluded_exceptions):
# If the response_code is in the excluded list return immediately
if response_code in excluded_exceptions:
return False

# This list of failures is based on this API Reference
# http://docs.aws.amazon.com/AWSEC2/latest/APIReference/errors-overview.html
retry_on = [
Expand Down
42 changes: 42 additions & 0 deletions tests/test_awsretry.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,48 @@ def raise_unexpected_error():

self.assertEqual(self.counter, 1)

def test_excluded_exception_does_not_retry(self):
self.counter = 0
err_msg = {'Error': {'Code': 'ResourceNotFoundException'}}

@AWSRetry.backoff(tries=4, delay=0.1, excluded_exceptions=['ResourceNotFoundException'])
def raise_excluded_error():
self.counter += 1
raise botocore.exceptions.ClientError(err_msg, 'excluded')

with self.assertRaises(botocore.exceptions.ClientError):
raise_excluded_error()

self.assertEqual(self.counter, 1)

def test_excluded_exception_retry_for_non_match(self):
self.counter = 0
err_msg = {'Error': {'Code': 'InstanceId.NotFound'}}

@AWSRetry.backoff(tries=4, delay=0.1, excluded_exceptions=['ResourceNotFoundException'])
def raise_excluded_error_no_match():
self.counter += 1
raise botocore.exceptions.ClientError(err_msg, 'excluded_not_match')

with self.assertRaises(botocore.exceptions.ClientError):
raise_excluded_error_no_match()

self.assertEqual(self.counter, 4)

def test_excluded_core_exception_does_not_retry(self):
self.counter = 0
err_msg = {'Error': {'Code': 'InternalFailure'}}

@AWSRetry.backoff(tries=4, delay=0.1, excluded_exceptions=['InternalFailure'])
def raise_excluded_core_error():
self.counter += 1
raise botocore.exceptions.ClientError(err_msg, 'excluded_core')

with self.assertRaises(botocore.exceptions.ClientError):
raise_excluded_core_error()

self.assertEqual(self.counter, 1)


if __name__ == '__main__':
unittest.main()