|
1 | 1 | import asyncio |
2 | 2 |
|
3 | | -from PasarGuardNodeBridge import NodeAPIError, PasarGuardNode |
| 3 | +from PasarGuardNodeBridge import NodeAPIError, PasarGuardNode, Health |
4 | 4 |
|
5 | 5 | from app import on_shutdown, on_startup, scheduler |
6 | 6 | from app.db import GetDB |
@@ -32,13 +32,29 @@ async def check_node(id: int, node: PasarGuardNode): |
32 | 32 | if e.code > 0: |
33 | 33 | await node_operator.connect_node(node_id=id) |
34 | 34 |
|
35 | | - broken_nodes, not_connected_nodes = await node_manager.get_nodes_by_health_status() |
| 35 | + async def check_health(db_node: Node, node: PasarGuardNode): |
| 36 | + if node is None: |
| 37 | + return |
| 38 | + try: |
| 39 | + health = await asyncio.wait_for(node.get_health(), timeout=10) |
| 40 | + except (asyncio.TimeoutError, NodeAPIError): |
| 41 | + await node_operator.update_node_status(db_node.id, NodeStatus.error, err="Get health timeout") |
| 42 | + |
| 43 | + if db_node.status in (NodeStatus.connecting, NodeStatus.error) and health is Health.HEALTHY: |
| 44 | + await node_operator.update_node_status(db_node.id, NodeStatus.connected) |
| 45 | + |
| 46 | + elif db_node.status in (NodeStatus.connecting, NodeStatus.error) and health is Health.NOT_CONNECTED: |
| 47 | + await node_operator.connect_node(node_id=db_node.id) |
36 | 48 |
|
37 | | - check_tasks = [check_node(id, node) for id, node in broken_nodes] |
38 | | - connect_tasks = [node_operator.connect_node(id) for id, _ in not_connected_nodes] |
| 49 | + elif db_node.status == NodeStatus.connected and health is not Health.HEALTHY: |
| 50 | + await check_node(db_node.id, node) |
| 51 | + |
| 52 | + async with GetDB() as db: |
| 53 | + db_nodes = await get_nodes(db=db, enabled=True) |
| 54 | + dict_nodes = await node_manager.get_nodes() |
39 | 55 |
|
40 | | - # Use return_exceptions=True to prevent one failed node from stopping others |
41 | | - await asyncio.gather(*check_tasks + connect_tasks, return_exceptions=True) |
| 56 | + check_tasks = [check_health(db_node, dict_nodes[db_node.id]) for db_node in db_nodes] |
| 57 | + await asyncio.gather(*check_tasks, return_exceptions=True) |
42 | 58 |
|
43 | 59 |
|
44 | 60 | @on_startup |
|
0 commit comments