fix: 修复 /api/health 404 和扫描/发现 500 错误
1. /api/health 404: 前端 fetchVersion 调用 /api/health,但后端只有 /health。 OpenResty 保留完整路径,需要在后端添加 /api/health 端点。 2. 扫描/发现 500: check_service.py 的连接池 _get_cached_ssh 缓存了 SSH 连接,但调用方在 finally 中 ssh.close() 关闭连接。第二次调用 从缓存拿到已关闭的连接导致执行失败。改为异常时清除缓存、正常时 保留连接(5分钟TTL自动管理生命周期)。 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -112,3 +112,9 @@ def health_check():
|
|||||||
status["db"] = "error"
|
status["db"] = "error"
|
||||||
status["status"] = "degraded"
|
status["status"] = "degraded"
|
||||||
return status
|
return status
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/health")
|
||||||
|
def api_health_check():
|
||||||
|
"""API 路径下的健康检查(用于前端通过 /api/ 代理访问)"""
|
||||||
|
return health_check()
|
||||||
|
|||||||
@@ -103,8 +103,10 @@ class CheckService:
|
|||||||
"online": online_count,
|
"online": online_count,
|
||||||
"offline": offline_count,
|
"offline": offline_count,
|
||||||
}
|
}
|
||||||
finally:
|
except Exception:
|
||||||
ssh.close()
|
# 连接异常时清除缓存,下次自动重连
|
||||||
|
_conn_pool.pop(olt_id, None)
|
||||||
|
raise
|
||||||
|
|
||||||
def check_single_device(self, device_id: int) -> Dict:
|
def check_single_device(self, device_id: int) -> Dict:
|
||||||
"""通过 SSH 单独查询一台 ONU 设备的当前状态和距离。
|
"""通过 SSH 单独查询一台 ONU 设备的当前状态和距离。
|
||||||
@@ -251,8 +253,9 @@ class CheckService:
|
|||||||
"offline": offline_count,
|
"offline": offline_count,
|
||||||
"new_discovered": new_count,
|
"new_discovered": new_count,
|
||||||
}
|
}
|
||||||
finally:
|
except Exception:
|
||||||
ssh.close()
|
_conn_pool.pop(olt_id, None)
|
||||||
|
raise
|
||||||
|
|
||||||
async def scan_olt(self, olt_id: int) -> Dict:
|
async def scan_olt(self, olt_id: int) -> Dict:
|
||||||
"""仅扫描 OLT,返回发现的设备列表(不写入数据库)"""
|
"""仅扫描 OLT,返回发现的设备列表(不写入数据库)"""
|
||||||
@@ -300,8 +303,9 @@ class CheckService:
|
|||||||
"devices": devices,
|
"devices": devices,
|
||||||
"duplicates": duplicates,
|
"duplicates": duplicates,
|
||||||
}
|
}
|
||||||
finally:
|
except Exception:
|
||||||
ssh.close()
|
_conn_pool.pop(olt_id, None)
|
||||||
|
raise
|
||||||
|
|
||||||
def _save_duplicate_macs(self, olt_id: int, duplicate_dict: dict):
|
def _save_duplicate_macs(self, olt_id: int, duplicate_dict: dict):
|
||||||
for mac, records in duplicate_dict.items():
|
for mac, records in duplicate_dict.items():
|
||||||
|
|||||||
Reference in New Issue
Block a user