diff --git a/backend/app/main.py b/backend/app/main.py index 370d548..db524b0 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -112,3 +112,9 @@ def health_check(): status["db"] = "error" status["status"] = "degraded" return status + + +@app.get("/api/health") +def api_health_check(): + """API 路径下的健康检查(用于前端通过 /api/ 代理访问)""" + return health_check() diff --git a/backend/app/services/check_service.py b/backend/app/services/check_service.py index a3bcd56..280c57d 100644 --- a/backend/app/services/check_service.py +++ b/backend/app/services/check_service.py @@ -103,8 +103,10 @@ class CheckService: "online": online_count, "offline": offline_count, } - finally: - ssh.close() + except Exception: + # 连接异常时清除缓存,下次自动重连 + _conn_pool.pop(olt_id, None) + raise def check_single_device(self, device_id: int) -> Dict: """通过 SSH 单独查询一台 ONU 设备的当前状态和距离。 @@ -251,8 +253,9 @@ class CheckService: "offline": offline_count, "new_discovered": new_count, } - finally: - ssh.close() + except Exception: + _conn_pool.pop(olt_id, None) + raise async def scan_olt(self, olt_id: int) -> Dict: """仅扫描 OLT,返回发现的设备列表(不写入数据库)""" @@ -300,8 +303,9 @@ class CheckService: "devices": devices, "duplicates": duplicates, } - finally: - ssh.close() + except Exception: + _conn_pool.pop(olt_id, None) + raise def _save_duplicate_macs(self, olt_id: int, duplicate_dict: dict): for mac, records in duplicate_dict.items():