Skip to content

Instantly share code, notes, and snippets.

@civic
Last active July 24, 2023 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save civic/7fe7b84dca7de0a0f5b46e368e153227 to your computer and use it in GitHub Desktop.
Save civic/7fe7b84dca7de0a0f5b46e368e153227 to your computer and use it in GitHub Desktop.
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