# -*- coding: utf-8 -*-
"""项目库共享服务看门狗：每 60 秒检查 8000 端口，掉线即以 0.0.0.0 重新拉起。
用于让同事的内网链接长期可用（本机开机常驻）。"""
import os, socket, subprocess, sys, time
BASE = r"D:\work buddy\2026-11-05-14-15-52"
PY = sys.executable

def alive():
    try:
        with socket.create_connection(("127.0.0.1", 8000), timeout=2):
            return True
    except Exception:
        return False

if alive():
    print("服务已在运行，不重复启动")
while True:
    if not alive():
        subprocess.Popen([PY, os.path.join(BASE, "serve_app.py")],
                         cwd=BASE, creationflags=0x00000008 | 0x00000200,
                         stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        time.sleep(6)
    time.sleep(60)

