1
- from collections import deque
2
- from contextlib import contextmanager
3
1
import contextlib
4
2
import datetime
5
3
import hmac
22
20
from . import utils
23
21
from .abc import BMCLAPIFile , Certificate , CertificateType , OpenBMCLAPIConfiguration , ResponseFile , SocketEmitResult
24
22
from .logger import logger
25
- from .config import API_VERSION , ROOT_PATH , cfg , USER_AGENT
23
+ from .config import API_VERSION , ROOT_PATH , cfg , USER_AGENT , DEBUG
26
24
from .storage import CheckStorage , StorageManager
27
25
from .database import get_db
28
26
@@ -159,6 +157,7 @@ def __init__(
159
157
self .sio = socketio .AsyncClient (
160
158
handle_sigint = False ,
161
159
reconnection_attempts = 10 ,
160
+ logger = DEBUG
162
161
)
163
162
self ._keepalive_lock = utils .CustomLock (locked = True )
164
163
self ._storage_wait = utils .CustomLock (locked = True )
@@ -171,6 +170,7 @@ def __init__(
171
170
self ._retry_times = 0
172
171
self ._task_group = None
173
172
self ._display_name = None
173
+ self ._reconnect_task = None
174
174
self ._manager = manager
175
175
176
176
@property
@@ -216,16 +216,30 @@ async def _():
216
216
if not self ._enabled :
217
217
return
218
218
self ._enabled = False
219
- logger .tinfo ("cluster.reconnect" , id = self .id , name = self .display_name )
220
- await get_db ().insert_cluster_info (self .id , "socketio" , "reconnect" )
221
- await self .enable ()
219
+ self ._want_enable = False
222
220
223
221
224
222
@self .sio .on ("disconnect" ) # type: ignore
225
223
async def _ ():
226
224
logger .tinfo ("cluster.disconnected" , id = self .id , name = self .display_name )
227
225
await self .disable ()
228
226
227
+ @self .sio .eio .on ("reconnect" )
228
+ async def _ (attempt : int ):
229
+ logger .tinfo ("cluster.reconnect" , id = self .id , name = self .display_name , attempt = attempt )
230
+ await get_db ().insert_cluster_info (self .id , "socketio" , "reconnect" )
231
+ await self .enable ()
232
+
233
+ @self .sio .eio .on ("reconnect_error" )
234
+ async def _ (err ):
235
+ logger .terror ("cluster.reconnect_error" , id = self .id , name = self .display_name , err = err )
236
+
237
+ @self .sio .eio .on ("reconnect_failed" )
238
+ async def _ ():
239
+ logger .terror ("cluster.reconnect_failed" , id = self .id , name = self .display_name )
240
+
241
+ task_group .start_soon (self .reconnect )
242
+
229
243
task_group .start_soon (self .keepalive )
230
244
231
245
@utils .event .callback ("storage_disable" )
@@ -238,6 +252,14 @@ async def _(msg: Any):
238
252
239
253
await self .connect ()
240
254
255
+ async def reconnect (self ):
256
+ if self ._reconnect_task is not None :
257
+ return
258
+ await anyio .sleep (60 )
259
+ self ._reconnect_task = True
260
+ await self .connect ()
261
+ self ._reconnect_task = None
262
+
241
263
async def keepalive (self ):
242
264
while not self ._stop :
243
265
await self ._keepalive_lock .wait ()
0 commit comments