From 282a42081b3b2eba379e8b56719c9372acd64de6 Mon Sep 17 00:00:00 2001 From: Falck Date: Sat, 18 Apr 2026 06:11:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E5=90=AF=E5=8A=A8=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E5=8A=9F=E8=83=BD=E4=B8=8E=E5=81=A5=E5=A3=AE=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- start.bat | 159 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 154 insertions(+), 5 deletions(-) diff --git a/start.bat b/start.bat index c82067d..ff086f7 100644 --- a/start.bat +++ b/start.bat @@ -9,6 +9,12 @@ setlocal enabledelayedexpansion cd /d "%~dp0" +:: ── 处理命令行参数 ── +if "%1"=="--help" goto :show_help +if "%1"=="-h" goto :show_help +if "%1"=="--version" goto :show_version +if "%1"=="-v" goto :show_version + :: ── 颜色代码 ── for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do ( set "DEL=%%a" @@ -33,6 +39,14 @@ call :colorEcho 0F " 开发者通用工具套组 · 一切皆为插件" call :colorEcho 07 " https://gitee.com/starlight-apk/feature-oss" echo. +:: ── 检查是否已有实例在运行 ── +call :check_pid +if %errorlevel% neq 0 ( + call :colorEcho 0C "[错误] 检测到已有实例在运行,请先停止" + pause + exit /b 1 +) + :: ═══════════════════════════════════════════════════════════ :: 1. 检测 Python :: ═══════════════════════════════════════════════════════════ @@ -58,6 +72,12 @@ if "%PYTHON_CMD%"=="" ( for /f "tokens=*" %%i in ('%PYTHON_CMD% --version 2^>^&1') do set "PY_VER=%%i" call :colorEcho 0A "[成功] %PY_VER%" +:: 显示系统信息 +call :colorEcho 0B "[信息] 系统信息:" +echo OS: Windows +echo 工作目录: %CD% +echo 时间: %date% %time% + :: ═══════════════════════════════════════════════════════════ :: 2. 虚拟环境 :: ═══════════════════════════════════════════════════════════ @@ -138,7 +158,12 @@ call :colorEcho 0B "[信息] 初始化数据目录..." set "DIRS=data data\html-render data\web-toolkit data\plugin-storage data\DCIM data\pkg data\signature-verifier\keys\private data\signature-verifier\keys\public logs" for %%d in (%DIRS%) do ( - if not exist "%%d" mkdir "%%d" + if not exist "%%d" ( + mkdir "%%d" >nul 2>&1 + if errorlevel 1 ( + call :colorEcho 0C "[错误] 无法创建目录: %%d" + ) + ) ) call :colorEcho 0A "[成功] 数据目录已就绪" @@ -152,6 +177,9 @@ call :colorEcho 0B " 启动 FutureOSS" call :colorEcho 0B "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo. +:: 创建 PID 文件 +call :create_pid "!random!" + if "%1"=="--daemon" goto :daemon_mode if "%1"=="-d" goto :daemon_mode @@ -161,6 +189,7 @@ echo. set "RESTART_DELAY=3" set "RESTART_COUNT=0" +set "MAX_RESTARTS=10" :loop %PYTHON_CMD% -m oss.cli serve @@ -171,13 +200,20 @@ if %EXIT_CODE% equ 0 ( goto :end ) +:: 检查是否超过最大重启次数 +if %RESTART_COUNT% geq %MAX_RESTARTS% ( + call :colorEcho 0C "[错误] 达到最大重启次数 (%MAX_RESTARTS%),停止服务" + goto :end +) + set /a RESTART_COUNT+=1 -call :colorEcho 0E "[警告] 服务异常退出 (code: %EXIT_CODE%),!RESTART_DELAY!s 后重启... (第 !RESTART_COUNT! 次)" +call :colorEcho 0E "[警告] 服务异常退出 (code: %EXIT_CODE%),!RESTART_DELAY!s 后重启... (第 !RESTART_COUNT!/%MAX_RESTARTS% 次)" timeout /t !RESTART_DELAY! /nobreak >nul -:: 指数退避 -if !RESTART_DELAY! lss 30 ( +:: 指数退避 (最大 60 秒) +if !RESTART_DELAY! lss 60 ( set /a RESTART_DELAY=!RESTART_DELAY! * 2 + if !RESTART_DELAY! gtr 60 set "RESTART_DELAY=60" ) goto :loop @@ -186,11 +222,17 @@ goto :loop call :colorEcho 0E "[警告] Windows 守护模式需要额外配置" call :colorEcho 07 "[提示] 建议使用任务计划程序或 nssm 工具实现" echo. -%PYTHON_CMD% -m oss.cli serve +call :colorEcho 0B "[信息] 启动后台服务..." +start /b %PYTHON_CMD% -m oss.cli serve > logs\daemon.log 2>&1 goto :end :end +call :cleanup call .venv\Scripts\deactivate.bat >nul 2>&1 +echo. +call :colorEcho 0B "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" +call :colorEcho 0F " FutureOSS 已停止" +call :colorEcho 0B "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" pause exit /b 0 @@ -213,5 +255,112 @@ call :colorText %params% "%msg%" exit /b 0 :colorText +nul 2>&1 +exit /b %errorlevel% + +:: ── 获取当前时间戳 ── +:get_timestamp +for /f "tokens=1-3 delims=/ " %%a in ('date /t') do set "T_DATE=%%c-%%a-%%b" +for /f "tokens=1-3 delims=: " %%a in ('time /t') do set "T_TIME=%%a:%%b:%%c" +set "TIMESTAMP=%T_DATE% %T_TIME%" +exit /b 0 + +:: ── 打印分隔线 ── +:print_separator +echo ═══════════════════════════════════════════════════════════ +exit /b 0 + +:: ── 打印带颜色的状态 ── +:print_status +set "status_type=%~1" +set "status_msg=%~2" +if "%status_type%"=="info" call :colorEcho 0B "[信息] %status_msg%" +if "%status_type%"=="success" call :colorEcho 0A "[成功] %status_msg%" +if "%status_type%"=="warn" call :colorEcho 0E "[警告] %status_msg%" +if "%status_type%"=="error" call :colorEcho 0C "[错误] %status_msg%" +exit /b 0 + +:: ── 健康检查 ── +:health_check +set "check_url=%~1" +set "max_retries=%~2" +if "%max_retries%"=="" set "max_retries=30" +set "retry_count=0" + +:health_loop +set /a retry_count+=1 +curl -s "%check_url%" >nul 2>&1 +if %errorlevel% equ 0 ( + call :colorEcho 0A "[健康检查] 服务已就绪" + exit /b 0 +) +if %retry_count% geq %max_retries% ( + call :colorEcho 0C "[健康检查] 服务启动超时" + exit /b 1 +) +timeout /t 1 /nobreak >nul +goto :health_loop + +:: ── 创建 PID 文件 ── +:create_pid +echo %~1 > .pid +exit /b 0 + +:: ── 删除 PID 文件 ── +:remove_pid +if exist ".pid" del ".pid" +exit /b 0 + +:: ── 检查 PID 文件 ── +:check_pid +if exist ".pid" ( + set /p PID=<.pid + tasklist /fi "pid eq %PID%" 2>nul | find "%PID%" >nul + if %errorlevel% equ 0 ( + call :colorEcho 0E "[警告] 服务已在运行 (PID: %PID%)" + exit /b 1 + ) else ( + call :remove_pid + ) +) +exit /b 0 + +:: ── 显示帮助信息 ── +:show_help +echo. +call :colorEcho 0F "FutureOSS 启动脚本 - Windows 版本" +echo. +echo 用法: start.bat [选项] +echo. +echo 选项: +echo --daemon, -d 以后台模式运行(Windows 建议使用任务计划程序) +echo --help, -h 显示此帮助信息 +echo --version, -v 显示版本信息 +echo. +echo 示例: +echo start.bat 前台运行模式 +echo start.bat --daemon 后台运行模式 +echo. +exit /b 0 + +:: ── 显示版本信息 ── +:show_version +echo FutureOSS v1.0.0 +echo 基于 Python 的开发者通用工具套组 +echo. +exit /b 0 + +:: ── 清理函数 ── +:cleanup +call :colorEcho 0E "[信息] 正在清理..." +call :remove_pid +call :colorEcho 0A "[成功] 清理完成" exit /b 0 \ No newline at end of file