fix(comfyui): 修复ComfyUI V2连接状态管理问题

- 修复comfyui_v2_connect命令,使用ServiceManager正确管理连接状态
- 修复comfyui_v2_disconnect命令,通过ServiceManager断开连接
- 修复comfyui_v2_get_connection_status命令,返回真实的连接状态
- 解决'获取队列状态失败: 客户端未连接'错误
- 确保连接的管理器实例被正确保存到AppState中

问题原因:
之前的实现创建了临时的ComfyUIManager实例进行连接,
但没有将已连接的实例保存到AppState中,导致后续调用
comfyui_v2_get_queue_status时使用的是未连接的管理器。

解决方案:
通过ServiceManager统一管理ComfyUI连接状态,确保所有
命令使用同一个管理器实例,保持连接状态的一致性。
This commit is contained in:
root 2025-08-08 20:17:53 +08:00
parent 6e59210871
commit 59763b1bf4
1 changed files with 88 additions and 20 deletions

View File

@ -180,17 +180,31 @@ pub async fn comfyui_v2_connect(
format!("更新配置失败: {}", e)
})?;
// 创建 ComfyUI 管理器
let manager = ComfyUIManager::new(converted_config)
// 获取或初始化服务管理器
let service_manager = state.get_comfyui_manager().await
.map_err(|e| {
error!("创建管理器失败: {}", e);
format!("创建管理器失败: {}", e)
error!("获取服务管理器失败: {}", e);
format!("获取服务管理器失败: {}", e)
})?;
// 通过服务管理器重新配置并连接
service_manager.reconfigure(converted_config).await
.map_err(|e| {
error!("重新配置服务失败: {}", e);
format!("重新配置服务失败: {}", e)
})?;
// 获取 ComfyUI 管理器并尝试连接
let comfyui_manager = service_manager.get_comfyui_manager().await
.map_err(|e| {
error!("获取ComfyUI管理器失败: {}", e);
format!("获取ComfyUI管理器失败: {}", e)
})?;
// 尝试连接
match manager.connect().await {
match comfyui_manager.connect().await {
Ok(_) => {
let stats = manager.get_connection_stats().await;
let stats = comfyui_manager.get_connection_stats().await;
info!("ComfyUI 连接成功");
Ok(ConnectionStatusResponse {
@ -216,10 +230,32 @@ pub async fn comfyui_v2_disconnect(
) -> Result<String, String> {
info!("Command: comfyui_v2_disconnect");
// TODO: 从应用状态获取管理器实例
// 这里需要在应用状态中维护管理器实例
Ok("连接已断开".to_string())
// 获取服务管理器
let service_manager = state.get_comfyui_manager().await
.map_err(|e| {
error!("获取服务管理器失败: {}", e);
format!("获取服务管理器失败: {}", e)
})?;
// 获取 ComfyUI 管理器并断开连接
match service_manager.get_comfyui_manager().await {
Ok(comfyui_manager) => {
match comfyui_manager.disconnect().await {
Ok(_) => {
info!("ComfyUI 连接已断开");
Ok("连接已断开".to_string())
}
Err(e) => {
error!("断开连接失败: {}", e);
Err(format!("断开连接失败: {}", e))
}
}
}
Err(e) => {
error!("获取ComfyUI管理器失败: {}", e);
Err(format!("获取ComfyUI管理器失败: {}", e))
}
}
}
/// 检查连接状态
@ -229,16 +265,48 @@ pub async fn comfyui_v2_get_connection_status(
) -> Result<ConnectionStatusResponse, String> {
info!("Command: comfyui_v2_get_connection_status");
// TODO: 从应用状态获取管理器实例
// 临时返回默认状态
Ok(ConnectionStatusResponse {
connected: false,
status: "未连接".to_string(),
base_url: "http://localhost:8188".to_string(),
last_health_check: None,
timeout_seconds: 300,
retry_attempts: 3,
})
// 获取服务管理器
let service_manager = match state.get_comfyui_manager().await {
Ok(manager) => manager,
Err(e) => {
error!("获取服务管理器失败: {}", e);
// 如果服务管理器未初始化,返回默认的未连接状态
return Ok(ConnectionStatusResponse {
connected: false,
status: "未初始化".to_string(),
base_url: "http://localhost:8188".to_string(),
last_health_check: None,
timeout_seconds: 300,
retry_attempts: 3,
});
}
};
// 获取 ComfyUI 管理器并检查连接状态
match service_manager.get_comfyui_manager().await {
Ok(comfyui_manager) => {
let stats = comfyui_manager.get_connection_stats().await;
Ok(ConnectionStatusResponse {
connected: comfyui_manager.is_connected().await,
status: format!("{:?}", stats.status),
base_url: stats.base_url,
last_health_check: stats.last_health_check.map(|t| format!("{:?}", t)),
timeout_seconds: stats.timeout_seconds,
retry_attempts: stats.retry_attempts,
})
}
Err(e) => {
error!("获取ComfyUI管理器失败: {}", e);
Ok(ConnectionStatusResponse {
connected: false,
status: "管理器未初始化".to_string(),
base_url: "http://localhost:8188".to_string(),
last_health_check: None,
timeout_seconds: 300,
retry_attempts: 3,
})
}
}
}
/// 健康检查