Skip to content

Commit

Permalink
hotfix LoadDataLambda racecondition (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
BastLeblanc authored Aug 28, 2024
1 parent db49913 commit 2f9234f
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions aws/cloudformation-templates/deployment-support.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Resources:
Role: !GetAtt LoadDataLambdaRole.Arn
Runtime: python3.12
MemorySize: 128
Timeout: 120
Timeout: 600
VpcConfig:
SecurityGroupIds:
- !Ref LambdaVpcSecurityGroup
Expand All @@ -184,25 +184,35 @@ Resources:
from urllib.error import URLError
import logging
import cfnresponse
import time
import os
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def handler(event, context):
response_data = {}
response_status = cfnresponse.SUCCESS
max_retries = 15 # Maximum number of retries - should be less than the timeout
retries = 0
if event['RequestType'] in ['Create', 'Update']:
url = os.environ['ProductsServiceUrl']
request = Request(f"{url}/init", method='POST')
try:
with urlopen(request) as response:
logger.info(f"Product Service init method success: {response.read()}")
except URLError as e:
logger.error(f"Error calling product service init: {e.code} : {e.reason}")
response_status = cfnresponse.FAILED
response_data['Message'] = f"Resource {event['RequestType']} failed: {e}"
while retries < max_retries:
try:
with urlopen(request) as response:
logger.info(f"Product Service init method success: {response.read()}")
# exit while, success.
break
except URLError as e:
retries += 1
print(f"Request failed. Retrying in 30 seconds... (Attempt {retries}/{max_retries})")
print(f"Error: {e}")
time.sleep(30)
#
if retries >= max_retries:
logger.error(f"Error calling product service init: {e.code} : {e.reason}")
response_status = cfnresponse.FAILED
response_data['Message'] = f"Resource {event['RequestType']} failed: {e}"
cfnresponse.send(event, context, response_status, response_data)
Expand Down

0 comments on commit 2f9234f

Please sign in to comment.