-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge_wrapper.py
More file actions
67 lines (51 loc) · 2.01 KB
/
bridge_wrapper.py
File metadata and controls
67 lines (51 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
"""
Bridge 守护进程 — 崩溃自动重启
通过 subprocess 运行 bridge_simple.py,当进程异常退出时自动重启(5s 延迟)。
正常退出(exit code 0)或 bridge.state 为 disabled 时停止守护。
用法:
macOS/Linux:
nohup venv/bin/python3 -u bridge_wrapper.py > bridge.log 2>&1 < /dev/null &
Windows (PowerShell):
Start-Process -NoNewWindow -FilePath venv\Scripts\python.exe -ArgumentList "bridge_wrapper.py" -RedirectStandardOutput bridge.log
上下线控制:
上线: echo enabled > bridge.state (然后启动 wrapper)
下线: echo disabled > bridge.state (wrapper 会在下次检查时自动退出)
"""
import subprocess
import sys
import time
from pathlib import Path
SKILL_DIR = Path(__file__).parent.resolve()
STATE_FILE = SKILL_DIR / "bridge.state"
RESTART_DELAY = 5
def is_enabled():
if not STATE_FILE.exists():
return True
return STATE_FILE.read_text().strip() == "enabled"
def main():
while True:
if not is_enabled():
print(f"[wrapper] Bridge 已禁用 (bridge.state=disabled),退出守护")
break
print(f"[wrapper] 启动 bridge_simple.py ...")
proc = subprocess.run(
[sys.executable, "bridge_simple.py"] + sys.argv[1:],
cwd=str(SKILL_DIR)
)
rc = proc.returncode
if rc == 0:
print(f"[wrapper] bridge_simple.py 正常退出 (code=0)")
break
if not is_enabled():
print(f"[wrapper] Bridge 已禁用,不再重启")
break
# 128+N = 进程因信号 N 退出(SIGTERM=15 → 143, SIGINT=2 → 130)
if rc > 128:
sig = rc - 128
print(f"[wrapper] bridge_simple.py 被信号终止 (signal={sig}, code={rc}),{RESTART_DELAY}秒后重启...")
else:
print(f"[wrapper] bridge_simple.py 异常退出 (code={rc}),{RESTART_DELAY}秒后重启...")
time.sleep(RESTART_DELAY)
if __name__ == "__main__":
main()