From d689381c9c8be7efb0bfa69700d70a3bd6a3f654 Mon Sep 17 00:00:00 2001 From: mdivecky Date: Mon, 28 Sep 2020 22:32:17 +0200 Subject: [PATCH] catch bad messages in RabbitMQ --- sender/sender.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/sender/sender.py b/sender/sender.py index d17ed8f..f453c83 100644 --- a/sender/sender.py +++ b/sender/sender.py @@ -37,19 +37,30 @@ def send_message(text, sender, destination): return True else: - print('Sender not in registrations.json') + print('Sender not in registrations.json.') return False def callback(ch, method, properties, body): - body = json.loads(body) - text = body[0] - sender = body[1] - destination = body[2] - if send_message(text, sender, destination): - channel.basic_ack(delivery_tag=method.delivery_tag) - return True + try: + body = json.loads(body) + if len(body) == 3: + text = body[0] + sender = body[1] + destination = body[2] + + if send_message(text, sender, destination): + print("Message sent succesfully") + else: + print("Sending message failed") + else: + print("Sending message failed") + channel.basic_ack(delivery_tag=method.delivery_tag) + return True + except: + channel.basic_ack(delivery_tag=method.delivery_tag) + pass channel.basic_consume('signal-send', callback, auto_ack=False)