Files
kami_ctrip/app.py
danial 3896439d08 feat(monitoring): add health check endpoint and psutil dependency
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.
2025-04-06 21:47:35 +08:00

93 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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()