mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
✨ 添加重启脚本命令
This commit is contained in:
parent
033792c812
commit
82a3527e2e
44
win启动.bat
Normal file
44
win启动.bat
Normal file
@ -0,0 +1,44 @@
|
||||
|
||||
|
||||
|
||||
:: 检查是否传入了端口号
|
||||
if "%~1"=="" (
|
||||
echo 未传入端口号,跳过杀死进程操作。
|
||||
goto END
|
||||
)
|
||||
|
||||
:: 获取传入的端口号
|
||||
set PORT=%~1
|
||||
|
||||
:: 查找并杀死所有占用端口的进程
|
||||
for /f "tokens=5" %%a in ('netstat -ano ^| findstr /r /c:"^ *[^ ]*:%PORT% " /c:"^ *[^ ]*:%PORT%$"') do (
|
||||
echo 正在杀死占用端口 %PORT% 的进程,PID: %%a
|
||||
taskkill /PID %%a /F
|
||||
)
|
||||
|
||||
:END
|
||||
|
||||
:: 获取当前批处理文件所在的目录
|
||||
set BAT_DIR=%~dp0
|
||||
|
||||
:: 设置 Python 可执行文件的完整路径
|
||||
set PYTHON_EXE="%BAT_DIR%Python311\python.exe"
|
||||
|
||||
:: 检查 python.exe 是否存在
|
||||
if not exist %PYTHON_EXE% (
|
||||
echo 错误:未找到 Python 可执行文件!
|
||||
echo 请确保 Python311 文件夹位于批处理文件所在目录中。
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
:: 使用指定的 Python 安装 Playwright 的 Chromium
|
||||
echo start install Playwright 的 Chromium...
|
||||
%PYTHON_EXE% -m playwright install chromium
|
||||
|
||||
:: 使用当前目录中的 python.exe 运行 bot.py
|
||||
echo 正在启动 bot.py...
|
||||
%PYTHON_EXE% bot.py
|
||||
|
||||
:: 如果脚本运行完毕,暂停以便查看输出
|
||||
pause
|
||||
@ -1,3 +1,5 @@
|
||||
import asyncio
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
@ -22,8 +24,6 @@ port = driver.config.port
|
||||
|
||||
FILE_NAME = ".configure_restart"
|
||||
|
||||
flag_file: Path | None = None
|
||||
|
||||
|
||||
@router.post(
|
||||
"/set_configure",
|
||||
@ -57,7 +57,7 @@ async def _(setting: Setting) -> Result:
|
||||
Config.set_config("web-ui", "username", setting.username)
|
||||
Config.set_config("web-ui", "password", setting.password, True)
|
||||
env_file.write_text(env_text, encoding="utf-8")
|
||||
flag_file = Path() / f"{FILE_NAME}_{time.time()}"
|
||||
flag_file = Path() / f"{FILE_NAME}_{int(time.time())}"
|
||||
flag_file.touch()
|
||||
return Result.ok(info="设置成功,请重启真寻以完成配置!")
|
||||
|
||||
@ -75,6 +75,13 @@ async def _(db_url: str) -> Result:
|
||||
return Result.ok(info="数据库连接成功!")
|
||||
|
||||
|
||||
async def run_restart_command(bat_path: Path, port: int):
|
||||
"""在后台执行重启命令"""
|
||||
await asyncio.sleep(1) # 确保 FastAPI 已返回响应
|
||||
subprocess.Popen([bat_path, str(port)], shell=True) # noqa: ASYNC220
|
||||
sys.exit(0) # 退出当前进程
|
||||
|
||||
|
||||
@router.post(
|
||||
"/restart",
|
||||
response_model=Result,
|
||||
@ -82,7 +89,10 @@ async def _(db_url: str) -> Result:
|
||||
description="重启",
|
||||
)
|
||||
async def _() -> Result:
|
||||
global flag_file
|
||||
flag_file = None
|
||||
for file in os.listdir(Path()):
|
||||
if file.startswith(FILE_NAME):
|
||||
flag_file = Path() / file
|
||||
if not flag_file or not flag_file.exists():
|
||||
return Result.fail("重启标志文件不存在...")
|
||||
set_time = flag_file.name.split("_")[-1]
|
||||
@ -90,7 +100,7 @@ async def _() -> Result:
|
||||
return Result.fail("重启标志文件已过期,请重新设置配置。")
|
||||
flag_file.unlink()
|
||||
bat_path = Path() / "win启动.bat"
|
||||
subprocess.Popen([bat_path, str(port)], shell=True) # noqa: ASYNC220
|
||||
|
||||
# 退出当前进程
|
||||
sys.exit(0)
|
||||
try:
|
||||
return Result.ok(info="执行重启命令成功")
|
||||
finally:
|
||||
asyncio.create_task(run_restart_command(bat_path, port)) # noqa: RUF006
|
||||
|
||||
Loading…
Reference in New Issue
Block a user