fix: wait for req socket send to make sure we dont stay stuck - if there's no REP this would be awaited forever.
ref: N25B-205
This commit is contained in:
@@ -17,6 +17,7 @@ class RICommunicationAgent(Agent):
|
|||||||
req_socket: zmq.Socket
|
req_socket: zmq.Socket
|
||||||
_address = ""
|
_address = ""
|
||||||
_bind = True
|
_bind = True
|
||||||
|
connected = False
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@@ -40,17 +41,34 @@ class RICommunicationAgent(Agent):
|
|||||||
|
|
||||||
# We need to listen and sent pings.
|
# We need to listen and sent pings.
|
||||||
message = {"endpoint": "ping", "data": {"id": "e.g. some reference id"}}
|
message = {"endpoint": "ping", "data": {"id": "e.g. some reference id"}}
|
||||||
await self.agent.req_socket.send_json(message)
|
seconds_to_wait_total = 4.0
|
||||||
|
try:
|
||||||
|
await asyncio.wait_for(
|
||||||
|
self.agent.req_socket.send_json(message), timeout=seconds_to_wait_total / 2
|
||||||
|
)
|
||||||
|
except TimeoutError as e:
|
||||||
|
logger.debug(
|
||||||
|
f"Waited too long to send message - we probably dont have any receivers... but let's check!"
|
||||||
|
)
|
||||||
|
|
||||||
# Wait up to three seconds for a reply:)
|
# Wait up to three seconds for a reply:)
|
||||||
try:
|
try:
|
||||||
message = await asyncio.wait_for(self.agent.req_socket.recv_json(), timeout=3.0)
|
logger.debug(f"waiting for message for {seconds_to_wait_total / 2} seconds.")
|
||||||
|
message = await asyncio.wait_for(
|
||||||
|
self.agent.req_socket.recv_json(), timeout=seconds_to_wait_total / 2
|
||||||
|
)
|
||||||
|
|
||||||
# We didnt get a reply :(
|
# We didnt get a reply :(
|
||||||
except asyncio.TimeoutError as e:
|
except TimeoutError as e:
|
||||||
logger.info("No ping retrieved in 3 seconds, killing myself.")
|
logger.info(f"No ping back retrieved in {seconds_to_wait_total/2} seconds totalling {seconds_to_wait_total} of time, killing myself.")
|
||||||
|
self.agent.connected = False
|
||||||
|
# TODO: Send event to UI letting know that we've lost connection
|
||||||
|
|
||||||
self.kill()
|
self.kill()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug(f"Differennt exception: {e}")
|
||||||
|
|
||||||
logger.debug('Received message "%s"', message)
|
logger.debug('Received message "%s"', message)
|
||||||
if "endpoint" not in message:
|
if "endpoint" not in message:
|
||||||
logger.error("No received endpoint in message, excepted ping endpoint.")
|
logger.error("No received endpoint in message, excepted ping endpoint.")
|
||||||
@@ -162,4 +180,7 @@ class RICommunicationAgent(Agent):
|
|||||||
# Set up ping behaviour
|
# Set up ping behaviour
|
||||||
listen_behaviour = self.ListenBehaviour()
|
listen_behaviour = self.ListenBehaviour()
|
||||||
self.add_behaviour(listen_behaviour)
|
self.add_behaviour(listen_behaviour)
|
||||||
|
|
||||||
|
# TODO: Let UI know that we're connected >:)
|
||||||
|
self.connected = True
|
||||||
logger.info("Finished setting up %s", self.jid)
|
logger.info("Finished setting up %s", self.jid)
|
||||||
|
|||||||
@@ -18,5 +18,4 @@ async def receive_command(command: SpeechCommand, request: Request):
|
|||||||
pub_socket: Socket = request.app.state.internal_comm_socket
|
pub_socket: Socket = request.app.state.internal_comm_socket
|
||||||
pub_socket.send_multipart([topic, command.model_dump_json().encode()])
|
pub_socket.send_multipart([topic, command.model_dump_json().encode()])
|
||||||
|
|
||||||
|
|
||||||
return {"status": "Command received"}
|
return {"status": "Command received"}
|
||||||
|
|||||||
Reference in New Issue
Block a user