-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.py
51 lines (42 loc) · 1.62 KB
/
app.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
import logging
import boto3
import os
from botocore.exceptions import ClientError
import json
QUEUE_URI = os.getenv("QUEUE_URI")
logger = logging.getLogger()
logging.basicConfig(level=logging.INFO,
format='%(asctime)s: %(levelname)s: %(message)s')
sqs_client = boto3.client("sqs", region_name = os.getenv("QUEUE_REGION"))
def receive_queue_message():
try:
response = sqs_client.receive_message(QueueUrl=QUEUE_URI, WaitTimeSeconds=5, MaxNumberOfMessages=1)
except ClientError:
logger.exception('Could not receive the message from the - {}.'.format(
QUEUE_URI))
raise
else:
return response
def delete_queue_message(receipt_handle):
try:
response = sqs_client.delete_message(QueueUrl=QUEUE_URI,
ReceiptHandle=receipt_handle)
except ClientError:
logger.exception('Could not delete the meessage from the - {}.'.format(
QUEUE_URI))
raise
else:
return response
if __name__ == '__main__':
while True:
messages = receive_queue_message()
print(messages)
if "Messages" in messages:
for msg in messages['Messages']:
msg_body = msg['Body']
receipt_handle = msg['ReceiptHandle']
logger.info(f'The message body: {msg_body}')
logger.info('Deleting message from the queue...')
resp_delete = delete_queue_message(receipt_handle)
logger.info(
'Received and deleted message(s) from {} with message {}.'.format(QUEUE_URI,resp_delete))