Skip to content

Commit

Permalink
Experimental: do not abort startup if cannot connect to STOMP server.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefano Cossu committed Mar 30, 2018
1 parent ad74aad commit 279873e
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions lakesuperior/messaging/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import stomp


logger = logging.getLogger(__name__)


class StompHandler(logging.Handler):
"""
Send messages to a remote queue broker using the STOMP protocol.
Expand All @@ -22,21 +25,30 @@ def __init__(self, conf):

self.conn = conn_cls([(self.conf['host'], self.conf['port'])])
self.conn.start()
self.conn.connect(
username=self.conf['username'],
passcode=self.conf['password'],
wait=True
)
try:
self.conn.connect(
username=self.conf['username'],
passcode=self.conf['password'],
wait=True
)
except stomp.exception.ConnectFailedException:
logger.warning(
'Could not connect to the STOMP server. Your messages '
'will be ditched.')

return super().__init__()


def __del_(self):
"""Disconnect the client."""
self.conn.disconnect()
if self.conn.is_connected():
self.conn.disconnect()

def emit(self, record):
"""Send the message to the destination endpoint."""
self.conn.send(destination=self.conf['destination'],
body=bytes(self.format(record), 'utf-8'))
if self.conn.is_connected():
self.conn.send(destination=self.conf['destination'],
body=bytes(self.format(record), 'utf-8'))
else:
logger.warning('STOMP server not connected. Message dropped.')

0 comments on commit 279873e

Please sign in to comment.