feat: 新增内网文件服务器离线监控,升级ERP报表导出插件适配新旧系统差异
1. 新增内网文件服务器心跳上报与离线监控功能:
- 为文件服务器添加周期心跳上报到中心机的能力
- 中心机新增文件服务器状态监控线程,超时自动判离线并钉钉告警管理员
- 新增数据库表存储文件服务器状态,提供管理端API查看在线状态
2. 修复ERP物料收发汇总报表插件:
- 调整插件版本到1.0.9,更新前端配置与UI适配新旧ERP系统
- 重构组织与仓库默认值逻辑,区分新版/旧版ERP的默认配置
- 统一所有场景下显式设置查询组织,移除旧版默认组织的兼容逻辑
- 新增代管仓仓库编码适配旧版系统
3. 优化文件服务器打包脚本路径,更新文档示例命令
This commit is contained in:
@@ -14,9 +14,14 @@ from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
import secrets
|
||||
import socket
|
||||
import threading
|
||||
import time
|
||||
import urllib.request
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import FastAPI, Header, HTTPException, UploadFile
|
||||
@@ -24,7 +29,7 @@ from fastapi.responses import FileResponse
|
||||
|
||||
from fileserver.core import config
|
||||
|
||||
app = FastAPI(title="37du 内网文件服务器")
|
||||
log = logging.getLogger("fileserver.heartbeat")
|
||||
|
||||
_DATA_DIR = config.DATA_DIR
|
||||
_DATA_DIR.mkdir(parents=True, exist_ok=True)
|
||||
@@ -32,6 +37,9 @@ _DATA_DIR.mkdir(parents=True, exist_ok=True)
|
||||
_STATUS_DIR = _DATA_DIR / ".meta"
|
||||
_STATUS_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 进程启动时间(心跳上报 uptime 用)
|
||||
_START_TS = time.time()
|
||||
|
||||
|
||||
def _require_token(x_api_token: str = "") -> None:
|
||||
"""校验管理 token;未配置 token 时放行(纯开发模式)。"""
|
||||
@@ -62,6 +70,85 @@ def _file_meta(fid: str) -> dict:
|
||||
return {}
|
||||
|
||||
|
||||
# ==================== 中心机心跳上报(主动上报状态,供中心机监控在线/离线) ====================
|
||||
def _server_status() -> dict:
|
||||
"""收集本服务器状态快照:文件数/占用/主机/IP/运行时长等。"""
|
||||
meta_files = list(_STATUS_DIR.glob("*.json"))
|
||||
total = 0
|
||||
for mf in meta_files:
|
||||
try:
|
||||
m = json.loads(mf.read_text(encoding="utf-8"))
|
||||
total += int(m.get("size") or 0)
|
||||
except Exception: # noqa: BLE001
|
||||
pass
|
||||
hostname = socket.gethostname()
|
||||
ip = ""
|
||||
try:
|
||||
ip = socket.gethostbyname(hostname)
|
||||
except OSError: # noqa: BLE001
|
||||
pass
|
||||
return {
|
||||
"host": config.BASE_URL, # 中心机以其唯一标识本服务器(默认=对外下载根)
|
||||
"base_url": config.BASE_URL,
|
||||
"hostname": hostname,
|
||||
"ip": ip,
|
||||
"port": config.PORT,
|
||||
"file_count": len(meta_files),
|
||||
"total_size": total,
|
||||
"version": "37du-fileserver",
|
||||
"uptime_sec": int(time.time() - _START_TS),
|
||||
}
|
||||
|
||||
|
||||
def _post_heartbeat() -> bool:
|
||||
"""单次上报心跳到中心机;成功返回 True。"""
|
||||
payload = json.dumps(_server_status(), ensure_ascii=False).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
f"{config.CONTROLPLANE_URL}/api/fileserver/heartbeat",
|
||||
data=payload, method="POST")
|
||||
req.add_header("Content-Type", "application/json")
|
||||
if config.CONTROLPLANE_TOKEN:
|
||||
req.add_header("X-API-Token", config.CONTROLPLANE_TOKEN)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=10) as resp:
|
||||
return resp.status < 400
|
||||
except OSError as exc: # noqa: BLE001 网络/中心机异常按失败处理
|
||||
log.warning("心跳上报失败:%s", exc)
|
||||
return False
|
||||
|
||||
|
||||
def _heartbeat_loop() -> None:
|
||||
"""后台线程:周期向中心机上报心跳。未配置 CONTROLPLANE_URL 则直接退出。
|
||||
|
||||
每次上报最多重试 3 次(项目级重试约定),三次失败记错误日志;不阻塞文件服务。
|
||||
"""
|
||||
if not config.CONTROLPLANE_URL:
|
||||
log.info("未配置 CONTROLPLANE_URL,跳过心跳上报")
|
||||
return
|
||||
while True:
|
||||
try:
|
||||
ok = False
|
||||
for _i in range(3):
|
||||
if _post_heartbeat():
|
||||
ok = True
|
||||
break
|
||||
if not ok:
|
||||
log.error("心跳上报连续失败(3 次):%s", config.CONTROLPLANE_URL)
|
||||
except Exception: # noqa: BLE001 上报异常不拖垮文件服务
|
||||
log.exception("心跳上报线程异常")
|
||||
time.sleep(max(5, config.HEARTBEAT_INTERVAL))
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def _lifespan(_app: FastAPI):
|
||||
threading.Thread(target=_heartbeat_loop, daemon=True,
|
||||
name="fileserver-heartbeat").start()
|
||||
yield
|
||||
|
||||
|
||||
app = FastAPI(title="37du 内网文件服务器", lifespan=_lifespan)
|
||||
|
||||
|
||||
@app.post("/api/upload")
|
||||
async def api_upload(upload: UploadFile,
|
||||
x_api_token: str = Header(default="", alias="X-API-Token")) -> dict:
|
||||
|
||||
Reference in New Issue
Block a user