mirror of
https://github.com/anxms/fn_nas.git
synced 2025-10-16 18:17:41 +00:00
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
03f8f3fa21 |
@@ -69,6 +69,9 @@ class FlynasCoordinator(DataUpdateCoordinator):
|
|||||||
|
|
||||||
async def get_ssh_connection(self):
|
async def get_ssh_connection(self):
|
||||||
"""从连接池获取或创建SSH连接"""
|
"""从连接池获取或创建SSH连接"""
|
||||||
|
# 避免递归调用,改为循环等待
|
||||||
|
start_time = time.time()
|
||||||
|
while True:
|
||||||
# 如果连接池中有可用连接且没有超过最大活动命令数
|
# 如果连接池中有可用连接且没有超过最大活动命令数
|
||||||
while len(self.ssh_pool) > 0 and self.active_commands < self.max_connections:
|
while len(self.ssh_pool) > 0 and self.active_commands < self.max_connections:
|
||||||
conn = self.ssh_pool.pop()
|
conn = self.ssh_pool.pop()
|
||||||
@@ -99,9 +102,13 @@ class FlynasCoordinator(DataUpdateCoordinator):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
_LOGGER.error("创建SSH连接失败: %s", str(e), exc_info=True)
|
_LOGGER.error("创建SSH连接失败: %s", str(e), exc_info=True)
|
||||||
raise UpdateFailed(f"SSH连接失败: {str(e)}")
|
raise UpdateFailed(f"SSH连接失败: {str(e)}")
|
||||||
else:
|
|
||||||
|
# 等待0.1秒后重试,避免递归
|
||||||
await asyncio.sleep(0.1)
|
await asyncio.sleep(0.1)
|
||||||
return await self.get_ssh_connection()
|
|
||||||
|
# 设置超时(30秒)
|
||||||
|
if time.time() - start_time > 30:
|
||||||
|
raise UpdateFailed("获取SSH连接超时")
|
||||||
|
|
||||||
async def determine_sudo_setting(self, conn):
|
async def determine_sudo_setting(self, conn):
|
||||||
"""确定是否需要使用sudo权限"""
|
"""确定是否需要使用sudo权限"""
|
||||||
@@ -179,6 +186,9 @@ class FlynasCoordinator(DataUpdateCoordinator):
|
|||||||
async def run_command(self, command: str, retries=2) -> str:
|
async def run_command(self, command: str, retries=2) -> str:
|
||||||
"""使用连接池执行命令"""
|
"""使用连接池执行命令"""
|
||||||
conn = None
|
conn = None
|
||||||
|
current_retries = retries
|
||||||
|
|
||||||
|
while current_retries >= 0:
|
||||||
try:
|
try:
|
||||||
conn = await self.get_ssh_connection()
|
conn = await self.get_ssh_connection()
|
||||||
|
|
||||||
@@ -202,24 +212,27 @@ class FlynasCoordinator(DataUpdateCoordinator):
|
|||||||
# 连接可能已损坏,关闭它
|
# 连接可能已损坏,关闭它
|
||||||
await self.close_connection(conn)
|
await self.close_connection(conn)
|
||||||
conn = None
|
conn = None
|
||||||
if retries > 0:
|
if current_retries > 0:
|
||||||
return await self.run_command(command, retries-1)
|
current_retries -= 1
|
||||||
|
continue
|
||||||
else:
|
else:
|
||||||
raise UpdateFailed(f"Command failed: {command}") from e
|
raise UpdateFailed(f"Command failed: {command}") from e
|
||||||
except asyncssh.Error as e:
|
except asyncssh.Error as e:
|
||||||
_LOGGER.error("SSH连接错误: %s", str(e))
|
_LOGGER.error("SSH连接错误: %s", str(e))
|
||||||
await self.close_connection(conn)
|
await self.close_connection(conn)
|
||||||
conn = None
|
conn = None
|
||||||
if retries > 0:
|
if current_retries > 0:
|
||||||
return await self.run_command(command, retries-1)
|
current_retries -= 1
|
||||||
|
continue
|
||||||
else:
|
else:
|
||||||
raise UpdateFailed(f"SSH错误: {str(e)}") from e
|
raise UpdateFailed(f"SSH错误: {str(e)}") from e
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
_LOGGER.error("意外错误: %s", str(e), exc_info=True)
|
_LOGGER.error("意外错误: %s", str(e), exc_info=True)
|
||||||
await self.close_connection(conn)
|
await self.close_connection(conn)
|
||||||
conn = None
|
conn = None
|
||||||
if retries > 0:
|
if current_retries > 0:
|
||||||
return await self.run_command(command, retries-1)
|
current_retries -= 1
|
||||||
|
continue
|
||||||
else:
|
else:
|
||||||
raise UpdateFailed(f"意外错误: {str(e)}") from e
|
raise UpdateFailed(f"意外错误: {str(e)}") from e
|
||||||
finally:
|
finally:
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"domain": "fn_nas",
|
"domain": "fn_nas",
|
||||||
"name": "飞牛NAS",
|
"name": "飞牛NAS",
|
||||||
"version": "1.3.1",
|
"version": "1.3.3",
|
||||||
"documentation": "https://github.com/anxms/fn_nas",
|
"documentation": "https://github.com/anxms/fn_nas",
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
"codeowners": ["@anxms"],
|
"codeowners": ["@anxms"],
|
||||||
|
Reference in New Issue
Block a user