Skip to content

Commit

Permalink
Support simpler broker and database connections
Browse files Browse the repository at this point in the history
  • Loading branch information
astrobokonon committed Mar 2, 2021
1 parent f39de26 commit bf4e88c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
28 changes: 28 additions & 0 deletions ligmos/utils/amq.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,34 @@ def setupAMQBroker(cblk, topics, listener=None):
return conn, listener


def checkSingleConnection(broker, subscribe=True):
"""
This is intended to be inside of some loop structure.
It's primarily used for checking whether the connection to the ActiveMQ
broker is still valid, and, if it was killed (set to None) because the
heartbeat failed, attempt to both reconnect and resubscribe to the
topics.
"""
connChecking = broker[0]
thisListener = broker[1]

if connChecking.conn is None:
print("No connection at all! Retrying...")
# The topics were already stuffed into the connChecking object,
# but it's nice to remember that we're subscribing to them
connChecking.connect(listener=thisListener, subscribe=subscribe)
elif connChecking.conn.transport.connected is False:
print("Connection died! Reestablishing...")
connChecking.connect(listener=thisListener, subscribe=subscribe)
else:
print("Connection still valid")

# Make sure we save any connection changes and give it back
broker = [connChecking, thisListener]

return broker


def checkConnections(amqbrokers, subscribe=True):
"""
This is intended to be inside of some loop structure.
Expand Down
25 changes: 25 additions & 0 deletions ligmos/workers/connSetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,35 @@
from ..utils import database


def connAMQ_simple(comm, topics, listener=None):
"""
Set up the actual connections, which we'll then give back to the actual
objects for them to do stuff with afterwards.
"""
# Now check the properties of this object to see if it's something we
# actually regconize and then connect to
if comm.type.lower().startswith('activemq'):
# We get brokerlistener back as a return just in case it was
# None initially, in which case amq.setupBroker would give one
conn, amqlistener = amq.setupAMQBroker(comm, topics, listener=listener)

# Store this so we can check/use it later
bkr = [conn, amqlistener]
else:
# No other types are defined yet
bkr = None

return bkr


def connAMQ(comm, amqtopics, amqlistener=None):
"""
Set up the actual connections, which we'll then give back to the actual
objects for them to do stuff with afterwards.
Assumes that you're subscribing to many topics specified in
different configuration sections in comm; if that's not the case, use
connAMQ_simple() instead!
"""
amqbrokers = {}

Expand Down

0 comments on commit bf4e88c

Please sign in to comment.