Spaces:
Sleeping
Sleeping
Upload scripts/start_server.py with huggingface_hub
Browse files- scripts/start_server.py +86 -0
scripts/start_server.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Persistent server starter: spawns server with proper detachment on Windows.
|
| 2 |
+
|
| 3 |
+
Uses subprocess.Popen with DETACHED_PROCESS + CREATE_NEW_PROCESS_GROUP flags
|
| 4 |
+
so the child survives parent exit. The child's stdio is redirected to log files.
|
| 5 |
+
"""
|
| 6 |
+
import os
|
| 7 |
+
import sys
|
| 8 |
+
import subprocess
|
| 9 |
+
import time
|
| 10 |
+
import urllib.request
|
| 11 |
+
|
| 12 |
+
WORKDIR = r"D:\opencode\captcha-solver-api"
|
| 13 |
+
LOG_DIR = r"D:\opencode\logs"
|
| 14 |
+
PYTHON = r"C:\Users\Administrator\AppData\Local\Programs\Python\Python312\python.exe"
|
| 15 |
+
PORT = 8765
|
| 16 |
+
|
| 17 |
+
# Ensure log dir
|
| 18 |
+
os.makedirs(LOG_DIR, exist_ok=True)
|
| 19 |
+
|
| 20 |
+
log_path = os.path.join(LOG_DIR, "solver.log")
|
| 21 |
+
err_path = os.path.join(LOG_DIR, "solver.err")
|
| 22 |
+
|
| 23 |
+
# Kill existing
|
| 24 |
+
subprocess.run(
|
| 25 |
+
["taskkill", "/F", "/IM", "python.exe", "/FI", f"WindowTitle eq captcha*"],
|
| 26 |
+
capture_output=True
|
| 27 |
+
)
|
| 28 |
+
time.sleep(1)
|
| 29 |
+
# Brute force kill anything on the port
|
| 30 |
+
out = subprocess.run(
|
| 31 |
+
["netstat", "-ano"],
|
| 32 |
+
capture_output=True, text=True
|
| 33 |
+
)
|
| 34 |
+
for line in out.stdout.splitlines():
|
| 35 |
+
if f":{PORT}" in line and "LISTENING" in line:
|
| 36 |
+
parts = line.split()
|
| 37 |
+
if parts:
|
| 38 |
+
try:
|
| 39 |
+
pid = int(parts[-1])
|
| 40 |
+
print(f"killing PID {pid} on port {PORT}")
|
| 41 |
+
subprocess.run(["taskkill", "/F", "/PID", str(pid)], capture_output=True)
|
| 42 |
+
except (ValueError, IndexError):
|
| 43 |
+
pass
|
| 44 |
+
|
| 45 |
+
# Spawn server fully detached
|
| 46 |
+
log_f = open(log_path, "wb")
|
| 47 |
+
err_f = open(err_path, "wb")
|
| 48 |
+
|
| 49 |
+
# DETACHED_PROCESS = 0x00000008, CREATE_NEW_PROCESS_GROUP = 0x00000200
|
| 50 |
+
flags = 0x00000008 | 0x00000200
|
| 51 |
+
|
| 52 |
+
proc = subprocess.Popen(
|
| 53 |
+
[PYTHON, "-u", "-m", "captcha_solver.main", "--port", str(PORT)],
|
| 54 |
+
cwd=WORKDIR,
|
| 55 |
+
stdout=log_f,
|
| 56 |
+
stderr=err_f,
|
| 57 |
+
stdin=subprocess.DEVNULL,
|
| 58 |
+
creationflags=flags,
|
| 59 |
+
close_fds=True,
|
| 60 |
+
)
|
| 61 |
+
print(f"started PID {proc.pid}")
|
| 62 |
+
|
| 63 |
+
# Wait for server to be ready
|
| 64 |
+
print("waiting for server...")
|
| 65 |
+
for i in range(60):
|
| 66 |
+
time.sleep(1)
|
| 67 |
+
try:
|
| 68 |
+
with urllib.request.urlopen(f"http://localhost:{PORT}/health", timeout=2) as r:
|
| 69 |
+
data = r.read().decode("utf-8")
|
| 70 |
+
print(f"READY after {i+1}s")
|
| 71 |
+
print(f"health: {data[:200]}")
|
| 72 |
+
break
|
| 73 |
+
except Exception:
|
| 74 |
+
if i % 5 == 4:
|
| 75 |
+
print(f" still waiting ({i+1}s)...")
|
| 76 |
+
else:
|
| 77 |
+
print("TIMEOUT after 60s")
|
| 78 |
+
print("--- log ---")
|
| 79 |
+
if os.path.exists(log_path):
|
| 80 |
+
with open(log_path) as f:
|
| 81 |
+
print(f.read()[-2000:])
|
| 82 |
+
print("--- err ---")
|
| 83 |
+
if os.path.exists(err_path):
|
| 84 |
+
with open(err_path) as f:
|
| 85 |
+
print(f.read()[-2000:])
|
| 86 |
+
sys.exit(1)
|