From 2ea705bde552849ea8cfb76bd62ecc013479c0ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E9=B8=BF=E5=AE=87?= Date: Thu, 27 Feb 2025 16:35:50 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(service):=20=E4=BC=98=E5=8C=96=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E6=8F=92=E5=85=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史鸿宇 --- apps/scheduler/pool/loader/service.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/apps/scheduler/pool/loader/service.py b/apps/scheduler/pool/loader/service.py index 2386afbe1..cf8714c24 100644 --- a/apps/scheduler/pool/loader/service.py +++ b/apps/scheduler/pool/loader/service.py @@ -57,7 +57,6 @@ class ServiceLoader: nodes = [NodePool(**node.model_dump(exclude_none=True, by_alias=True)) for node in nodes] await self._update_db(nodes, metadata) - async def save(self, service_id: str, metadata: ServiceMetadata, data: dict) -> None: """在文件系统上保存Service,并更新数据库""" service_path = Path(config["SEMANTICS_DIR"]) / SERVICE_DIR / service_id @@ -78,7 +77,6 @@ class ServiceLoader: await file_checker.diff_one(service_path) await self.load(service_id, file_checker.hashes[f"{SERVICE_DIR}/{service_id}"]) - async def delete(self, service_id: str) -> None: """删除Service,并更新数据库""" service_collection = MongoDB.get_collection("service") @@ -131,7 +129,7 @@ class ServiceLoader: upsert=True, ) for node in nodes: - await node_collection.insert_one(jsonable_encoder(node)) + await node_collection.update_one({"_id": node.id}, {"$set": jsonable_encoder(node)}, upsert=True) except Exception as e: err = f"[ServiceLoader] 更新 MongoDB 失败:{e}" LOGGER.error(err) -- Gitee From 33b78370084b8b1f528045fd1cb2b9cabdef5dce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E9=B8=BF=E5=AE=87?= Date: Thu, 27 Feb 2025 17:57:12 +0800 Subject: [PATCH 2/2] =?UTF-8?q?refactor(app):=20=E4=BC=98=E5=8C=96=20loggi?= =?UTF-8?q?ng?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 史鸿宇 --- apps/manager/appcenter.py | 10 ++--- apps/routers/appcenter.py | 77 ++++++++++++++++++++------------------- 2 files changed, 42 insertions(+), 45 deletions(-) diff --git a/apps/manager/appcenter.py b/apps/manager/appcenter.py index 5c1e8e2ee..29c45f5bf 100644 --- a/apps/manager/appcenter.py +++ b/apps/manager/appcenter.py @@ -209,13 +209,9 @@ class AppCenterManager: users=data.permission.users or [], ), ) - try: - app_loader = AppLoader() - await app_loader.save(metadata, app_id) - return app_id - except Exception: - logger.exception("[AppCenterManager] 创建应用失败") - return "" + app_loader = AppLoader() + await app_loader.save(metadata, app_id) + return app_id @staticmethod async def update_app(user_sub: str, app_id: str, data: AppData) -> None: diff --git a/apps/routers/appcenter.py b/apps/routers/appcenter.py index 297ded166..b4c21a815 100644 --- a/apps/routers/appcenter.py +++ b/apps/routers/appcenter.py @@ -3,12 +3,12 @@ Copyright (c) Huawei Technologies Co., Ltd. 2024-2025. All rights reserved. """ +import logging from typing import Annotated, Optional, Union from fastapi import APIRouter, Body, Depends, Path, Query, status from fastapi.responses import JSONResponse -from apps.constants import LOGGER from apps.dependency.csrf import verify_csrf_token from apps.dependency.user import get_user, verify_user from apps.entities.appcenter import AppFlowInfo, AppPermissionData @@ -28,6 +28,7 @@ from apps.entities.response_data import ( ) from apps.manager.appcenter import AppCenterManager +logger = logging.getLogger("ray") router = APIRouter( prefix="/api/app", tags=["appcenter"], @@ -103,28 +104,28 @@ async def create_or_update_application( if app_id: # 更新应用 try: await AppCenterManager.update_app(user_sub, app_id, request) - except ValueError as e: - LOGGER.error(msg=f"[AppCenter] 更新应用请求无效:{e!s}") + except ValueError: + logger.exception("[AppCenter] 更新应用请求无效") return JSONResponse( status_code=status.HTTP_400_BAD_REQUEST, content=ResponseData( code=status.HTTP_400_BAD_REQUEST, - message=str(e), + message="BAD_REQUEST", result={}, ).model_dump(exclude_none=True, by_alias=True), ) - except PermissionError as e: - LOGGER.error(msg=f"[AppCenter] 更新应用鉴权失败:{e!s}") + except PermissionError: + logger.exception("[AppCenter] 更新应用鉴权失败") return JSONResponse( status_code=status.HTTP_403_FORBIDDEN, content=ResponseData( code=status.HTTP_403_FORBIDDEN, - message=str(e), + message="UNAUTHORIZED", result={}, ).model_dump(exclude_none=True, by_alias=True), ) - except Exception as e: - LOGGER.error(msg=f"[AppCenter] 更新应用失败:{e}") + except Exception: + logger.exception("[AppCenter] 更新应用失败") return JSONResponse( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content=ResponseData( @@ -136,8 +137,8 @@ async def create_or_update_application( else: # 创建应用 try: app_id = await AppCenterManager.create_app(user_sub, request) - except Exception as e: - LOGGER.error(msg=f"[AppCenter] 创建应用失败:{e}") + except Exception: + logger.exception("[AppCenter] 创建应用失败") return JSONResponse( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content=ResponseData( @@ -164,8 +165,8 @@ async def get_recently_used_applications( """获取最近使用的应用""" try: recent_apps = await AppCenterManager.get_recently_used_apps(count, user_sub) - except Exception as e: - LOGGER.error(msg=f"[AppCenter] 获取最近使用的应用失败:{e}") + except Exception: + logger.exception("[AppCenter] 获取最近使用的应用失败") return JSONResponse( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content=ResponseData( @@ -191,8 +192,8 @@ async def get_application( """获取应用详情""" try: app_data = await AppCenterManager.fetch_app_data_by_id(app_id) - except ValueError as e: - LOGGER.error(msg=f"[AppCenter] 获取应用详情请求无效:{e!s}") + except ValueError: + logger.exception("[AppCenter] 获取应用详情请求无效") return JSONResponse( status_code=status.HTTP_400_BAD_REQUEST, content=ResponseData( @@ -201,8 +202,8 @@ async def get_application( result={}, ).model_dump(exclude_none=True, by_alias=True), ) - except Exception as e: - LOGGER.error(msg=f"[AppCenter] 获取应用详情失败:{e}") + except Exception: + logger.exception("[AppCenter] 获取应用详情失败") return JSONResponse( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content=ResponseData( @@ -256,28 +257,28 @@ async def delete_application( """删除应用""" try: await AppCenterManager.delete_app(app_id, user_sub) - except ValueError as e: - LOGGER.error(msg=f"[AppCenter] 删除应用请求无效:{e!s}") + except ValueError: + logger.exception("[AppCenter] 删除应用请求无效") return JSONResponse( status_code=status.HTTP_400_BAD_REQUEST, content=ResponseData( code=status.HTTP_400_BAD_REQUEST, - message=str(e), + message="INVALID_APP_ID", result={}, ).model_dump(exclude_none=True, by_alias=True), ) - except PermissionError as e: - LOGGER.error(msg=f"[AppCenter] 删除应用鉴权失败:{e!s}") + except PermissionError: + logger.exception("[AppCenter] 删除应用鉴权失败") return JSONResponse( status_code=status.HTTP_403_FORBIDDEN, content=ResponseData( code=status.HTTP_403_FORBIDDEN, - message=str(e), + message="UNAUTHORIZED", result={}, ).model_dump(exclude_none=True, by_alias=True), ) - except Exception as e: - LOGGER.error(msg=f"[AppCenter] 删除应用失败:{e}") + except Exception: + logger.exception("[AppCenter] 删除应用失败") return JSONResponse( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content=ResponseData( @@ -304,28 +305,28 @@ async def publish_application( """发布应用""" try: await AppCenterManager.publish_app(app_id, user_sub) - except ValueError as e: - LOGGER.error(msg=f"[AppCenter] 发布应用请求无效:{e!s}") + except ValueError: + logger.exception("[AppCenter] 发布应用请求无效") return JSONResponse( status_code=status.HTTP_400_BAD_REQUEST, content=ResponseData( code=status.HTTP_400_BAD_REQUEST, - message=str(e), + message="BAD_REQUEST", result={}, ).model_dump(exclude_none=True, by_alias=True), ) - except PermissionError as e: - LOGGER.error(msg=f"[AppCenter] 发布应用鉴权失败:{e!s}") + except PermissionError: + logger.exception("[AppCenter] 发布应用鉴权失败") return JSONResponse( status_code=status.HTTP_403_FORBIDDEN, content=ResponseData( code=status.HTTP_403_FORBIDDEN, - message=str(e), + message="UNAUTHORIZED", result={}, ).model_dump(exclude_none=True, by_alias=True), ) - except Exception as e: - LOGGER.error(msg=f"[AppCenter] 发布应用失败:{e}") + except Exception: + logger.exception("[AppCenter] 发布应用失败") return JSONResponse( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content=ResponseData( @@ -353,18 +354,18 @@ async def modify_favorite_application( """更改应用收藏状态""" try: await AppCenterManager.modify_favorite_app(app_id, user_sub, favorited=request.favorited) - except ValueError as e: - LOGGER.error(msg=f"[AppCenter] 修改收藏状态请求无效:{e!s}") + except ValueError: + logger.exception("[AppCenter] 修改收藏状态请求无效") return JSONResponse( status_code=status.HTTP_400_BAD_REQUEST, content=ResponseData( code=status.HTTP_400_BAD_REQUEST, - message=str(e), + message="BAD_REQUEST", result={}, ).model_dump(exclude_none=True, by_alias=True), ) - except Exception as e: - LOGGER.error(msg=f"[AppCenter] 修改收藏状态失败:{e}") + except Exception: + logger.exception("[AppCenter] 修改收藏状态失败") return JSONResponse( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content=ResponseData( -- Gitee