@@ -55,14 +55,21 @@ async def update_node_connection_status(node_id: int, node: PasarGuardNode):
5555 """
5656 try :
5757 await node .get_backend_stats (timeout = 8 )
58- await node_operator .update_node_status (
59- node_id , NodeStatus .connected , await node .core_version (), await node .node_version ()
60- )
58+ async with GetDB () as db :
59+ await NodeOperation ._update_single_node_status (
60+ db ,
61+ node_id ,
62+ NodeStatus .connected ,
63+ xray_version = await node .core_version (),
64+ node_version = await node .node_version (),
65+ )
6166 except NodeAPIError as e :
6267 if e .code > - 3 :
63- await node_operator .update_node_status (node_id , NodeStatus .error , err = e .detail )
68+ async with GetDB () as db :
69+ await NodeOperation ._update_single_node_status (db , node_id , NodeStatus .error , message = e .detail )
6470 if e .code > 0 :
65- await node_operator .connect_node (node_id = node_id )
71+ async with GetDB () as db :
72+ await node_operator .connect_node_wrapper (db , node_id )
6673
6774
6875async def process_node_health_check (db_node : Node , node : PasarGuardNode ):
@@ -77,16 +84,23 @@ async def process_node_health_check(db_node: Node, node: PasarGuardNode):
7784 return
7885
7986 if node .requires_hard_reset ():
80- await node_operator .connect_node (db_node .id )
87+ async with GetDB () as db :
88+ await node_operator .connect_node_wrapper (db , db_node .id )
8189 return
8290
8391 try :
8492 health = await asyncio .wait_for (verify_node_backend_health (node , db_node .name ), timeout = 15 )
8593 except asyncio .TimeoutError :
86- await node_operator .update_node_status (db_node .id , NodeStatus .error , err = "Health check timeout" )
94+ async with GetDB () as db :
95+ await NodeOperation ._update_single_node_status (
96+ db , db_node .id , NodeStatus .error , message = "Health check timeout"
97+ )
8798 return
8899 except NodeAPIError :
89- await node_operator .update_node_status (db_node .id , NodeStatus .error , err = "Get health failed" )
100+ async with GetDB () as db :
101+ await NodeOperation ._update_single_node_status (
102+ db , db_node .id , NodeStatus .error , message = "Get health failed"
103+ )
90104 return
91105
92106 # Skip nodes that are already healthy and connected
@@ -95,12 +109,14 @@ async def process_node_health_check(db_node: Node, node: PasarGuardNode):
95109
96110 # Update status for recovering nodes
97111 if db_node .status in (NodeStatus .connecting , NodeStatus .error ) and health == Health .HEALTHY :
98- await node_operator .update_node_status (
99- db_node .id ,
100- NodeStatus .connected ,
101- core_version = await node .core_version (),
102- node_version = await node .node_version (),
103- )
112+ async with GetDB () as db :
113+ await NodeOperation ._update_single_node_status (
114+ db ,
115+ db_node .id ,
116+ NodeStatus .connected ,
117+ xray_version = await node .core_version (),
118+ node_version = await node .node_version (),
119+ )
104120 return
105121
106122 # For all other cases, update connection status
@@ -126,27 +142,10 @@ async def initialize_nodes():
126142 async with GetDB () as db :
127143 db_nodes = await get_nodes (db = db , enabled = True )
128144
129- # Semaphore to limit concurrent node startups to 3 at a time
130- semaphore = asyncio .Semaphore (3 )
131-
132- async def start_node (node : Node ):
133- try :
134- await node_manager .update_node (node )
135- except NodeAPIError as e :
136- await node_operator .update_node_status (node .id , NodeStatus .error , err = e .detail )
137- return
138-
139- await node_operator .connect_node (node_id = node .id )
140-
141- async def start_node_with_limit (node : Node ):
142- async with semaphore :
143- await start_node (node )
144-
145145 if not db_nodes :
146146 logger .warning ("Attention: You have no node, you need to have at least one node" )
147147 else :
148- start_tasks = [start_node_with_limit (node = db_node ) for db_node in db_nodes ]
149- await asyncio .gather (* start_tasks )
148+ await node_operator .connect_nodes_bulk (db , db_nodes )
150149 logger .info ("All nodes' cores have been started." )
151150
152151 scheduler .add_job (
0 commit comments