Files
any-auto-register/services/solver_manager.py
2026-03-30 17:43:18 +08:00

84 lines
2.3 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.
"""Turnstile Solver 进程管理 - 后端启动时自动拉起"""
import subprocess
import sys
import os
import time
import threading
import requests
SOLVER_PORT = int(os.getenv("SOLVER_PORT", "8889"))
SOLVER_URL = f"http://localhost:{SOLVER_PORT}"
SOLVER_BROWSER_TYPE = os.getenv("SOLVER_BROWSER_TYPE", "camoufox").strip() or "camoufox"
_proc: subprocess.Popen = None
_log_file = None
_lock = threading.Lock()
def is_running() -> bool:
try:
r = requests.get(f"{SOLVER_URL}/", timeout=2)
return r.status_code < 500
except Exception:
return False
def start():
global _proc, _log_file
with _lock:
if is_running():
print("[Solver] 已在运行")
return
solver_script = os.path.join(
os.path.dirname(__file__), "turnstile_solver", "start.py"
)
log_path = os.path.join(
os.path.dirname(__file__), "turnstile_solver", "solver.log"
)
_log_file = open(log_path, "a", encoding="utf-8")
_proc = subprocess.Popen(
[
sys.executable,
"-u",
solver_script,
"--browser_type",
SOLVER_BROWSER_TYPE,
"--port",
str(SOLVER_PORT),
],
stdout=_log_file,
stderr=subprocess.STDOUT,
)
# 等待服务就绪最多30s
for _ in range(30):
time.sleep(1)
if is_running():
print(f"[Solver] 已启动 PID={_proc.pid}")
return
if _proc.poll() is not None:
print(f"[Solver] 启动失败,退出码={_proc.returncode},日志: {log_path}")
_proc = None
if _log_file:
_log_file.close()
_log_file = None
return
print(f"[Solver] 启动超时,日志: {log_path}")
def stop():
global _proc, _log_file
with _lock:
if _proc and _proc.poll() is None:
_proc.terminate()
_proc.wait(timeout=5)
print("[Solver] 已停止")
_proc = None
if _log_file:
_log_file.close()
_log_file = None
def start_async():
"""在后台线程启动,不阻塞主进程"""
t = threading.Thread(target=start, daemon=True)
t.start()