From 3db4c75fe7903493d65850f27b7603f55aa63177 Mon Sep 17 00:00:00 2001 From: W1NDes Date: Tue, 3 Mar 2026 15:19:17 +0800 Subject: [PATCH] =?UTF-8?q?Feat(api):=20=E6=B7=BB=E5=8A=A0alas=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E9=87=8D=E5=90=AF=E7=9A=84api=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module/webui/api.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/module/webui/api.py b/module/webui/api.py index 298a11dd3..1840cd56e 100644 --- a/module/webui/api.py +++ b/module/webui/api.py @@ -13,6 +13,7 @@ from starlette.middleware import Middleware from starlette.middleware.base import BaseHTTPMiddleware from module.webui.process_manager import ProcessManager +from module.webui.setting import State from module.config.utils import alas_instance from module.submodule.utils import get_config_mod from module.logger import logger @@ -327,6 +328,30 @@ async def batch_instance_status(request: Request): logger.error(f"Batch get status error: {e}") return create_response(False, f"批量获取状态失败: {str(e)}") +async def restart_alas(request: Request): + """重启整套Alas脚本""" + try: + if State.restart_event is None: + return create_response(False, "重启功能不可用:请在部署设置中启用 EnableReload") + + logger.info("API: Restarting Alas") + + def _do_restart(): + from module.webui.app import clearup + clearup() + State.restart_event.set() + + # 使用BackgroundTask确保响应先发送,再执行重启 + from starlette.background import BackgroundTask + return JSONResponse( + {"success": True, "message": "Alas正在重启"}, + background=BackgroundTask(_do_restart) + ) + + except Exception as e: + logger.error(f"Restart Alas error: {e}") + return create_response(False, f"重启失败: {str(e)}") + # 定义路由 api_routes = [ Route("/api/instances", list_instances, methods=["GET"]), @@ -336,6 +361,7 @@ api_routes = [ Route("/api/instances/batch-start", batch_start_instances, methods=["POST"]), Route("/api/instances/batch-stop", batch_stop_instances, methods=["POST"]), Route("/api/instances/batch-status", batch_instance_status, methods=["POST"]), + Route("/api/restart", restart_alas, methods=["POST"]), ] # 创建API应用,添加本地访问限制中间件