Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop empty messages being sent and log warning gracefully #267

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions ssm/ssm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def _send_msg_ams(self, text, msgid):
encrypted.
"""
log.info('Sending message: %s', msgid)
if text is not None:
if text is not None and len(text) > 0:
# First we sign the message
to_send = crypto.sign(text, self._cert, self._key)
# Possibly encrypt the message.
Expand Down Expand Up @@ -482,31 +482,17 @@ def send_all(self):

if self._protocol == Ssm2.STOMP_MESSAGING:
# Then we are sending to a STOMP message broker.
self._send_msg(text, msgid)

log.info('Waiting for broker to accept message.')
while self._last_msg is None:
if not self.connected:
raise Ssm2Exception('Lost connection.')
# Small sleep to avoid hammering the CPU
time.sleep(0.01)

log_string = "Sent %s" % msgid
self._send_via_stomp(text, msgid)

elif self._protocol == Ssm2.AMS_MESSAGING:
# Then we are sending to an Argo Messaging Service instance.
argo_id = self._send_msg_ams(text, msgid)

log_string = "Sent %s, Argo ID: %s" % (msgid, argo_id)
self._send_via_ams(text, msgid)

else:
# The SSM has been improperly configured
raise Ssm2Exception('Unknown messaging protocol: %s' %
self._protocol)

# log that the message was sent
log.info(log_string)

self._last_msg = None
self._outq.remove(msgid)

Expand All @@ -517,6 +503,33 @@ def send_all(self):
except OSError as e:
log.warning('OSError raised while purging message queue: %s', e)

def _send_via_stomp(self, text, msgid):
"""
Sending message via STOMP message broker.
"""
self._send_msg(text, msgid)

log.info('Waiting for broker to accept message.')
while self._last_msg is None:
if not self.connected:
raise Ssm2Exception('Lost connection.')
# Small sleep to avoid hammering the CPU
time.sleep(0.01)

log.info("Sent %s" % msgid)

def _send_via_ams(self, text, msgid):
"""
Sending message via HTTPS (to Argo message broker.)
"""
argo_id = self._send_msg_ams(text, msgid)

if argo_id is not None:
log.info("Sent %s, Argo ID: %s" % (msgid, argo_id))
else:
log.warning("Message %s is empty and "
"returns a None type." % (msgid))

###########################################################################
# Connection handling methods
###########################################################################
Expand Down