Skip to content

Commit 60cae5b

Browse files
committed
fix: sio reconnect
1 parent 913143a commit 60cae5b

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

learning_loop_node/detector/rest/backdoor_controls.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ async def _reset(request: Request):
4444
# restart_path = Path(os.getcwd()).absolute() / 'app_code' / 'restart' / 'restart.py'
4545
# restart_path.touch()
4646
# assert isinstance(request.app, 'DetectorNode')
47-
await request.app.soft_reload()
47+
app: 'DetectorNode' = request.app
48+
await app.soft_reload()
4849

4950
# assert isinstance(request.app, DetectorNode)
5051
# request.app.reload(reason='------- reset was called from backdoor controls',)

learning_loop_node/node.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ def __init__(self, name: str, uuid: Optional[str] = None, node_type: str = 'node
4343
self.needs_login = needs_login
4444

4545
self.log = logging.getLogger('Node')
46-
self.loop_communicator = LoopCommunicator()
47-
self.websocket_url = self.loop_communicator.websocket_url()
46+
self.init_loop_communicator()
4847
self.data_exchanger = DataExchanger(None, self.loop_communicator)
4948

5049
self.startup_datetime = datetime.now()
@@ -59,6 +58,10 @@ def __init__(self, name: str, uuid: Optional[str] = None, node_type: str = 'node
5958

6059
self.include_router(router)
6160

61+
def init_loop_communicator(self):
62+
self.loop_communicator = LoopCommunicator()
63+
self.websocket_url = self.loop_communicator.websocket_url()
64+
6265
@property
6366
def sio_client(self) -> AsyncClient:
6467
if self._sio_client is None:
@@ -117,8 +120,19 @@ async def _on_repeat(self):
117120
self.log.info('Reconnecting to loop via sio')
118121
await self.connect_sio()
119122
if not self.sio_client.connected:
120-
self.log.warning('Could not connect to loop via sio')
121-
return
123+
try:
124+
self.log.warning('Could not connect to loop via sio. Reconnecting...')
125+
126+
self.init_loop_communicator()
127+
if self.needs_login:
128+
await self.loop_communicator.ensure_login(relogin=True)
129+
await self.create_sio_client()
130+
if not self.sio_client.connected:
131+
raise Exception('Could not connect to loop via sio')
132+
except Exception as e:
133+
self.log.exception('Fatal error while reconnecting to loop via sio: %s', e)
134+
return
135+
122136
await self.on_repeat()
123137

124138
# --------------------------------------------------- SOCKET.IO ---------------------------------------------------
@@ -127,7 +141,7 @@ async def create_sio_client(self):
127141
"""Create a socket.io client that communicates with the learning loop and register the events.
128142
Note: The method is called in startup and soft restart of detector, so the _sio_client should always be available."""
129143
self.log.debug('--------------Connect HTTP Cookie-------------------')
130-
self.log.debug(f'Cookies: {self.loop_communicator.get_cookies()}')
144+
self.log.debug('Cookies: %s', self.loop_communicator.get_cookies())
131145
self.log.debug('---------------------------------')
132146
if self.loop_communicator.ssl_cert_path:
133147
logging.info('SIO using SSL certificate path: %s', self.loop_communicator.ssl_cert_path)
@@ -145,7 +159,7 @@ async def create_sio_client(self):
145159
http_session=aiohttp.ClientSession(cookies=self.loop_communicator.get_cookies()))
146160

147161
# pylint: disable=protected-access
148-
self.sio_client._trigger_event = ensure_socket_response(self.sio_client._trigger_event)
162+
self._sio_client._trigger_event = ensure_socket_response(self._sio_client._trigger_event)
149163

150164
@self._sio_client.event
151165
async def connect():
@@ -166,14 +180,15 @@ async def connect_sio(self):
166180
try:
167181
await self.sio_client.disconnect()
168182
except Exception:
169-
pass
183+
self.log.warning('Could not disconnect from loop via sio. Ignoring...')
170184

171185
self.log.info('(re)connecting to Learning Loop at %s', self.websocket_url)
172186
try:
173187
await self.sio_client.connect(f"{self.websocket_url}", headers=self.sio_headers, socketio_path="/ws/socket.io")
174188
self.log.info('connected to Learning Loop')
175189
except socketio.exceptions.ConnectionError: # type: ignore
176190
self.log.warning('connection error')
191+
177192
except Exception:
178193
self.log.exception('error while connecting to "%s". Exception:', self.websocket_url)
179194

0 commit comments

Comments
 (0)