-
-
Save civic/7fe7b84dca7de0a0f5b46e368e153227 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import json | |
import time | |
log = logging.getLogger(__name__) | |
log.setLevel(logging.DEBUG) | |
def lambda_handler(event, context): | |
sqs_batch_item_failures = [] | |
for record in event['Records']: | |
message_id = record['messageId'] | |
attributes = record['attributes'] | |
approximateReceiveCount = attributes['ApproximateReceiveCount'] | |
body = record['body'] | |
msg = json.loads(body) | |
try: | |
if msg['no'] == 5: | |
api_request() | |
log.info("Consumed a message: no=[%s], messageId=[%s], ApproximateReceiveCount=[%s]", | |
msg['no'], message_id, approximateReceiveCount) | |
except Exception as e: | |
sqs_batch_item_failures.append({"itemIdentifier": message_id}) #失敗メッセージのID保存 | |
log.info("Error->Skip: no=[%s], messageId=[%s], ApproximateReceiveCount=[%s]", | |
msg['no'], message_id, approximateReceiveCount) | |
log.info("Finish %d records. failures %s records.", | |
len(event['Records']), len(sqs_batch_item_failures)) | |
#失敗メッセージのレポート | |
return { | |
"batchItemFailures": sqs_batch_item_failures | |
} | |
def api_request(msg): | |
log.info("Long term process.") | |
time.sleep(2) | |
raise ValueError("API Request timed out.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment