Fix env save-and-restart by deferring PM2 restart until after HTTP response.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-13 10:54:48 +08:00
parent c4753ffb89
commit 1bda78ceb2
5 changed files with 69 additions and 27 deletions
+2 -24
View File
@@ -2,8 +2,6 @@
from __future__ import annotations
import os
import subprocess
import sys
from pathlib import Path
from typing import Any
@@ -13,6 +11,7 @@ from lib.env.shared_env_lib import (
build_ai_env_payload,
restart_instances_then_hub_pm2,
)
from lib.instance.instance_pm2_lib import schedule_pm2_restart
HUB_DIR = Path(__file__).resolve().parent
@@ -46,25 +45,4 @@ def restart_all_pm2() -> dict[str, Any]:
def restart_hub_pm2() -> dict[str, Any]:
app_name = (os.getenv("PM2_APP_NAME") or "").strip() or "manual-trading-hub"
if not sys.platform.startswith("linux"):
return {"ok": False, "msg": "仅 Linux 服务器支持 PM2 重启", "app": app_name}
try:
proc = subprocess.run(
["pm2", "restart", app_name, "--update-env"],
capture_output=True,
text=True,
timeout=120,
)
ok = proc.returncode == 0
return {
"ok": ok,
"app": app_name,
"msg": (proc.stdout or proc.stderr or "").strip()[:500],
"returncode": proc.returncode,
}
except FileNotFoundError:
return {"ok": False, "msg": "未找到 pm2 命令", "app": app_name}
except subprocess.TimeoutExpired:
return {"ok": False, "msg": "pm2 restart 超时", "app": app_name}
except Exception as e:
return {"ok": False, "msg": str(e), "app": app_name}
return schedule_pm2_restart(app_name)