@@ -43,8 +43,7 @@ def __init__(self, name: str, uuid: Optional[str] = None, node_type: str = 'node
43
43
self .needs_login = needs_login
44
44
45
45
self .log = logging .getLogger ('Node' )
46
- self .loop_communicator = LoopCommunicator ()
47
- self .websocket_url = self .loop_communicator .websocket_url ()
46
+ self .init_loop_communicator ()
48
47
self .data_exchanger = DataExchanger (None , self .loop_communicator )
49
48
50
49
self .startup_datetime = datetime .now ()
@@ -59,6 +58,10 @@ def __init__(self, name: str, uuid: Optional[str] = None, node_type: str = 'node
59
58
60
59
self .include_router (router )
61
60
61
+ def init_loop_communicator (self ):
62
+ self .loop_communicator = LoopCommunicator ()
63
+ self .websocket_url = self .loop_communicator .websocket_url ()
64
+
62
65
@property
63
66
def sio_client (self ) -> AsyncClient :
64
67
if self ._sio_client is None :
@@ -117,8 +120,19 @@ async def _on_repeat(self):
117
120
self .log .info ('Reconnecting to loop via sio' )
118
121
await self .connect_sio ()
119
122
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
+
122
136
await self .on_repeat ()
123
137
124
138
# --------------------------------------------------- SOCKET.IO ---------------------------------------------------
@@ -127,7 +141,7 @@ async def create_sio_client(self):
127
141
"""Create a socket.io client that communicates with the learning loop and register the events.
128
142
Note: The method is called in startup and soft restart of detector, so the _sio_client should always be available."""
129
143
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 ())
131
145
self .log .debug ('---------------------------------' )
132
146
if self .loop_communicator .ssl_cert_path :
133
147
logging .info ('SIO using SSL certificate path: %s' , self .loop_communicator .ssl_cert_path )
@@ -145,7 +159,7 @@ async def create_sio_client(self):
145
159
http_session = aiohttp .ClientSession (cookies = self .loop_communicator .get_cookies ()))
146
160
147
161
# 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 )
149
163
150
164
@self ._sio_client .event
151
165
async def connect ():
@@ -166,14 +180,15 @@ async def connect_sio(self):
166
180
try :
167
181
await self .sio_client .disconnect ()
168
182
except Exception :
169
- pass
183
+ self . log . warning ( 'Could not disconnect from loop via sio. Ignoring...' )
170
184
171
185
self .log .info ('(re)connecting to Learning Loop at %s' , self .websocket_url )
172
186
try :
173
187
await self .sio_client .connect (f"{ self .websocket_url } " , headers = self .sio_headers , socketio_path = "/ws/socket.io" )
174
188
self .log .info ('connected to Learning Loop' )
175
189
except socketio .exceptions .ConnectionError : # type: ignore
176
190
self .log .warning ('connection error' )
191
+
177
192
except Exception :
178
193
self .log .exception ('error while connecting to "%s". Exception:' , self .websocket_url )
179
194
0 commit comments