mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-04 11:20:01 +08:00
添加uv支持 (#2119)
* bugfix:修复内存泄露和信号量饥饿问题 * 修复图片渲染按高度截断问题 * 优化图片渲染速度 * 权限检查去掉无效引用代码 * 添加uv支持 * 🚨 auto fix by pre-commit hooks * bugfix:修改gitignore换行 * bugfix:修复测试没有新生成uv.lock * 修复导入错误 * bugfix:移除重复调用 * 🚨 auto fix by pre-commit hooks * 清理残余poetry引用 * 更新uv安装方式 * 修复阿里云获取问题 * 增加资源下载提示 * 🚨 auto fix by pre-commit hooks * 修改资源下载为流式 * 🚨 auto fix by pre-commit hooks * 提高启动速度 * 移除bot.py支持 * 🚨 auto fix by pre-commit hooks * 优化win脚本逻辑 * 🚨 auto fix by pre-commit hooks * 清理残余无效逻辑 * 代码改进 * 🚨 auto fix by pre-commit hooks * 增加数据库迁移存在性检查 * 🚨 auto fix by pre-commit hooks * chore(test): 添加pytest超时控制和优雅关闭机制 - 在GitHub Actions工作流中添加作业级和步骤级超时限制,防止测试无限期挂起 - 添加pytest-timeout依赖并配置全局超时为120秒 - 在send_queue服务添加关闭钩子,确保worker任务正确取消 - 在priority_manager添加on_shutdown钩子,支持优先级生命周期的关闭阶段 * chore(lint): 禁用超长行的lint警告 * Modify restart logic for Windows platform * 🚨 auto fix by pre-commit hooks * bugfix:修复sys导入问题 * 清理无效结构 * bugfix:修复路径问题 * bugfix:修复shell语法传递给git导致资源获取失败问题 * 优化关闭显示 * bugfix:修复路径问题 * bugfix:增加路径安全 * bugfix:修复orm绕过问题 * 放宽numpy版本限制 * 修改重启方案 * bugfix:修复循环导入 * 优化逻辑 * Enhance disconnect function with error handling Added error handling for disconnect function and imported ConfigurationError. * Implement emergency restart mechanism Added emergency restart mechanism using atexit to ensure process restart even on severe exceptions during shutdown. * 🚨 auto fix by pre-commit hooks * 重启行为归一化 * 修复测试检测问题 * bugfix:修复测试侧类型报错问题 * 引入launcher机制 * 移除重启测试 * 收紧缓存调用路径 * 类型注解收敛 * 优化浏览器回收行为 * 优化浏览器渲染 * bugfix:解决重复关闭浏览器问题 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: ManyManyTomato <93612024+ATTomatoo@users.noreply.github.com> Co-authored-by: AkashiCoin <l1040186796@gmail.com>
This commit is contained in:
co-authored by
pre-commit-ci[bot]
ManyManyTomato
AkashiCoin
parent
74bf912d04
commit
8b16126e40
@@ -0,0 +1,229 @@
|
||||
import _thread
|
||||
import asyncio
|
||||
import copy
|
||||
import json
|
||||
from pathlib import Path
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
from nonebot.adapters import Bot
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
||||
|
||||
_RESTART_STATE_FILE = Path() / "data" / ".restart_state.json"
|
||||
_LEGACY_RESTART_MARK = Path() / "is_restart"
|
||||
_LEGACY_RESTART_SCRIPT = Path() / "restart.sh"
|
||||
_LEGACY_CONFIGURE_RESTART_PREFIX = ".configure_restart"
|
||||
_RESTART_TICKET_KEY = "restart_ticket"
|
||||
_PENDING_REQUEST_KEY = "pending_request"
|
||||
_LAUNCHER_ACTION_KEY = "launcher_action"
|
||||
_ACTION_RESTART = "restart"
|
||||
|
||||
_restart_pending: bool = False
|
||||
|
||||
|
||||
def _ensure_state_parent() -> None:
|
||||
_RESTART_STATE_FILE.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
def _read_restart_state() -> dict[str, Any]:
|
||||
if not _RESTART_STATE_FILE.exists():
|
||||
return {}
|
||||
try:
|
||||
data = json.loads(_RESTART_STATE_FILE.read_text(encoding="utf-8"))
|
||||
except Exception as e:
|
||||
logger.warning(f"读取重启状态文件失败,已忽略旧状态: {e}", "重启")
|
||||
return {}
|
||||
return data if isinstance(data, dict) else {}
|
||||
|
||||
|
||||
def _write_restart_state(state: dict[str, Any]) -> None:
|
||||
if not state:
|
||||
if _RESTART_STATE_FILE.exists():
|
||||
_RESTART_STATE_FILE.unlink()
|
||||
return
|
||||
_ensure_state_parent()
|
||||
temp_file = _RESTART_STATE_FILE.with_name(f"{_RESTART_STATE_FILE.name}.tmp")
|
||||
temp_file.write_text(
|
||||
json.dumps(state, ensure_ascii=False, indent=2),
|
||||
encoding="utf-8",
|
||||
)
|
||||
temp_file.replace(_RESTART_STATE_FILE)
|
||||
|
||||
|
||||
def _cleanup_legacy_restart_artifacts() -> None:
|
||||
legacy_paths = [_LEGACY_RESTART_MARK, _LEGACY_RESTART_SCRIPT]
|
||||
legacy_paths.extend(Path().glob(f"{_LEGACY_CONFIGURE_RESTART_PREFIX}*"))
|
||||
for path in legacy_paths:
|
||||
if not path.exists():
|
||||
continue
|
||||
try:
|
||||
path.unlink()
|
||||
logger.info(f"已清理旧重启遗留文件: {path.name}", "重启")
|
||||
except Exception as e:
|
||||
logger.warning(f"清理旧重启遗留文件失败: {path.name} | {e}", "重启")
|
||||
|
||||
|
||||
def issue_restart_ticket(source: str, *, ttl_seconds: int = 600) -> None:
|
||||
now = time.time()
|
||||
state = _read_restart_state()
|
||||
state[_RESTART_TICKET_KEY] = {
|
||||
"source": source,
|
||||
"issued_at": now,
|
||||
"expires_at": now + ttl_seconds,
|
||||
}
|
||||
_write_restart_state(state)
|
||||
logger.info(f"已记录重启授权,来源: {source}", "重启")
|
||||
|
||||
|
||||
def _validate_restart_ticket(
|
||||
state: dict[str, Any],
|
||||
expected_source: str,
|
||||
) -> tuple[bool, str]:
|
||||
ticket = state.get(_RESTART_TICKET_KEY)
|
||||
if not isinstance(ticket, dict):
|
||||
return False, "重启标志不存在..."
|
||||
if ticket.get("source") != expected_source:
|
||||
return False, "重启标志来源不匹配,请重新发起操作。"
|
||||
expires_at = float(ticket.get("expires_at", 0))
|
||||
if time.time() > expires_at:
|
||||
state.pop(_RESTART_TICKET_KEY, None)
|
||||
_write_restart_state(state)
|
||||
return False, "重启标志已过期,请重新设置配置。"
|
||||
return True, ""
|
||||
|
||||
|
||||
async def _schedule_restart() -> tuple[bool, str]:
|
||||
global _restart_pending
|
||||
if _restart_pending:
|
||||
logger.warning("重启已在进行中,忽略重复请求。", "重启")
|
||||
return False, "重启已在进行中,请稍后查看结果。"
|
||||
_restart_pending = True
|
||||
logger.info("已标记重启请求,等待 launcher 接管下一代 worker...", "重启")
|
||||
|
||||
async def _send_sigint() -> None:
|
||||
await asyncio.sleep(0.3)
|
||||
logger.info("发送重启信号...", "重启")
|
||||
_thread.interrupt_main()
|
||||
|
||||
asyncio.create_task(_send_sigint()) # noqa: RUF006
|
||||
return True, "执行重启命令成功"
|
||||
|
||||
|
||||
async def request_restart(
|
||||
source: str,
|
||||
*,
|
||||
receipt_bot_id: str | None = None,
|
||||
receipt_user_id: str | None = None,
|
||||
require_ticket: str | None = None,
|
||||
) -> tuple[bool, str]:
|
||||
state = _read_restart_state()
|
||||
previous_state = copy.deepcopy(state)
|
||||
if require_ticket:
|
||||
ok, message = _validate_restart_ticket(state, require_ticket)
|
||||
if not ok:
|
||||
return False, message
|
||||
|
||||
pending_request: dict[str, Any] = {
|
||||
"source": source,
|
||||
"requested_at": time.time(),
|
||||
}
|
||||
if receipt_bot_id and receipt_user_id:
|
||||
pending_request["receipt"] = {
|
||||
"bot_id": receipt_bot_id,
|
||||
"user_id": receipt_user_id,
|
||||
}
|
||||
state[_PENDING_REQUEST_KEY] = pending_request
|
||||
state[_LAUNCHER_ACTION_KEY] = _ACTION_RESTART
|
||||
if require_ticket:
|
||||
state.pop(_RESTART_TICKET_KEY, None)
|
||||
|
||||
try:
|
||||
_write_restart_state(state)
|
||||
except Exception as e:
|
||||
logger.error(f"写入重启状态失败: {e}", "重启")
|
||||
return False, "写入重启状态失败。"
|
||||
|
||||
ok, message = await _schedule_restart()
|
||||
if not ok:
|
||||
try:
|
||||
_write_restart_state(previous_state)
|
||||
except Exception as e:
|
||||
logger.warning(f"回滚重启状态失败: {e}", "重启")
|
||||
return False, message
|
||||
|
||||
logger.info(f"收到重启请求,来源: {source}", "重启")
|
||||
return True, message
|
||||
|
||||
|
||||
async def handle_restart_connect(bot: Bot) -> None:
|
||||
state = _read_restart_state()
|
||||
pending_request = state.get(_PENDING_REQUEST_KEY)
|
||||
if not isinstance(pending_request, dict):
|
||||
return
|
||||
|
||||
source = str(pending_request.get("source", "unknown"))
|
||||
receipt = pending_request.get("receipt")
|
||||
if not isinstance(receipt, dict):
|
||||
logger.info(f"检测到重启完成,来源: {source}", "重启")
|
||||
state.pop(_PENDING_REQUEST_KEY, None)
|
||||
_write_restart_state(state)
|
||||
return
|
||||
|
||||
expected_bot_id = str(receipt.get("bot_id", ""))
|
||||
receipt_user_id = str(receipt.get("user_id", ""))
|
||||
if expected_bot_id and expected_bot_id != str(bot.self_id):
|
||||
logger.debug(
|
||||
f"重启回执等待目标 Bot 连接: source={source} bot={expected_bot_id}"
|
||||
)
|
||||
return
|
||||
|
||||
logger.info(f"检测到重启完成,来源: {source}", "重启")
|
||||
|
||||
from zhenxun.configs.config import BotConfig
|
||||
from zhenxun.utils.message import MessageUtils
|
||||
from zhenxun.utils.platform import PlatformUtils
|
||||
|
||||
target = PlatformUtils.get_target(user_id=receipt_user_id)
|
||||
if target:
|
||||
try:
|
||||
await MessageUtils.build_message(
|
||||
f"{BotConfig.self_nickname}已成功重启!"
|
||||
).send(target, bot=bot)
|
||||
except Exception as e:
|
||||
logger.warning(f"发送重启回执失败: {e}", "重启")
|
||||
else:
|
||||
logger.warning("未找到重启回执目标,已跳过发送。", "重启")
|
||||
|
||||
state.pop(_PENDING_REQUEST_KEY, None)
|
||||
_write_restart_state(state)
|
||||
|
||||
|
||||
def _finalize_restart_state_on_startup() -> None:
|
||||
state = _read_restart_state()
|
||||
pending_request = state.get(_PENDING_REQUEST_KEY)
|
||||
if not isinstance(pending_request, dict):
|
||||
return
|
||||
|
||||
source = str(pending_request.get("source", "unknown"))
|
||||
receipt = pending_request.get("receipt")
|
||||
if isinstance(receipt, dict):
|
||||
logger.info(f"检测到待发送的重启回执,来源: {source}", "重启")
|
||||
return
|
||||
|
||||
logger.info(f"检测到重启完成,来源: {source}", "重启")
|
||||
state.pop(_PENDING_REQUEST_KEY, None)
|
||||
_write_restart_state(state)
|
||||
|
||||
|
||||
@PriorityLifecycle.on_startup(priority=0)
|
||||
async def _cleanup_restart_artifacts() -> None:
|
||||
_cleanup_legacy_restart_artifacts()
|
||||
_finalize_restart_state_on_startup()
|
||||
|
||||
|
||||
@PriorityLifecycle.on_shutdown(priority=99)
|
||||
async def _notify_restart_shutdown() -> None:
|
||||
if _restart_pending:
|
||||
logger.info("launcher 将在当前 worker 退出后接管重启。", "重启")
|
||||
@@ -32,7 +32,7 @@ class NotFoundError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class GroupInfoNotFound(Exception):
|
||||
class GroupConsoleNotFound(Exception):
|
||||
"""
|
||||
群组未找到
|
||||
"""
|
||||
|
||||
@@ -5,7 +5,6 @@ from pathlib import Path
|
||||
import random
|
||||
import re
|
||||
|
||||
import imagehash
|
||||
from nonebot.utils import is_coroutine_callable, run_sync
|
||||
from PIL import Image
|
||||
|
||||
@@ -355,6 +354,15 @@ def get_img_hash(image_file: str | Path) -> str:
|
||||
返回:
|
||||
str: 哈希值
|
||||
"""
|
||||
try:
|
||||
import imagehash
|
||||
except ImportError:
|
||||
logger.warning(
|
||||
"imagehash 未安装或其依赖(numpy/scipy/PyWavelets)不可用,"
|
||||
"图片哈希功能不可用",
|
||||
"禁言检测",
|
||||
)
|
||||
return ""
|
||||
hash_value = ""
|
||||
try:
|
||||
with open(image_file, "rb") as fp:
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
from collections.abc import Callable
|
||||
from typing import ClassVar
|
||||
|
||||
@@ -39,6 +40,14 @@ class PriorityLifecycle:
|
||||
return wrapper
|
||||
|
||||
|
||||
async def _run_hook(func: Callable, priority: int, hook_type: str = "startup") -> None:
|
||||
logger.debug(f"执行优先级 [{priority}] on_{hook_type} 方法: {func.__module__}")
|
||||
if is_coroutine_callable(func):
|
||||
await func()
|
||||
else:
|
||||
func()
|
||||
|
||||
|
||||
@driver.on_startup
|
||||
async def _():
|
||||
priority_data = PriorityLifecycle._data.get(PriorityLifecycleType.STARTUP)
|
||||
@@ -48,13 +57,25 @@ async def _():
|
||||
priority = 0
|
||||
try:
|
||||
for priority in priority_list:
|
||||
for func in priority_data[priority]:
|
||||
logger.debug(
|
||||
f"执行优先级 [{priority}] on_startup 方法: {func.__module__}"
|
||||
)
|
||||
if is_coroutine_callable(func):
|
||||
await func()
|
||||
else:
|
||||
func()
|
||||
funcs = priority_data[priority]
|
||||
if len(funcs) == 1:
|
||||
await _run_hook(funcs[0], priority)
|
||||
else:
|
||||
await asyncio.gather(*[_run_hook(f, priority) for f in funcs])
|
||||
except HookPriorityException as e:
|
||||
logger.error(f"打断优先级 [{priority}] on_startup 方法. {type(e)}: {e}")
|
||||
|
||||
|
||||
@driver.on_shutdown
|
||||
async def _():
|
||||
priority_data = PriorityLifecycle._data.get(PriorityLifecycleType.SHUTDOWN)
|
||||
if not priority_data:
|
||||
return
|
||||
priority_list = sorted(priority_data.keys())
|
||||
for priority in priority_list:
|
||||
funcs = priority_data[priority]
|
||||
for func in funcs:
|
||||
try:
|
||||
await _run_hook(func, priority, "shutdown")
|
||||
except Exception as e:
|
||||
logger.error(f"执行优先级 [{priority}] on_shutdown 方法出错: {e}")
|
||||
|
||||
@@ -7,34 +7,24 @@ from typing import ClassVar
|
||||
from zhenxun.configs.config import Config
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
BAT_FILE = Path() / "win启动.bat"
|
||||
|
||||
LOG_COMMAND = "VirtualEnvPackageManager"
|
||||
|
||||
Config.add_plugin_config(
|
||||
"virtualenv",
|
||||
"python_path",
|
||||
None,
|
||||
help="虚拟环境python路径,为空时使用系统环境的poetry",
|
||||
help="虚拟环境python路径,为空时使用系统环境的uv",
|
||||
)
|
||||
|
||||
|
||||
class VirtualEnvPackageManager:
|
||||
WIN_COMMAND: ClassVar[list[str]] = [
|
||||
"./Python310/python.exe",
|
||||
"-m",
|
||||
"pip",
|
||||
]
|
||||
|
||||
DEFAULT_COMMAND: ClassVar[list[str]] = ["poetry", "run", "pip"]
|
||||
DEFAULT_COMMAND: ClassVar[list[str]] = ["uv", "pip"]
|
||||
|
||||
@classmethod
|
||||
def __get_command(cls) -> list[str]:
|
||||
if path := Config.get_config("virtualenv", "python_path"):
|
||||
return [path, "-m", "pip"]
|
||||
return (
|
||||
cls.WIN_COMMAND.copy() if BAT_FILE.exists() else cls.DEFAULT_COMMAND.copy()
|
||||
)
|
||||
return cls.DEFAULT_COMMAND.copy()
|
||||
|
||||
@classmethod
|
||||
async def install(cls, package: list[str] | str):
|
||||
@@ -48,7 +38,7 @@ class VirtualEnvPackageManager:
|
||||
try:
|
||||
command = cls.__get_command()
|
||||
command.append("install")
|
||||
command.append(" ".join(package))
|
||||
command.extend(package)
|
||||
logger.info(f"执行虚拟环境安装包指令: {command}", LOG_COMMAND)
|
||||
result = await asyncio.to_thread(
|
||||
subprocess.run,
|
||||
@@ -62,9 +52,10 @@ class VirtualEnvPackageManager:
|
||||
LOG_COMMAND,
|
||||
)
|
||||
return result.stdout
|
||||
except CalledProcessError as e:
|
||||
logger.error(f"安装虚拟环境包指令执行失败: {e.stderr}.", LOG_COMMAND)
|
||||
return e.stderr
|
||||
except (CalledProcessError, FileNotFoundError) as e:
|
||||
stderr = e.stderr if isinstance(e, CalledProcessError) else str(e)
|
||||
logger.error(f"安装虚拟环境包指令执行失败: {stderr}.", LOG_COMMAND)
|
||||
return stderr
|
||||
|
||||
@classmethod
|
||||
async def uninstall(cls, package: list[str] | str):
|
||||
@@ -78,8 +69,7 @@ class VirtualEnvPackageManager:
|
||||
try:
|
||||
command = cls.__get_command()
|
||||
command.append("uninstall")
|
||||
command.append("-y")
|
||||
command.append(" ".join(package))
|
||||
command.extend(package)
|
||||
logger.info(f"执行虚拟环境卸载包指令: {command}", LOG_COMMAND)
|
||||
result = await asyncio.to_thread(
|
||||
subprocess.run,
|
||||
@@ -93,9 +83,10 @@ class VirtualEnvPackageManager:
|
||||
LOG_COMMAND,
|
||||
)
|
||||
return result.stdout
|
||||
except CalledProcessError as e:
|
||||
logger.error(f"卸载虚拟环境包指令执行失败: {e.stderr}.", LOG_COMMAND)
|
||||
return e.stderr
|
||||
except (CalledProcessError, FileNotFoundError) as e:
|
||||
stderr = e.stderr if isinstance(e, CalledProcessError) else str(e)
|
||||
logger.error(f"卸载虚拟环境包指令执行失败: {stderr}.", LOG_COMMAND)
|
||||
return stderr
|
||||
|
||||
@classmethod
|
||||
async def update(cls, package: list[str] | str):
|
||||
@@ -110,7 +101,7 @@ class VirtualEnvPackageManager:
|
||||
command = cls.__get_command()
|
||||
command.append("install")
|
||||
command.append("--upgrade")
|
||||
command.append(" ".join(package))
|
||||
command.extend(package)
|
||||
logger.info(f"执行虚拟环境更新包指令: {command}", LOG_COMMAND)
|
||||
result = await asyncio.to_thread(
|
||||
subprocess.run,
|
||||
@@ -121,9 +112,10 @@ class VirtualEnvPackageManager:
|
||||
)
|
||||
logger.debug(f"更新虚拟环境包指令执行完成: {result.stdout}", LOG_COMMAND)
|
||||
return result.stdout
|
||||
except CalledProcessError as e:
|
||||
logger.error(f"更新虚拟环境包指令执行失败: {e.stderr}.", LOG_COMMAND)
|
||||
return e.stderr
|
||||
except (CalledProcessError, FileNotFoundError) as e:
|
||||
stderr = e.stderr if isinstance(e, CalledProcessError) else str(e)
|
||||
logger.error(f"更新虚拟环境包指令执行失败: {stderr}.", LOG_COMMAND)
|
||||
return stderr
|
||||
|
||||
@staticmethod
|
||||
def _clean_requirements_file(file_path: Path) -> None:
|
||||
@@ -191,12 +183,13 @@ class VirtualEnvPackageManager:
|
||||
LOG_COMMAND,
|
||||
)
|
||||
return result.stdout
|
||||
except CalledProcessError as e:
|
||||
except (CalledProcessError, FileNotFoundError) as e:
|
||||
stderr = e.stderr if isinstance(e, CalledProcessError) else str(e)
|
||||
logger.error(
|
||||
f"安装虚拟环境依赖文件指令执行失败: {e.stderr}.",
|
||||
f"安装虚拟环境依赖文件指令执行失败: {stderr}.",
|
||||
LOG_COMMAND,
|
||||
)
|
||||
return e.stderr
|
||||
return stderr
|
||||
|
||||
@classmethod
|
||||
async def list(cls) -> str:
|
||||
@@ -217,6 +210,7 @@ class VirtualEnvPackageManager:
|
||||
LOG_COMMAND,
|
||||
)
|
||||
return result.stdout
|
||||
except CalledProcessError as e:
|
||||
logger.error(f"列出虚拟环境包指令执行失败: {e.stderr}.", LOG_COMMAND)
|
||||
except (CalledProcessError, FileNotFoundError) as e:
|
||||
stderr = e.stderr if isinstance(e, CalledProcessError) else str(e)
|
||||
logger.error(f"列出虚拟环境包指令执行失败: {stderr}.", LOG_COMMAND)
|
||||
return ""
|
||||
|
||||
@@ -58,7 +58,7 @@ class ZhenxunRepoConfig:
|
||||
# 备份杂项
|
||||
BACKUP_FILES: ClassVar[list[str]] = [
|
||||
"pyproject.toml",
|
||||
"poetry.lock",
|
||||
"uv.lock",
|
||||
"requirements.txt",
|
||||
".env.dev",
|
||||
".env.example",
|
||||
@@ -89,7 +89,7 @@ class ZhenxunRepoConfig:
|
||||
PYPROJECT_FILE_STRING = "pyproject.toml"
|
||||
PYPROJECT_FILE = Path() / PYPROJECT_FILE_STRING
|
||||
|
||||
PYPROJECT_LOCK_FILE_STRING = "poetry.lock"
|
||||
PYPROJECT_LOCK_FILE_STRING = "uv.lock"
|
||||
PYPROJECT_LOCK_FILE = Path() / PYPROJECT_LOCK_FILE_STRING
|
||||
|
||||
|
||||
@@ -363,13 +363,16 @@ class ZhenxunRepoManagerClass:
|
||||
download_url = await GithubUtils.parse_github_url(
|
||||
self.config.RESOURCE_GITHUB_URL
|
||||
).get_archive_download_urls()
|
||||
logger.debug("开始下载resources资源包...", LOG_COMMAND)
|
||||
logger.info("开始下载资源压缩包...", LOG_COMMAND)
|
||||
if await AsyncHttpx.download_file(
|
||||
download_url, self.config.RESOURCE_ZIP_FILE, stream=True
|
||||
download_url,
|
||||
self.config.RESOURCE_ZIP_FILE,
|
||||
stream=True,
|
||||
show_progress=True,
|
||||
):
|
||||
logger.debug("下载resources资源文件压缩包成功!", LOG_COMMAND)
|
||||
logger.info("下载资源压缩包成功!", LOG_COMMAND)
|
||||
else:
|
||||
raise ZhenxunUpdateException("下载resources资源包失败...")
|
||||
raise ZhenxunUpdateException("下载资源压缩包失败...")
|
||||
|
||||
async def resources_unzip(self):
|
||||
"""解压资源文件"""
|
||||
@@ -428,13 +431,16 @@ class ZhenxunRepoManagerClass:
|
||||
source: Literal["git", "ali"] = "ali",
|
||||
branch: str = "main",
|
||||
force: bool = False,
|
||||
):
|
||||
) -> RepoUpdateResult | None:
|
||||
"""更新资源文件
|
||||
|
||||
参数:
|
||||
source: 更新源,git 为 git 更新,ali 为阿里云更新
|
||||
branch: 分支名称
|
||||
force: 是否强制更新
|
||||
|
||||
返回:
|
||||
RepoUpdateResult | None: git 更新时返回结果,zip 更新时返回 None
|
||||
"""
|
||||
critical_dir = self.config.RESOURCE_PATH / "themes" / "default"
|
||||
if not critical_dir.exists() or not any(critical_dir.iterdir()):
|
||||
@@ -445,11 +451,30 @@ class ZhenxunRepoManagerClass:
|
||||
force = True
|
||||
|
||||
if await check_git():
|
||||
await self.resources_git_update(source, branch, force)
|
||||
logger.debug("使用git更新资源文件!", LOG_COMMAND)
|
||||
result = await self.resources_git_update(source, branch, force)
|
||||
if result.success:
|
||||
logger.info("使用git更新资源文件完成!", LOG_COMMAND)
|
||||
return result
|
||||
else:
|
||||
logger.warning(
|
||||
f"使用git更新资源文件失败: {result.error_message},"
|
||||
"尝试回退到zip下载...",
|
||||
LOG_COMMAND,
|
||||
)
|
||||
# git 失败时回退 zip,确保资源文件一定能获取到
|
||||
try:
|
||||
await self.resources_zip_update()
|
||||
logger.info("回退zip下载资源文件完成!", LOG_COMMAND)
|
||||
result.success = True
|
||||
result.error_message = ""
|
||||
return result
|
||||
except Exception as e:
|
||||
logger.error("回退zip下载资源文件也失败", LOG_COMMAND, e=e)
|
||||
return result
|
||||
else:
|
||||
await self.resources_zip_update()
|
||||
logger.debug("使用zip更新资源文件!", LOG_COMMAND)
|
||||
logger.info("使用zip更新资源文件完成!", LOG_COMMAND)
|
||||
return None
|
||||
|
||||
# ==================== Web UI 管理相关方法 ====================
|
||||
|
||||
|
||||
@@ -339,6 +339,21 @@ class PlatformUtils:
|
||||
update_list.append(_group)
|
||||
if create_list:
|
||||
await GroupConsole.bulk_create(create_list, 10)
|
||||
task_modules = await GroupConsole._get_task_modules(default_status=False)
|
||||
plugin_modules = await GroupConsole._get_plugin_modules(
|
||||
default_status=False
|
||||
)
|
||||
new_ids = [g.group_id for g in create_list]
|
||||
fresh = await GroupConsole.filter(group_id__in=new_ids).all()
|
||||
if task_modules or plugin_modules:
|
||||
for group in fresh:
|
||||
await GroupConsole._update_modules(
|
||||
group, task_modules, plugin_modules
|
||||
)
|
||||
from zhenxun.services.cache.runtime_cache import GroupMemoryCache
|
||||
|
||||
for group in fresh:
|
||||
await GroupMemoryCache.upsert_from_model(group)
|
||||
if group_list:
|
||||
await GroupConsole.bulk_update(
|
||||
update_list, ["group_name", "max_member_count", "member_count"], 10
|
||||
|
||||
@@ -153,7 +153,7 @@ class BaseRepoManager(ABC):
|
||||
return ""
|
||||
|
||||
try:
|
||||
async with aiofiles.open(version_file) as f:
|
||||
async with aiofiles.open(version_file, encoding="utf-8") as f:
|
||||
return (await f.read()).strip()
|
||||
except Exception as e:
|
||||
logger.error(f"读取版本文件失败: {e}")
|
||||
@@ -174,10 +174,10 @@ class BaseRepoManager(ABC):
|
||||
|
||||
try:
|
||||
version_bb = "vNone"
|
||||
async with aiofiles.open(version_file) as rf:
|
||||
async with aiofiles.open(version_file, encoding="utf-8") as rf:
|
||||
if text := await rf.read():
|
||||
version_bb = text.strip().split("-")[0]
|
||||
async with aiofiles.open(version_file, "w") as f:
|
||||
async with aiofiles.open(version_file, "w", encoding="utf-8") as f:
|
||||
await f.write(f"{version_bb}-{version[:6]}")
|
||||
return True
|
||||
except Exception as e:
|
||||
@@ -261,9 +261,9 @@ class BaseRepoManager(ABC):
|
||||
# 检查本地目录是否存在
|
||||
if not await AsyncPath(local_path).exists():
|
||||
# 如果不存在,则克隆仓库
|
||||
logger.info(f"克隆仓库 {repo_url} 到 {local_path}", LOG_COMMAND)
|
||||
logger.info(f"正在克隆仓库 {repo_url},请耐心等待...", LOG_COMMAND)
|
||||
success, _stdout, stderr = await run_git_command(
|
||||
f"clone -b {branch} {repo_url} {local_path}"
|
||||
f"clone --progress -b {branch} {repo_url} {local_path}"
|
||||
)
|
||||
if not success:
|
||||
return RepoUpdateResult(
|
||||
@@ -375,11 +375,28 @@ class BaseRepoManager(ABC):
|
||||
|
||||
# 拉取最新代码
|
||||
logger.info(f"拉取最新代码: {repo_url}", LOG_COMMAND)
|
||||
pull_cmd = f"pull origin {branch}"
|
||||
if force:
|
||||
pull_cmd = f"fetch --all && git reset --hard origin/{branch}"
|
||||
logger.info("使用强制拉取模式", LOG_COMMAND)
|
||||
success, _, stderr = await run_git_command(pull_cmd, cwd=local_path)
|
||||
# 强制模式需要两步:先 fetch,再 reset,不能用 shell && 链式写法
|
||||
success, _, stderr = await run_git_command(
|
||||
"fetch --all", cwd=local_path
|
||||
)
|
||||
if not success:
|
||||
return RepoUpdateResult(
|
||||
repo_type=repo_type or RepoType.GITHUB,
|
||||
repo_name=repo_name,
|
||||
owner=owner or "",
|
||||
old_version=old_version.strip(),
|
||||
new_version="",
|
||||
error_message=f"拉取最新代码失败: {stderr}",
|
||||
)
|
||||
success, _, stderr = await run_git_command(
|
||||
f"reset --hard origin/{branch}", cwd=local_path
|
||||
)
|
||||
else:
|
||||
success, _, stderr = await run_git_command(
|
||||
f"pull origin {branch}", cwd=local_path
|
||||
)
|
||||
if not success:
|
||||
return RepoUpdateResult(
|
||||
repo_type=repo_type or RepoType.GITHUB,
|
||||
|
||||
@@ -23,8 +23,9 @@ async def check_git() -> bool:
|
||||
bool: 是否存在git命令
|
||||
"""
|
||||
try:
|
||||
process = await asyncio.create_subprocess_shell(
|
||||
"git --version",
|
||||
process = await asyncio.create_subprocess_exec(
|
||||
"git",
|
||||
"--version",
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
@@ -50,7 +51,7 @@ async def run_git_command(
|
||||
command: str, cwd: Path | None = None
|
||||
) -> tuple[bool, str, str]:
|
||||
"""
|
||||
运行git命令
|
||||
运行git命令,实时输出 stderr 进度信息(如 git clone --progress)。
|
||||
|
||||
参数:
|
||||
command: 命令
|
||||
@@ -60,19 +61,54 @@ async def run_git_command(
|
||||
tuple[bool, str, str]: (是否成功, 标准输出, 标准错误)
|
||||
"""
|
||||
try:
|
||||
full_command = f"git {command}"
|
||||
# 将Path对象转换为字符串
|
||||
cwd_str = str(cwd) if cwd else None
|
||||
process = await asyncio.create_subprocess_shell(
|
||||
full_command,
|
||||
args = command.split()
|
||||
process = await asyncio.create_subprocess_exec(
|
||||
"git",
|
||||
*args,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
cwd=cwd_str,
|
||||
cwd=cwd,
|
||||
)
|
||||
stdout_bytes, stderr_bytes = await process.communicate()
|
||||
|
||||
stdout = stdout_bytes.decode("utf-8").strip()
|
||||
stderr = stderr_bytes.decode("utf-8").strip()
|
||||
stderr_lines: list[str] = []
|
||||
|
||||
async def _read_stderr():
|
||||
assert process.stderr is not None
|
||||
buf = b""
|
||||
while True:
|
||||
chunk = await process.stderr.read(256)
|
||||
if not chunk:
|
||||
if buf:
|
||||
text = buf.decode("utf-8", errors="replace").strip()
|
||||
if text:
|
||||
stderr_lines.append(text)
|
||||
logger.debug(text, LOG_COMMAND)
|
||||
break
|
||||
buf += chunk
|
||||
while b"\n" in buf or b"\r" in buf:
|
||||
idx_r = buf.find(b"\r")
|
||||
idx_n = buf.find(b"\n")
|
||||
if idx_r == -1:
|
||||
idx = idx_n
|
||||
elif idx_n == -1:
|
||||
idx = idx_r
|
||||
else:
|
||||
idx = min(idx_r, idx_n)
|
||||
line_bytes = buf[:idx]
|
||||
if buf[idx : idx + 2] == b"\r\n":
|
||||
buf = buf[idx + 2 :]
|
||||
else:
|
||||
buf = buf[idx + 1 :]
|
||||
text = line_bytes.decode("utf-8", errors="replace").strip()
|
||||
if text:
|
||||
stderr_lines.append(text)
|
||||
logger.debug(text, LOG_COMMAND)
|
||||
|
||||
stdout_bytes, _ = await asyncio.gather(_collect_stdout(process), _read_stderr())
|
||||
|
||||
await process.wait()
|
||||
stdout = (stdout_bytes or b"").decode("utf-8").strip()
|
||||
stderr = "\n".join(stderr_lines)
|
||||
|
||||
return process.returncode == 0, stdout, stderr
|
||||
except Exception as e:
|
||||
@@ -80,6 +116,18 @@ async def run_git_command(
|
||||
return False, "", str(e)
|
||||
|
||||
|
||||
async def _collect_stdout(process: asyncio.subprocess.Process) -> bytes:
|
||||
"""收集子进程的全部 stdout 输出。"""
|
||||
assert process.stdout is not None
|
||||
chunks: list[bytes] = []
|
||||
while True:
|
||||
chunk = await process.stdout.read(4096)
|
||||
if not chunk:
|
||||
break
|
||||
chunks.append(chunk)
|
||||
return b"".join(chunks)
|
||||
|
||||
|
||||
def glob_to_regex(pattern: str) -> str:
|
||||
"""
|
||||
将glob模式转换为正则表达式
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
_RESTART_STATE_FILE = Path() / "data" / ".restart_state.json"
|
||||
_LAUNCHER_ACTION_KEY = "launcher_action"
|
||||
_ACTION_RESTART = "restart"
|
||||
|
||||
|
||||
def _ensure_state_parent() -> None:
|
||||
_RESTART_STATE_FILE.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
def read_restart_state() -> dict[str, Any]:
|
||||
if not _RESTART_STATE_FILE.exists():
|
||||
return {}
|
||||
try:
|
||||
data = json.loads(_RESTART_STATE_FILE.read_text(encoding="utf-8"))
|
||||
except Exception:
|
||||
return {}
|
||||
return data if isinstance(data, dict) else {}
|
||||
|
||||
|
||||
def write_restart_state(state: dict[str, Any]) -> None:
|
||||
if not state:
|
||||
if _RESTART_STATE_FILE.exists():
|
||||
_RESTART_STATE_FILE.unlink()
|
||||
return
|
||||
_ensure_state_parent()
|
||||
temp_file = _RESTART_STATE_FILE.with_name(f"{_RESTART_STATE_FILE.name}.tmp")
|
||||
temp_file.write_text(
|
||||
json.dumps(state, ensure_ascii=False, indent=2),
|
||||
encoding="utf-8",
|
||||
)
|
||||
temp_file.replace(_RESTART_STATE_FILE)
|
||||
|
||||
|
||||
def consume_launcher_restart_signal() -> bool:
|
||||
state = read_restart_state()
|
||||
if state.get(_LAUNCHER_ACTION_KEY) != _ACTION_RESTART:
|
||||
return False
|
||||
state.pop(_LAUNCHER_ACTION_KEY, None)
|
||||
write_restart_state(state)
|
||||
return True
|
||||
|
||||
|
||||
def clear_launcher_restart_signal() -> None:
|
||||
state = read_restart_state()
|
||||
if _LAUNCHER_ACTION_KEY not in state:
|
||||
return
|
||||
state.pop(_LAUNCHER_ACTION_KEY, None)
|
||||
write_restart_state(state)
|
||||
@@ -184,7 +184,7 @@ def change_img_md5(path_file: str | Path) -> bool:
|
||||
bool: 是否修改成功
|
||||
"""
|
||||
try:
|
||||
with open(path_file, "a") as f:
|
||||
with open(path_file, "a", encoding="utf-8") as f:
|
||||
f.write(str(int(time.time() * 1000)))
|
||||
return True
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user