diff --git a/backend/artifacts/methods/mcp_methods.py b/backend/artifacts/methods/mcp_methods.py index ca28dbc4859184a1356eea60b4c062a0ddccc69d..600d5b067c42ed0cb1a0676cfeeeec1e2febb33e 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 6c546f37f3c410a571c519f34c04498f5c905783..0685c649eebbe53b6026f254428460854ecd40a3 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 65b912130dde2d00ab6ee3b074d71e9e82f858a1..d016abd78575177846c4405ab1f3f919318bbaa6 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)