From 8c42ae77216c27950d2bdf9ce8d73877eebb804b Mon Sep 17 00:00:00 2001 From: dingjiahuichina Date: Thu, 23 Oct 2025 09:50:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E4=BD=BF=E6=95=B0=E6=8D=AE=E6=9C=AA=E6=B8=85?= =?UTF-8?q?=E7=90=86=E9=97=AE=E9=A2=98=EF=BC=8C=E4=BF=AE=E5=A4=8DOEDP?= =?UTF-8?q?=E5=92=8CMCP=E5=85=B6=E4=B8=80=E5=90=8C=E6=AD=A5=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E5=AF=BC=E8=87=B4=E5=8F=A6=E4=B8=80=E4=B9=9F=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/artifacts/methods/mcp_methods.py | 11 ++++- backend/artifacts/methods/plugin_methods.py | 46 ++++++++++++++------- backend/artifacts/views.py | 36 ++++++++++------ 3 files changed, 63 insertions(+), 30 deletions(-) diff --git a/backend/artifacts/methods/mcp_methods.py b/backend/artifacts/methods/mcp_methods.py index ca28dbc..600d5b0 100644 --- a/backend/artifacts/methods/mcp_methods.py +++ b/backend/artifacts/methods/mcp_methods.py @@ -39,18 +39,23 @@ class MCPMethods: @staticmethod def sync_mcps() -> Dict[str, Any]: """同步MCP服务信息""" - try: + try: mcp_data, message = MCPMethods._read_mcp_info() if mcp_data is None: + # 读取失败时清空数据库 + clear_table(MCPServer._meta.db_table) return {'is_success': False, 'message': message} - + + # MCP解析过程可能耗时,因此数据库清理在MCP解析之后,避免出现较长空窗期 clear_table(MCPServer._meta.db_table) if mcp_data: + # 数据序列化并验证 serializer = MCPBulkCreateSerializer(data=mcp_data, many=True) if not serializer.is_valid(): return {'is_success': False, 'message': serializer.errors} + # 写入新数据 mcps = serializer.save() msg = f"Successfully synced {len(mcps)} MCP packages" else: @@ -60,6 +65,8 @@ class MCPMethods: except Exception as e: logger.error(f"Sync MCP failed: {str(e)}") + # 发生异常时清空数据库 + clear_table(MCPServer._meta.db_table) return {'is_success': False, 'message': f'Sync failed: {str(e)}'} @staticmethod diff --git a/backend/artifacts/methods/plugin_methods.py b/backend/artifacts/methods/plugin_methods.py index 6c546f3..0685c64 100644 --- a/backend/artifacts/methods/plugin_methods.py +++ b/backend/artifacts/methods/plugin_methods.py @@ -48,22 +48,38 @@ class PluginMethods: @staticmethod def sync_plugins(): """同步插件信息到数据库""" - update_result, msg = PluginMethods._update_plugin_info() - if not update_result: - return {'is_success': False, 'message': msg} - plugin_data, msg = PluginMethods._read_plugin_info() - if not plugin_data: - return {'is_success': False, 'message': msg} + try: + update_result, msg = PluginMethods._update_plugin_info() + if not update_result: + # 更新失败时清空数据库 + clear_table(OEDPPlugin._meta.db_table) + return {'is_success': False, 'message': msg} + + plugin_data, msg = PluginMethods._read_plugin_info() + if not plugin_data: + # 读取失败时清空数据库 + clear_table(OEDPPlugin._meta.db_table) + return {'is_success': False, 'message': msg} - # 将插件的信息存入数据库中 - serializer = PluginBulkCreateSerializer(data=plugin_data, many=True) - clear_table(OEDPPlugin._meta.db_table) - if not serializer.is_valid(): - logger.error(f"Failed to validate plugin data, errors: {serializer.errors}") - return {'is_success': False, 'message': serializer.errors} - plugins = serializer.save() - logger.info("Store plugin data to database successfully.") - return {'is_success': True, 'message': "Sync plugin data successfully."} + # 插件解析过程可能耗时,因此数据库清理在插件解析之后,避免出现较长空窗期 + clear_table(OEDPPlugin._meta.db_table) + + # 数据序列化并验证 + serializer = PluginBulkCreateSerializer(data=plugin_data, many=True) + if not serializer.is_valid(): + logger.error(f"Failed to validate plugin data, errors: {serializer.errors}") + return {'is_success': False, 'message': serializer.errors} + + # 写入新数据 + plugins = serializer.save() + logger.info("Store plugin data to database successfully.") + return {'is_success': True, 'message': "Sync plugin data successfully."} + + except Exception as e: + logger.error(f"Sync plugins failed: {str(e)}") + # 发生异常时清空数据库 + clear_table(OEDPPlugin._meta.db_table) + return {'is_success': False, 'message': f'Sync failed: {str(e)}'} @staticmethod def download_plugin(key: str): diff --git a/backend/artifacts/views.py b/backend/artifacts/views.py index 65b9121..d016abd 100644 --- a/backend/artifacts/views.py +++ b/backend/artifacts/views.py @@ -50,20 +50,30 @@ class ArtifactViewSet(viewsets.GenericViewSet): logger.info("==== API: [POST] /v1.0/artifacts/sync/ ====") # 使用PluginMethods同步插件信息 - result = PluginMethods.sync_plugins() - if not result['is_success']: - return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) - + plugin_result = PluginMethods.sync_plugins() + plugin_success = plugin_result['is_success'] + # 使用MCPMethods同步MCP服务信息 - result = MCPMethods.sync_mcps() - if not result['is_success']: - return Response(result, status=status.HTTP_500_INTERNAL_SERVER_ERROR) - - # 仅返回调用结果 - data_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") - msg = "Sync data successfully." - logger.info(msg) - return Response({'is_success': True, 'message': msg, 'time': data_time}, status=status.HTTP_200_OK) + mcp_result = MCPMethods.sync_mcps() + mcp_success = mcp_result['is_success'] + + # 只要插件或MCP服务任一同步成功,就返回成功 + if plugin_success or mcp_success: + data_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + msg = "Sync data successfully." + if plugin_success and mcp_success: + msg = "Both plugin and MCP service data synced successfully." + elif plugin_success: + msg = "Plugin data synced successfully (MCP service sync failed)." + elif mcp_success: + msg = "MCP service data synced successfully (plugin sync failed)." + logger.info(msg) + return Response({'is_success': True, 'message': msg, 'time': data_time}, status=status.HTTP_200_OK) + else: + # 如果两者都失败,返回错误 + msg = "Both plugin and MCP service sync failed." + logger.error(msg) + return Response({'is_success': False, 'message': msg}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) @action(methods=['POST'], detail=False) -- Gitee