-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeadletter_batch.py
73 lines (59 loc) · 1.88 KB
/
deadletter_batch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import json
import time
import logging
import log_helper
from botocore.exceptions import ClientError
from app.athena import Athena
from app.sqs import Sqs
from aws_xray_sdk.core import patch_all
patch_all()
log_helper.init_log_config()
def handler(event, context):
"""
Handle dead letter SQS messages through EventBridge every minute.
1. Send a message to query queue
2. Delete a message from dead letter query queue
3. Put the RestartQuery metric
"""
print('event:' + str(event))
count = 0
try:
sqs = Sqs()
athena = Athena()
messages = sqs.receive_deadletter_message()
logging.info('messages : %s' % messages)
while 'Messages' in messages:
if count > 1000:
return
for record in messages['Messages']:
count = count + 1
json_query = record['Body']
print('message num:' + str(count) + ', json_query: ' + json_query)
sqs.send_message(json_query)
receipt_handle = record['ReceiptHandle']
print('Delete receipt_handle:' + receipt_handle)
sqs.delete_deadletter_message(receipt_handle)
messages = sqs.receive_deadletter_message()
else:
print('message is empty!')
return {
"statusCode": 200,
"body": {
"code ": 'OK',
"message ": 'SUCCESS'
}
}
except ClientError as e:
logging.error(e)
print('ERROR: ' + str(e))
return {
"statusCode": 500,
"body": {
"code ": 'ERROR',
"message ": str(e)
}
}
finally:
athena.put_restart_metric('default', count)
# if __name__ == "__main__":
# handler(None, None)