mirror of
https://git.oceanpay.cc/danial/kami_ctrip.git
synced 2025-12-18 12:32:09 +00:00
Introduce a health check endpoint `/health` to monitor the application's status, including uptime, memory usage, CPU percentage, and thread count. Add `psutil` dependency to gather system metrics. Also, include `curl` in Dockerfile for health check functionality and update the health check configuration in Dockerfile.
93 lines
2.5 KiB
Python
93 lines
2.5 KiB
Python
import json
|
||
import os
|
||
import time
|
||
import psutil
|
||
|
||
from flask import Flask, g, request, jsonify
|
||
|
||
from ctrip import CtripLogin
|
||
from logger import get_logger
|
||
|
||
app = Flask(__name__)
|
||
logger = get_logger()
|
||
|
||
# 记录应用启动时间
|
||
START_TIME = time.time()
|
||
|
||
|
||
@app.before_request
|
||
def before_request():
|
||
g.ip_list = None
|
||
g.order_num = None
|
||
g.ck = None
|
||
g.card_num = None
|
||
g.card_pwd = None
|
||
g.money = None
|
||
g.proxies = {}
|
||
|
||
|
||
@app.route("/health", methods=["GET"])
|
||
def health_check():
|
||
"""健康检查接口,用于容器监控服务状态"""
|
||
# 获取当前进程信息
|
||
process = psutil.Process(os.getpid())
|
||
|
||
# 计算运行时间
|
||
uptime = time.time() - START_TIME
|
||
|
||
# 获取内存使用情况
|
||
memory_info = process.memory_info()
|
||
|
||
health_data = {
|
||
"status": "ok",
|
||
"uptime": f"{uptime:.2f} seconds",
|
||
"memory": {
|
||
"rss": f"{memory_info.rss / (1024 * 1024):.2f} MB", # 物理内存
|
||
"vms": f"{memory_info.vms / (1024 * 1024):.2f} MB", # 虚拟内存
|
||
},
|
||
"cpu_percent": f"{process.cpu_percent(interval=0.1):.2f}%",
|
||
"threads": len(process.threads()),
|
||
"version": "1.0.0",
|
||
}
|
||
|
||
return jsonify(health_data)
|
||
|
||
|
||
@app.route("/xiecheng/v3/card/bind", methods=["GET", "POST"])
|
||
def bind_card_v3():
|
||
if request.method == "GET":
|
||
return "okk"
|
||
elif request.method == "POST":
|
||
# 接收参数
|
||
data = json.loads(request.get_data())
|
||
g.ck = data.get("ck")
|
||
g.card_num = data.get("card_num")
|
||
g.card_pwd = data.get("card_pwd")
|
||
g.order_num = data.get("order_num")
|
||
g.money = data.get("money")
|
||
# 绑卡
|
||
trip = CtripLogin(
|
||
cookies=g.ck,
|
||
card_num=g.card_num,
|
||
card_pwd=g.card_pwd,
|
||
price=g.money,
|
||
order_num=g.order_num,
|
||
)
|
||
res = trip.run()
|
||
logger.info(f"订单ID:{g.order_num},返回速代充:{res}")
|
||
if res.get("code") == 111:
|
||
for _ in range(5):
|
||
res = trip.check_card()
|
||
logger.info(f"订单ID:{g.order_num},未知重新查卡返回:{res}")
|
||
if res.get("code") in [110, 2001]:
|
||
res = {"code": 110, "msg": "重新查卡返回可重新绑卡", "data": {}}
|
||
return jsonify(res)
|
||
if res.get("code") == 111:
|
||
continue
|
||
res = {"code": 111, "msg": "重新查卡未知", "data": {}}
|
||
return jsonify(res)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
app.run()
|