Răsfoiți Sursa

修复部署服务重启后task_id变化

lxylxy123321 2 zile în urmă
părinte
comite
a835c37dba
2 a modificat fișierele cu 21 adăugiri și 3 ștergeri
  1. 20 2
      backend/app/services/deploy_service.py
  2. 1 1
      test.py

+ 20 - 2
backend/app/services/deploy_service.py

@@ -398,9 +398,14 @@ async def restart_serving(task_id: str, user_id: str = "") -> dict[str, Any]:
             return {"error": "模型文件路径丢失,无法重启,请重新部署"}
 
         output_path = record.output_path
+        original_port = record.port  # 记住原端口,尽量复用
 
-    # 分配新端口
-    port = await _allocate_port()
+    # 优先复用原端口,如果被其他 pending/running 服务占了才重新分配
+    if original_port:
+        port_available = await _check_port_available(original_port)
+        port = original_port if port_available else await _allocate_port()
+    else:
+        port = await _allocate_port()
 
     # 更新状态为 pending,标记正在重启
     await _update_deploy_status(task_id, "pending", port=port)
@@ -520,6 +525,19 @@ async def _allocate_port() -> int:
     raise RuntimeError(f"无可用端口({_SERVE_PORT_MIN}-{_SERVE_PORT_MAX} 全部占用)")
 
 
+async def _check_port_available(port: int) -> bool:
+    """检查指定端口是否可被复用(没有被其他 pending/running 服务占用)。"""
+    async with async_session() as session:
+        result = await session.execute(
+            select(DeployTaskModel.id).where(
+                DeployTaskModel.deploy_mode == "serve",
+                DeployTaskModel.status.in_(["pending", "running"]),
+                DeployTaskModel.port == port,
+            )
+        )
+        return result.first() is None
+
+
 async def _run_remote_export(task_id: str, job_id: str, merge_with_base: bool, export_format: str) -> dict:
     """通过 SSH 在远程容器执行模型合并/导出。"""
     remote_cmd = (

+ 1 - 1
test.py

@@ -1,7 +1,7 @@
 from openai import OpenAI
 
 client = OpenAI(
-    base_url="http://192.168.92.151:3000/api/v1/deployment/proxy/abececf1-74e0-4374-8f0b-504b0f08730d/v1",
+    base_url="http://192.168.92.151:3000/api/v1/deployment/proxy/1ce356ff-49d3-48da-a583-46e87c2776da/v1",
     api_key="sk-1wTkTvsfu0IiyZFhNAx8HMgtIf2TxLGP-DyrcNKYlIc"  # 替换为你的 API Key
 )