feat: Python 版前置机 Agent 完整实现

- WebSocket 连接 B 端(注册/心跳/指数退避重连)
- 命令分发(10 种远程运维命令)
- 看门狗守护、日志实时流、在线升级含自动回滚
- 修复 main.py 日志格式 style 与占位符不匹配的问题
- 补充 README、配置示例、依赖清单

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-08-27 11:58:31 +08:00
co-authored by Claude
parent cc35a41ea4
commit 68751c2354
13 changed files with 1188 additions and 3 deletions
+67 -2
View File
@@ -1,3 +1,68 @@
# hospital-front-agent
# hospital-front-agentPython 版)
前置机代理
医院前置机被控端 Agent,从 [hospital-front](../hospital-front)Kotlin/Java)拆出的独立 Python 实现。
## 职责
1. **WebSocket 主动连接 B 端管理服务器**(注册 / 心跳 / 接收命令)
2. **守护主程序**(健康探测 + 宕机自动拉起 + 告警)
3. **远程运维命令**:健康查询、版本查询、日志查看(尾部 + 实时流)、配置读写、优雅重启、**在线升级(含自动回滚)**
与 Java 版的**消息协议完全一致**,B 端无需任何修改。
## 目录结构
```
hospital-front-agent/
├── agent/
│ ├── main.py # 入口:组装、PID 文件、信号处理
│ ├── config.py # conf/agent.properties + AGENT_ 环境变量
│ ├── ws_client.py # WS 客户端:注册/心跳/指数退避重连
│ ├── dispatcher.py # 命令分发(10 种命令)
│ ├── main_app_client.py # 环回 HTTP 调主程序 /api/admin
│ ├── process_guard.py # 看门狗:3 次失败拉起、告警
│ ├── log_stream.py # 日志实时流:2s 增量轮询、滚动检测
│ └── upgrade.py # 升级 SOP:下载→校验→备份→停→替换→拉起→健康检查→回滚
├── conf/agent.properties # 配置示例
└── requirements.txt
```
## 运行
依赖 Python 3.10+。
```bash
pip install -r requirements.txt
python -m agent.main
```
工作目录须与主程序部署根目录一致(`bin/``pid/``tmp/``backup/` 所在目录),
配置默认从 `conf/agent.properties` 读取。
## 配置
`conf/agent.properties`。优先级:环境变量(`AGENT_SERVER_URL` 等) > properties > 默认值。
## 打包(PyInstaller 单 exe
```bash
pip install pyinstaller
pyinstaller --onefile --name hospital-agent --paths . agent/main.py
```
产物 `dist/hospital-agent.exe`,拷贝到部署根目录 `bin/` 下即可。
## Windows 服务注册(NSSM 示例)
```bat
nssm install HospitalAgent "C:\hospital\bin\hospital-agent.exe"
nssm set HospitalAgent AppDirectory "C:\hospital"
nssm set HospitalAgent AppStdout "C:\hospital\logs\agent-out.log"
nssm set HospitalAgent AppStderr "C:\hospital\logs\agent-err.log"
```
## 与 Java 版的差异
- Agent 为独立 Python 进程,**不再占用 `bin/hospital-front.jar`**
升级替换 jar 时无需维护 Agent jar 副本(Java 版遗留的 `hospital-agent.jar` 复制逻辑已移除)。
- 其余命令协议、升级 SOP、看门狗策略、日志流算法均与 Java 版等价。