-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_messages.py
60 lines (47 loc) · 1.59 KB
/
get_messages.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
import boto3
import os
import json
import sys
from datetime import datetime
MAX_EMPTY_RECEIVES=int(os.getenv('MAX_EMPTY_RECEIVES'))
VISIBILITY_TIMEOUT=int(os.getenv('VISIBILITY_TIMEOUT'))
AWS_ACCESS_KEY_ID=os.getenv('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY=os.getenv('AWS_SECRET_ACCESS_KEY')
AWS_SESSION_TOKEN=os.getenv('AWS_SESSION_TOKEN')
AWS_REGION_NAME=os.getenv('AWS_REGION_NAME')
QUEUE_URL=os.getenv('QUEUE_NAME_URL')
if len(sys.argv) > 1:
declared_number_of_messages = int(sys.argv[1])
else:
declared_number_of_messages = 10000
time_now = datetime.now()
queue_name = QUEUE_URL.split("/")[-1]
file_prefix = "messages/"
file_name = file_prefix + time_now.strftime("%Y-%m-%d-%H-%M-%S-") + queue_name + ".json"
session = boto3.Session(
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
region_name=AWS_REGION_NAME,
aws_session_token=AWS_SESSION_TOKEN,
)
sqs_client = session.client('sqs')
all_messages = []
empty_receives = 0
while (empty_receives < MAX_EMPTY_RECEIVES and len(all_messages) < declared_number_of_messages):
resp = sqs_client.receive_message(
QueueUrl=QUEUE_URL,
AttributeNames=['All'],
VisibilityTimeout=VISIBILITY_TIMEOUT,
MaxNumberOfMessages=10
)
try:
all_messages += resp['Messages']
except KeyError:
print('Empty receive!')
empty_receives += 1
all_body= list(map(lambda msg: json.loads(msg['Body']),all_messages))
f = open(file_name, "w")
f.write(json.dumps(all_body, indent=2))
f.close()
print("Messages retrieved: ", len(all_body))
print("Saved on ", file_name)