retry connection setup until succeeds;

This commit is contained in:
Marco Zeisler 2021-05-28 19:03:56 +02:00
parent af5c419004
commit d9fe778186

View File

@ -1,7 +1,9 @@
import time
from threading import Thread
from typing import Union
import pika
from pika.exceptions import AMQPConnectionError
class MBWrapper:
@ -79,10 +81,17 @@ class MBWrapper:
self._connection.close()
def _setup_channel(self):
connect_succeeded = False
while not connect_succeeded:
try:
self._connection = pika.BlockingConnection(
pika.ConnectionParameters(host=self.host))
self._channel = self._connection.channel()
connect_succeeded = True
except pika.exceptions.AMQPConnectionError:
print('Could not connect, retry')
time.sleep(1)
self._channel = self._connection.channel()
self._channel.exchange_declare(exchange=self.exchange_name, exchange_type=self.exchange_type)
def _receive(self, ch, method, properties, body):