Skip to content

Commit

Permalink
Fix shutdown (#335)
Browse files Browse the repository at this point in the history
Relates to #220 . This change is to enable blueapi to be shutdown with
crtl+c when the activemq drops and it can no longer connect to it.

---------

Co-authored-by: Callum Forrester <[email protected]>
  • Loading branch information
abbiemery and callumforrester authored Nov 24, 2023
1 parent b30dda7 commit 885636a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/blueapi/messaging/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,12 @@ def disconnect(self) -> None:
"""
Disconnect the app from transport
"""

@abstractmethod
def is_connected(self) -> bool:
"""
Returns status of the connection between the app and the transport.
Returns:
status (bool): Returns True if connected, False otherwise
"""
3 changes: 3 additions & 0 deletions src/blueapi/messaging/stomptemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,6 @@ def _on_message(self, frame: Frame) -> None:
sub.callback(frame)
else:
LOGGER.warn(f"No subscription active for id: {sub_id}")

def is_connected(self) -> bool:
return self._conn.is_connected()
3 changes: 2 additions & 1 deletion src/blueapi/service/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def _publish_event_stream(self, stream: EventStream, destination: str) -> None:

def stop(self) -> None:
self.worker.stop()
self.messaging_template.disconnect()
if self.messaging_template.is_connected():
self.messaging_template.disconnect()


HANDLER: Optional[Handler] = None
Expand Down
2 changes: 2 additions & 0 deletions tests/messaging/test_stomptemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ def test_reconnect(template: MessagingTemplate, test_queue: str) -> None:
reply = template.send_and_receive(test_queue, "test", str).result(timeout=_TIMEOUT)
assert reply == "ack"
template.disconnect()
assert not template.is_connected()
template.connect()
assert template.is_connected()
reply = template.send_and_receive(test_queue, "test", str).result(timeout=_TIMEOUT)
assert reply == "ack"

Expand Down

0 comments on commit 885636a

Please sign in to comment.