中控更新改为先同步 git pull 再重启,并修复前端已是最新时一直显示更新中。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-04 07:56:06 +08:00
parent 04c750f2dd
commit ea24149a55
4 changed files with 122 additions and 80 deletions
+2 -1
View File
@@ -541,7 +541,8 @@ def api_hub_self_update():
from lib.update_lib import schedule_pull_and_restart from lib.update_lib import schedule_pull_and_restart
try: try:
result = schedule_pull_and_restart(delay_seconds=2.0) # 同步 pull + 延迟重启;timeout 留给 git pull
result = schedule_pull_and_restart(delay_seconds=1.2)
except Exception as e: except Exception as e:
result = {"ok": False, "msg": f"触发更新异常: {e}", "deferred": False} result = {"ok": False, "msg": f"触发更新异常: {e}", "deferred": False}
return jsonify(result), (200 if result.get("ok") else 400) return jsonify(result), (200 if result.get("ok") else 400)
+76 -67
View File
@@ -1,8 +1,7 @@
"""中控本机快速更新:git pull + pm2 restart.""" """中控本机快速更新:先同步 git pull,再异步 pm2 restart."""
from __future__ import annotations from __future__ import annotations
import os import os
import shlex
import subprocess import subprocess
import sys import sys
import threading import threading
@@ -23,14 +22,14 @@ def _repo_root() -> Path:
return _ROOT return _ROOT
def _run_git(args: list[str], *, cwd: Path) -> tuple[bool, str]: def _run_git(args: list[str], *, cwd: Path, timeout: float = 15) -> tuple[bool, str]:
try: try:
proc = subprocess.run( proc = subprocess.run(
["git", *args], ["git", *args],
cwd=str(cwd), cwd=str(cwd),
capture_output=True, capture_output=True,
text=True, text=True,
timeout=15, timeout=timeout,
) )
except FileNotFoundError: except FileNotFoundError:
return False, "未找到 git" return False, "未找到 git"
@@ -84,53 +83,42 @@ def read_git_version() -> dict[str, Any]:
return out return out
def _run_pull_and_restart(root: Path, script: Path) -> None: def _pm2_app_name() -> str:
log_path = _UPDATE_LOG return (os.getenv("PM2_APP_NAME") or "").strip() or "crypto_okx_hub"
def _schedule_pm2_restart(*, delay_seconds: float = 1.5) -> None:
app_name = _pm2_app_name()
delay = max(0.8, float(delay_seconds))
def _worker() -> None:
time.sleep(delay)
_append_log(f"pm2 restart {app_name} --update-env")
try: try:
log_path.parent.mkdir(parents=True, exist_ok=True)
except Exception:
pass
env = os.environ.copy()
env["REPO"] = str(root)
if not (env.get("PM2_APP_NAME") or "").strip():
env["PM2_APP_NAME"] = "crypto_okx_hub"
try:
with open(log_path, "a", encoding="utf-8") as logf:
logf.write(
time.strftime("%Y-%m-%d %H:%M:%S")
+ f" start pull_and_restart REPO={root}\n"
)
logf.flush()
proc = subprocess.run( proc = subprocess.run(
["bash", str(script)], ["pm2", "restart", app_name, "--update-env"],
cwd=str(root), capture_output=True,
env=env,
stdout=logf,
stderr=subprocess.STDOUT,
text=True, text=True,
timeout=180, timeout=120,
) )
logf.write( _append_log(
time.strftime("%Y-%m-%d %H:%M:%S") f"pm2 restart returncode={proc.returncode} "
+ f" finish returncode={proc.returncode}\n" f"out={(proc.stdout or proc.stderr or '')[:300]}"
) )
except Exception as e: except Exception as e:
_append_log("pull_and_restart error: " + (str(e) or "unknown")) _append_log("pm2 restart error: " + (str(e) or "unknown"))
threading.Thread(target=_worker, name="hub-pm2-restart", daemon=True).start()
def schedule_pull_and_restart(*, delay_seconds: float = 2.0) -> dict[str, Any]: def schedule_pull_and_restart(*, delay_seconds: float = 1.5) -> dict[str, Any]:
"""同步 git pull,再异步 pm2 restart(便于前端立刻知道是否有新版本)."""
if not sys.platform.startswith("linux"): if not sys.platform.startswith("linux"):
msg = f"仅 Linux 服务器支持更新(当前 platform={sys.platform})" msg = f"仅 Linux 服务器支持更新(当前 platform={sys.platform})"
_append_log("reject: " + msg) _append_log("reject: " + msg)
return {"ok": False, "msg": msg, "deferred": False, "platform": sys.platform} return {"ok": False, "msg": msg, "deferred": False, "platform": sys.platform}
root = _repo_root() root = _repo_root()
script = root / "deploy" / "pull_and_restart.sh"
if not script.is_file():
msg = f"缺少更新脚本: {script}"
_append_log("reject: " + msg)
return {"ok": False, "msg": msg, "deferred": False}
if not (root / "ecosystem.config.cjs").is_file(): if not (root / "ecosystem.config.cjs").is_file():
msg = f"缺少 ecosystem.config.cjs: {root}" msg = f"缺少 ecosystem.config.cjs: {root}"
_append_log("reject: " + msg) _append_log("reject: " + msg)
@@ -140,41 +128,62 @@ def schedule_pull_and_restart(*, delay_seconds: float = 2.0) -> dict[str, Any]:
now = time.time() now = time.time()
with _update_lock: with _update_lock:
if now < _update_pending_until: if now < _update_pending_until:
msg = "已有更新任务进行中,请稍后再试" return {"ok": False, "msg": "已有更新任务进行中,请稍后再试", "deferred": False}
return {"ok": False, "msg": msg, "deferred": False} _update_pending_until = now + 120.0
_update_pending_until = now + 180.0
version = read_git_version() before = read_git_version()
delay = max(1.0, float(delay_seconds)) before_sha = str(before.get("sha") or "")
_append_log(f"git pull start before={before.get('short_sha') or ''} root={root}")
def _worker() -> None: ok_pull, pull_out = _run_git(["pull", "--ff-only"], cwd=root, timeout=90)
try: if not ok_pull:
time.sleep(delay) # 回退再试普通 pull
_run_pull_and_restart(root, script) ok_pull, pull_out = _run_git(["pull"], cwd=root, timeout=90)
finally: after = read_git_version()
with _update_lock: after_sha = str(after.get("sha") or "")
# 保留短暂冷却,避免连点 changed = bool(before_sha and after_sha and before_sha != after_sha)
pass
try:
t = threading.Thread(target=_worker, name="hub-self-update", daemon=True)
t.start()
_append_log( _append_log(
f"accepted before={version.get('short_sha') or ''} delay={delay}s root={root}" f"git pull done ok={ok_pull} changed={changed} "
f"after={after.get('short_sha') or ''} msg={pull_out[:200]}"
) )
if not ok_pull and not after.get("ok"):
with _update_lock:
_update_pending_until = 0.0
return { return {
"ok": True, "ok": False,
"msg": "更新已受理,即将 git pull 并重启", "msg": pull_out or "git pull 失败",
"deferred": True, "deferred": False,
"before_sha": version.get("sha") or "", "before_sha": before_sha,
"before_short_sha": version.get("short_sha") or "", "before_short_sha": before.get("short_sha") or "",
"branch": version.get("branch") or "",
"repo": str(root),
"log": str(_UPDATE_LOG),
} }
# pull 失败但 HEAD 仍可读:仍尝试重启(可能网络问题但本地可跑)
try:
_schedule_pm2_restart(delay_seconds=delay_seconds)
except Exception as e: except Exception as e:
with _update_lock: with _update_lock:
_update_pending_until = 0.0 _update_pending_until = 0.0
msg = str(e) or "触发更新失败" return {"ok": False, "msg": str(e) or "触发重启失败", "deferred": False}
_append_log("reject: " + msg)
return {"ok": False, "msg": msg, "deferred": False} if not ok_pull:
msg = "git pull 未成功,已触发重启: " + (pull_out or "")
elif changed:
msg = f"已拉取新版本 {(before.get('short_sha') or '?')} → {(after.get('short_sha') or '?')},即将重启"
else:
msg = "代码已是最新,即将重启进程"
return {
"ok": True,
"msg": msg,
"deferred": True,
"changed": changed,
"pull_ok": ok_pull,
"pull_msg": pull_out[:500],
"before_sha": before_sha,
"before_short_sha": before.get("short_sha") or "",
"after_sha": after_sha,
"after_short_sha": after.get("short_sha") or "",
"branch": after.get("branch") or before.get("branch") or "",
"repo": str(root),
"log": str(_UPDATE_LOG),
}
+40 -8
View File
@@ -64,22 +64,38 @@
el.className = "update-status" + (cls ? " " + cls : ""); el.className = "update-status" + (cls ? " " + cls : "");
} }
async function waitRecover(versionUrl, beforeSha, statusEl, label) { async function waitRecover(versionUrl, beforeSha, statusEl, label, opts) {
const options = opts || {};
const expectSha = options.expectSha || "";
const alreadyLatest = !!options.alreadyLatest;
let sawDown = false; let sawDown = false;
const maxTries = 45; let stableOnline = 0;
const maxTries = 30;
for (let i = 0; i < maxTries; i++) { for (let i = 0; i < maxTries; i++) {
await sleep(2000); await sleep(1500);
const res = await fetchJson(versionUrl); const res = await fetchJson(versionUrl);
const v = res.data || {}; const v = res.data || {};
const online = res.httpOk && (v.ok !== false) && (v.online !== false); const online = res.httpOk && (v.ok !== false) && (v.online !== false);
if (!online) { if (!online) {
sawDown = true; sawDown = true;
setStatus(statusEl, "已受理 → 等待上线… (" + (i + 1) + "/" + maxTries + ")", "pending"); stableOnline = 0;
setStatus(statusEl, "重启中 → 等待上线… (" + (i + 1) + "/" + maxTries + ")", "pending");
continue; continue;
} }
const afterSha = v.sha || ""; const afterSha = v.sha || "";
const afterShort = v.short_sha || (afterSha ? afterSha.slice(0, 7) : ""); const afterShort = v.short_sha || (afterSha ? afterSha.slice(0, 7) : "");
const beforeShort = beforeSha ? String(beforeSha).slice(0, 7) : ""; const beforeShort = beforeSha ? String(beforeSha).slice(0, 7) : "";
if (expectSha && afterSha && afterSha === expectSha) {
setStatus(
statusEl,
(label || "更新完成") +
(beforeSha && beforeSha !== afterSha
? ": " + (beforeShort || "?") + " → " + (afterShort || "?")
: " " + (afterShort || "")),
"ok"
);
return v;
}
if (afterSha && beforeSha && afterSha !== beforeSha) { if (afterSha && beforeSha && afterSha !== beforeSha) {
setStatus(statusEl, (label || "更新完成") + ": " + (beforeShort || "?") + " → " + (afterShort || "?"), "ok"); setStatus(statusEl, (label || "更新完成") + ": " + (beforeShort || "?") + " → " + (afterShort || "?"), "ok");
return v; return v;
@@ -94,9 +110,21 @@
); );
return v; return v;
} }
setStatus(statusEl, "更新中… (" + (i + 1) + "/" + maxTries + ")", "pending"); stableOnline += 1;
// 重启很快时可能看不到掉线:连续在线数次且版本符合预期/未变 → 视为完成
if (stableOnline >= 4) {
if (alreadyLatest || (afterSha && beforeSha && afterSha === beforeSha)) {
setStatus(statusEl, "完成(已是最新 " + (afterShort || "") + ",进程已重启)", "ok");
return v;
} }
setStatus(statusEl, "超时未确认上线,请稍后刷新版本或查看 logs/hub_update.log", "err"); if (expectSha && afterSha === expectSha) {
setStatus(statusEl, (label || "更新完成") + " " + (afterShort || ""), "ok");
return v;
}
}
setStatus(statusEl, "等待重启生效… (" + (i + 1) + "/" + maxTries + ")", "pending");
}
setStatus(statusEl, "超时未确认,请点「刷新版本」或查看 logs/hub_update.log", "err");
return null; return null;
} }
@@ -147,13 +175,17 @@
btn.disabled = false; btn.disabled = false;
return; return;
} }
setStatus(statusEl, "已受理 → 等待上线…", "pending"); setStatus(statusEl, data.msg || "已拉取,等待重启…", "pending");
try { try {
await waitRecover( await waitRecover(
"/api/settings/update/hub/version", "/api/settings/update/hub/version",
beforeSha || data.before_sha || "", beforeSha || data.before_sha || "",
statusEl, statusEl,
"中控更新完成" "中控更新完成",
{
expectSha: data.after_sha || "",
alreadyLatest: !data.changed,
}
); );
await refreshHubVersion(); await refreshHubVersion();
} finally { } finally {
+1 -1
View File
@@ -183,7 +183,7 @@
{% endblock %} {% endblock %}
{% block scripts %} {% block scripts %}
<script src="{{ url_for('static', filename='settings_backup.js') }}?v=2"></script> <script src="{{ url_for('static', filename='settings_backup.js') }}?v=2"></script>
<script src="{{ url_for('static', filename='settings_update.js') }}?v=2"></script> <script src="{{ url_for('static', filename='settings_update.js') }}?v=3"></script>
<script> <script>
HubSettingsBackup.boot(); HubSettingsBackup.boot();
HubSettingsUpdate.boot(); HubSettingsUpdate.boot();