Added more error handling and reporting
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-07-26 17:01:10 +02:00
parent ba69b9c2b1
commit a844a13608
4 changed files with 50 additions and 4 deletions

View File

@ -50,19 +50,33 @@ class MagicAMPQ:
"""
This method should be called periodically to keep up the connection
"""
lock_start = time.time()
with self._lock:
lock_acquire_time = time.time() - lock_start
if lock_acquire_time >= 1.5:
self.app.logger.warning(f"Loop: Lock acquire took {lock_acquire_time:5f} sec")
try:
self._pika_connection.process_data_events(0)
# We won't attempt retry if this fail
except pika.exceptions.AMQPConnectionError:
except pika.exceptions.AMQPConnectionError as e:
self.app.logger.warning(f"Connection error during process loop: {e} (attempting reconnect)")
self._reconnect_ampq()
total_time = time.time() - lock_start
if total_time > 2:
self.app.logger.warning(f"Loop: Total loop took {total_time:5f} sec")
def publish(self, payload=None):
"""
Publish a simple json serialized message to the configured queue.
If the connection is broken, then this call will block until the connection is restored
"""
lock_start = time.time()
with self._lock:
lock_acquire_time = time.time() - lock_start
if lock_acquire_time >= 0.3:
self.app.logger.warning(f"Publish: Lock acquire took {lock_acquire_time:5f} sec")
tries = 0
while True:
try:
@ -72,7 +86,8 @@ class MagicAMPQ:
body=json.dumps(payload).encode('UTF-8')
)
break # message sent successfully
except pika.exceptions.AMQPConnectionError:
except pika.exceptions.AMQPConnectionError as e:
self.app.logger.warning(f"Connection error during publish: {e} (attempting reconnect)")
if tries > 30:
raise # just give up
@ -81,7 +96,8 @@ class MagicAMPQ:
try:
self._reconnect_ampq()
break
except pika.exceptions.AMQPConnectionError:
except pika.exceptions.AMQPConnectionError as e:
self.app.logger.warning(f"Connection error during reconnection: {e} (attempting reconnect)")
tries += 1
if tries > 30:
@ -89,6 +105,9 @@ class MagicAMPQ:
if tries > 10:
time.sleep(2)
total_time = time.time() - lock_start
if total_time > 0.5:
self.app.logger.warning(f"Publish: Total publish took {total_time:5f} sec")
def is_healthy(self) -> bool:
with self._lock: