import socket
import subprocess
import time
from datetime import datetime
import os
import mysql.connector
from zzb_data_prod.config import MYSQL_HOST,MYSQL_USER,MYSQL_PASSWORD,MYSQL_DB

def get_time():
    return datetime.now().strftime('%Y-%m-%d %H:%M:%S')


def check_port(host, port):
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(5)
        result = sock.connect_ex((host, port))
        sock.close()
        return result == 0  # 返回布尔值，表示端口是否可用
    except Exception as e:
        print(f"[{get_time()}] 端口检测异常: {str(e)}")
        return False


def restart_service():
    try:
        subprocess.run(["bash", "/root/docker/milvus/standalone_embed.sh", "restart"])
        print(f"[{get_time()}] milvus服务重启成功")
        return True
    except subprocess.CalledProcessError as e:
        print(f"[{get_time()}] 服务重启失败: {str(e)}")
        return False


def start_application_process():
    """启动8000端口对应的应用进程"""
    try:
        # 先尝试停止可能存在的旧进程
        time.sleep(2)  # 给进程停止的时间
        # 进入应用目录
        # 启动新进程
        subprocess.run(
            ["bash", "/root/pdf_parser/restart_app.sh"],
        )
        print(f"[{get_time()}] 应用进程(8000端口)已成功启动")
        return True
            
    except Exception as e:
        print(f"[{get_time()}] 启动应用进程失败: {str(e)}")
        return False


def get_local_ip():
    try:
        # 创建一个 UDP 套接字
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        # 连接到一个外部地址（这里使用 Google 的公共 DNS 服务器）
        s.connect(("8.8.8.8", 80))
        # 获取本地套接字的 IP 地址
        local_ip = s.getsockname()[0]
    except Exception as e:
        print(f"[{get_time()}] 获取内网 IP 失败: {e}")
        local_ip = "127.0.0.1"  # 如果失败，返回本地回环地址
    finally:
        s.close()  # 关闭套接字
    return local_ip

def monitor_port_8000():
    """监控8000端口，如果异常则启动应用进程"""
    print(f"[{get_time()}] 检查8000端口状态...")
    port_available = check_port("127.0.0.1", 8000)
    
    if not port_available:
        print(f"[{get_time()}] 检测到8000端口异常，尝试启动应用进程...")
        success = start_application_process()
        
        if success:
            # 启动后检查是否成功
            time.sleep(10)  # 等待应用启动
            if check_port("127.0.0.1", 8000):
                print(f"[{get_time()}] 应用进程启动成功，8000端口已正常")
                # INSERT_YOUR_CODE
                # 检查并修改数据库字段
                try:
                    
                    conn = mysql.connector.connect(
                        host=MYSQL_HOST,
                        user=MYSQL_USER,
                        password=MYSQL_PASSWORD,  
                        database=MYSQL_DB   
                    )
                    cursor = conn.cursor()
                    local_ip = get_local_ip()
                    sql = f"update model_ip set status = 0 where ip = '{local_ip}:8000';"
                    print(f"[{get_time()}] 执行sql: {sql}")
                    cursor.execute(sql)
                    conn.commit()
                    print(f"[{get_time()}] 数据库字段已成功修改")
                except Exception as e:
                    print(f"[{get_time()}] 修改数据库字段失败: {str(e)}")
                finally:
                    try:
                        cursor.close()
                        conn.close()
                    except:
                        pass
            else:
                print(f"[{get_time()}] 应用进程启动后，8000端口仍未正常")
    else:
        print(f"[{get_time()}] 8000端口状态正常")
        
       

if __name__ == '__main__':
    print(f"[{get_time()}] 启动Milvus监控服务")
    port_ok = check_port("127.0.0.1", 19530)
    if not port_ok:
        print("检测到Milvus服务异常，尝试重启...")
        restart_service()

    print(f"[{get_time()}] 启动 8000 端口监控服务")
    # 开始监控8000端口，每60秒检查一次
    monitor_port_8000()
            
     
