mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-29 00:32:06 +08:00
* 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>
230 lines
7.5 KiB
Python
230 lines
7.5 KiB
Python
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 退出后接管重启。", "重启")
|