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 @@
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 @@

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)

Check warning on line 485 in ssm/ssm2.py

View check run for this annotation

Codecov / codecov/patch

ssm/ssm2.py#L485

Added line #L485 was not covered by tests

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)

Check warning on line 489 in ssm/ssm2.py

View check run for this annotation

Codecov / codecov/patch

ssm/ssm2.py#L489

Added line #L489 was not covered by tests

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 @@
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)

Check warning on line 510 in ssm/ssm2.py

View check run for this annotation

Codecov / codecov/patch

ssm/ssm2.py#L510

Added line #L510 was not covered by tests

log.info('Waiting for broker to accept message.')

Check warning on line 512 in ssm/ssm2.py

View check run for this annotation

Codecov / codecov/patch

ssm/ssm2.py#L512

Added line #L512 was not covered by tests
while self._last_msg is None:
if not self.connected:
raise Ssm2Exception('Lost connection.')

Check warning on line 515 in ssm/ssm2.py

View check run for this annotation

Codecov / codecov/patch

ssm/ssm2.py#L515

Added line #L515 was not covered by tests
# Small sleep to avoid hammering the CPU
time.sleep(0.01)

Check warning on line 517 in ssm/ssm2.py

View check run for this annotation

Codecov / codecov/patch

ssm/ssm2.py#L517

Added line #L517 was not covered by tests

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

Check warning on line 519 in ssm/ssm2.py

View check run for this annotation

Codecov / codecov/patch

ssm/ssm2.py#L519

Added line #L519 was not covered by tests

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

Check warning on line 525 in ssm/ssm2.py

View check run for this annotation

Codecov / codecov/patch

ssm/ssm2.py#L525

Added line #L525 was not covered by tests

if argo_id is not None:
log.info("Sent %s, Argo ID: %s" % (msgid, argo_id))

Check warning on line 528 in ssm/ssm2.py

View check run for this annotation

Codecov / codecov/patch

ssm/ssm2.py#L528

Added line #L528 was not covered by tests
else:
log.warning("Message %s is empty and "

Check warning on line 530 in ssm/ssm2.py

View check run for this annotation

Codecov / codecov/patch

ssm/ssm2.py#L530

Added line #L530 was not covered by tests
"returns a None type." % (msgid))

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