mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-23 19:01:26 -07:00
CloudRetry/AWSRetry backoff decorator with unit tests (#17039)
* Added aws_retry decorator function with unit tests * Restructured the code to be used with a base class. This base class CloudRetry can be reused by any other cloud provider. This decorator should be used in situations, where you need to implement a backoff algorithm and want to retry based on the status code from the exception. * updated documentation * fixed tabs * added botocore and boto3 to requirements.txt * removed cloud.py from py24 tests, as it depends on boto3 * fix relative imports * updated test to be 2.6 compat * updated method name from retry to backoff * readded lxd * Updated default backoff from 2 seconds to 1.1s. This will be about a total of 48 seconds in 10 tries. This is configurable.
This commit is contained in:
parent
60706cc12e
commit
b510abce17
7 changed files with 226 additions and 1 deletions
|
@ -27,8 +27,11 @@
|
|||
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
import os
|
||||
import re
|
||||
from time import sleep
|
||||
|
||||
from ansible.module_utils.cloud import CloudRetry
|
||||
|
||||
try:
|
||||
import boto
|
||||
import boto.ec2 #boto does weird import stuff
|
||||
|
@ -55,6 +58,29 @@ class AnsibleAWSError(Exception):
|
|||
pass
|
||||
|
||||
|
||||
class AWSRetry(CloudRetry):
|
||||
base_class = botocore.exceptions.ClientError
|
||||
|
||||
@staticmethod
|
||||
def status_code_from_exception(error):
|
||||
return error.response['Error']['Code']
|
||||
|
||||
@staticmethod
|
||||
def found(response_code):
|
||||
# This list of failures is based on this API Reference
|
||||
# http://docs.aws.amazon.com/AWSEC2/latest/APIReference/errors-overview.html
|
||||
retry_on = [
|
||||
'RequestLimitExceeded', 'Unavailable', 'ServiceUnavailable',
|
||||
'InternalFailure', 'InternalError'
|
||||
]
|
||||
|
||||
not_found = re.compile(r'^\w+.NotFound')
|
||||
if response_code in retry_on or not_found.search(response_code):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def boto3_conn(module, conn_type=None, resource=None, region=None, endpoint=None, **params):
|
||||
try:
|
||||
return _boto3_conn(conn_type=conn_type, resource=resource, region=region, endpoint=endpoint, **params)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue