重构:模块化项目结构,分离配置、路由、摄像头管理器,添加健康检查,更新Dockerfile和启动脚本
This commit is contained in:
63
app/health.py
Normal file
63
app/health.py
Normal file
@@ -0,0 +1,63 @@
|
||||
"""
|
||||
健康检查端点
|
||||
"""
|
||||
import psutil
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from flask import jsonify
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def get_memory_usage():
|
||||
"""获取内存使用(MB)"""
|
||||
process = psutil.Process()
|
||||
memory_mb = process.memory_info().rss / 1024 / 1024
|
||||
return round(memory_mb, 2)
|
||||
|
||||
def get_cpu_usage():
|
||||
"""获取CPU使用率(%)"""
|
||||
return psutil.cpu_percent(interval=0.1)
|
||||
|
||||
def register_health_routes(app):
|
||||
"""注册健康检查路由到Flask应用"""
|
||||
|
||||
@app.route('/status')
|
||||
def status():
|
||||
"""整体状态检查"""
|
||||
return jsonify({
|
||||
'status': 'ok',
|
||||
'service': 'multi-camera-monitor',
|
||||
'timestamp': datetime.now().isoformat()
|
||||
})
|
||||
|
||||
@app.route('/memory')
|
||||
def memory():
|
||||
"""内存使用"""
|
||||
return jsonify({
|
||||
'memory_mb': get_memory_usage(),
|
||||
'unit': 'MB'
|
||||
})
|
||||
|
||||
@app.route('/cpu')
|
||||
def cpu():
|
||||
"""CPU使用"""
|
||||
return jsonify({
|
||||
'cpu_percent': get_cpu_usage(),
|
||||
'unit': '%'
|
||||
})
|
||||
|
||||
@app.route('/health')
|
||||
def health():
|
||||
"""综合健康检查(用于Docker)"""
|
||||
try:
|
||||
# 简单检查
|
||||
memory = get_memory_usage()
|
||||
cpu = get_cpu_usage()
|
||||
return jsonify({
|
||||
'status': 'healthy',
|
||||
'memory_mb': memory,
|
||||
'cpu_percent': cpu
|
||||
}), 200
|
||||
except Exception as e:
|
||||
logger.error(f"健康检查失败: {e}")
|
||||
return jsonify({'status': 'unhealthy', 'error': str(e)}), 500
|
||||
Reference in New Issue
Block a user