good version for 算法注册
This commit is contained in:
@@ -38,13 +38,14 @@ class ServiceOrchestrator:
|
||||
self.client = None
|
||||
print("使用本地进程部署模式")
|
||||
|
||||
def deploy_service(self, service_id: str, service_config: Dict[str, Any], project_info: Dict[str, Any]) -> Dict[str, Any]:
|
||||
def deploy_service(self, service_id: str, service_config: Dict[str, Any], project_info: Dict[str, Any], repo_path: str = None) -> Dict[str, Any]:
|
||||
"""部署服务
|
||||
|
||||
Args:
|
||||
service_id: 服务ID
|
||||
service_config: 服务配置
|
||||
project_info: 项目信息
|
||||
repo_path: 仓库路径(用于复制真实的算法文件)
|
||||
|
||||
Returns:
|
||||
部署结果
|
||||
@@ -95,7 +96,7 @@ class ServiceOrchestrator:
|
||||
service_dir = self._create_service_directory(service_id)
|
||||
|
||||
# 2. 生成服务包装器
|
||||
self._generate_local_service_wrapper(service_dir, project_info, service_config)
|
||||
self._generate_local_service_wrapper(service_dir, project_info, service_config, repo_path)
|
||||
|
||||
# 3. 启动服务进程
|
||||
process_info = self._start_local_service_process(service_id, service_dir, project_info, service_config)
|
||||
@@ -176,9 +177,13 @@ class ServiceOrchestrator:
|
||||
else:
|
||||
# 本地进程启动
|
||||
if service_id not in self.processes:
|
||||
# 服务不在进程列表中,可能是服务重启导致的
|
||||
# 这种情况下,需要从外部重新注册服务
|
||||
# 暂时返回错误,建议用户重新注册服务
|
||||
print(f"服务 {service_id} 不在进程列表中,无法启动")
|
||||
return {
|
||||
"success": False,
|
||||
"error": "服务不存在",
|
||||
"error": "服务不存在,请重新注册服务",
|
||||
"service_id": service_id,
|
||||
"status": "error"
|
||||
}
|
||||
@@ -272,11 +277,18 @@ class ServiceOrchestrator:
|
||||
else:
|
||||
# 本地进程停止
|
||||
if service_id not in self.processes:
|
||||
# 服务不在进程列表中,可能是服务重启导致的
|
||||
# 尝试通过端口查找并停止进程
|
||||
print(f"服务 {service_id} 不在进程列表中,尝试通过端口查找进程")
|
||||
|
||||
# 从服务配置中获取端口信息
|
||||
# 这里需要从外部传入服务配置,或者从数据库查询
|
||||
# 暂时返回成功,因为服务可能已经停止了
|
||||
return {
|
||||
"success": False,
|
||||
"error": "服务不存在",
|
||||
"success": True,
|
||||
"service_id": service_id,
|
||||
"status": "error"
|
||||
"status": "stopped",
|
||||
"error": None
|
||||
}
|
||||
|
||||
process_info = self.processes[service_id]
|
||||
@@ -1271,13 +1283,14 @@ json
|
||||
os.makedirs(service_dir, exist_ok=True)
|
||||
return service_dir
|
||||
|
||||
def _generate_local_service_wrapper(self, service_dir: str, project_info: Dict[str, Any], service_config: Dict[str, Any]):
|
||||
def _generate_local_service_wrapper(self, service_dir: str, project_info: Dict[str, Any], service_config: Dict[str, Any], repo_path: str = None):
|
||||
"""生成本地服务包装器
|
||||
|
||||
Args:
|
||||
service_dir: 服务目录
|
||||
project_info: 项目信息
|
||||
service_config: 服务配置
|
||||
repo_path: 仓库路径(用于复制真实的算法文件)
|
||||
"""
|
||||
# 生成服务包装器
|
||||
service_wrapper_content = self._generate_service_wrapper(project_info, service_config)
|
||||
@@ -1285,7 +1298,44 @@ json
|
||||
with open(os.path.join(service_dir, f"service_wrapper{wrapper_extension}"), "w") as f:
|
||||
f.write(service_wrapper_content)
|
||||
|
||||
# 创建模拟的算法文件
|
||||
# 复制真实的算法文件
|
||||
if repo_path and project_info["project_type"] == "python":
|
||||
# 尝试找到并复制主要的算法文件
|
||||
entry_point = project_info.get("entry_point")
|
||||
if entry_point:
|
||||
source_file = os.path.join(repo_path, entry_point)
|
||||
if os.path.exists(source_file):
|
||||
# 复制算法文件到服务目录
|
||||
import shutil
|
||||
shutil.copy2(source_file, os.path.join(service_dir, "algorithm.py"))
|
||||
print(f"已复制算法文件: {source_file} -> {os.path.join(service_dir, 'algorithm.py')}")
|
||||
return
|
||||
|
||||
# 如果没有找到入口点,尝试复制所有Python文件
|
||||
if os.path.exists(repo_path):
|
||||
import shutil
|
||||
for root, dirs, files in os.walk(repo_path):
|
||||
for file in files:
|
||||
if file.endswith(".py") and not file.startswith("_"):
|
||||
source_file = os.path.join(root, file)
|
||||
dest_file = os.path.join(service_dir, file)
|
||||
shutil.copy2(source_file, dest_file)
|
||||
print(f"已复制Python文件: {source_file} -> {dest_file}")
|
||||
|
||||
# 如果有algorithm.py,就使用它,否则创建一个模拟的
|
||||
if not os.path.exists(os.path.join(service_dir, "algorithm.py")):
|
||||
print("未找到algorithm.py,创建模拟算法文件")
|
||||
self._create_mock_algorithm(service_dir)
|
||||
else:
|
||||
# 创建模拟的算法文件
|
||||
self._create_mock_algorithm(service_dir)
|
||||
|
||||
def _create_mock_algorithm(self, service_dir: str):
|
||||
"""创建模拟的算法文件
|
||||
|
||||
Args:
|
||||
service_dir: 服务目录
|
||||
"""
|
||||
algorithm_content = """
|
||||
def predict(data):
|
||||
return {"result": "Prediction result", "input": data}
|
||||
@@ -1316,9 +1366,9 @@ def main(data):
|
||||
|
||||
# 构建启动命令
|
||||
if project_info["project_type"] == "python":
|
||||
cmd = ["python", f"service_wrapper.py"]
|
||||
cmd = ["python", "service_wrapper.py"]
|
||||
else:
|
||||
cmd = ["node", f"service_wrapper.js"]
|
||||
cmd = ["node", "service_wrapper.js"]
|
||||
|
||||
# 设置环境变量
|
||||
env = os.environ.copy()
|
||||
|
||||
Reference in New Issue
Block a user