Skip to content

Commit

Permalink
Set Spotinst API timeouts (#545)
Browse files Browse the repository at this point in the history
- Add highly opinionated timeouts to Spotinst API requests
  • Loading branch information
lmineiro authored and jmcs committed Oct 24, 2018
1 parent b55e95b commit d57deeb
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions senza/spotinst/components/elastigroup_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@

DEPLOY_STRATEGY_RESTART = 'RESTART_SERVER'
DEPLOY_STRATEGY_REPLACE = 'REPLACE_SERVER'
DEFAULT_CONNECT_TIMEOUT = 9
DEFAULT_READ_TIMEOUT = 30


class SpotInstAccountData:
'''
Data required to access SpotInst API
'''

def __init__(self, account_id, access_token):
self.account_id = account_id
self.access_token = access_token
Expand Down Expand Up @@ -58,7 +61,7 @@ def update_elastigroup(body, elastigroup_id, spotinst_account_data):

response = requests.put(
'{}/aws/ec2/group/{}?accountId={}'.format(SPOTINST_API_URL, elastigroup_id, spotinst_account_data.account_id),
headers=headers, timeout=10, data=json.dumps(body))
headers=headers, timeout=(DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT), data=json.dumps(body))
response.raise_for_status()
data = response.json()
groups = data.get("response", {}).get("items", [])
Expand Down Expand Up @@ -101,7 +104,7 @@ def get_elastigroup(elastigroup_id, spotinst_account_data):

response = requests.get(
'{}/aws/ec2/group/{}?accountId={}'.format(SPOTINST_API_URL, elastigroup_id, spotinst_account_data.account_id),
headers=headers, timeout=5)
headers=headers, timeout=(DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT))
response.raise_for_status()
data = response.json()
groups = data.get("response", {}).get("items", [])
Expand Down Expand Up @@ -152,7 +155,7 @@ def deploy(batch_size=20, grace_period=300, strategy=DEPLOY_STRATEGY_REPLACE,
response = requests.put(
'{}/aws/ec2/group/{}/roll?accountId={}'.format(SPOTINST_API_URL, elastigroup_id,
spotinst_account_data.account_id),
headers=headers, timeout=10, data=json.dumps(body))
headers=headers, timeout=(DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT), data=json.dumps(body))
response.raise_for_status()
data = response.json()
deploys = data.get("response", {}).get("items", [])
Expand All @@ -174,7 +177,7 @@ def deploy_status(deploy_id, elastigroup_id, spotinst_account_data):
response = requests.get(
'{}/aws/ec2/group/{}/roll/{}?accountId={}'.format(SPOTINST_API_URL, elastigroup_id, deploy_id,
spotinst_account_data.account_id),
headers=headers, timeout=5)
headers=headers, timeout=(DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT))
response.raise_for_status()
data = response.json()
deploys = data.get("response", {}).get("items", [])
Expand Down

0 comments on commit d57deeb

Please sign in to comment.