Start local backend in cluster client mode

- Workers need a local backend to connect to even in client mode
- Add backend startup and readiness check for cluster clients
- Ensure proper cleanup on exit
parent 0bb73422
......@@ -360,9 +360,52 @@ Examples:
print(f"Connecting to cluster master at {args.cluster_host}:{args.cluster_port}")
print("Press Ctrl+C to stop")
# Start backend process (needed for workers to connect to)
backend_cmd = [sys.executable, '-m', 'vidai.backend']
backend_proc = subprocess.Popen(backend_cmd)
# Wait for backend to be ready
import time
import socket
from vidai.compat import get_socket_path, is_unix_sockets_supported
print("Waiting for backend to be ready...")
max_wait = 15
for i in range(max_wait):
try:
if is_unix_sockets_supported():
# Check Unix socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.settimeout(1)
sock.connect(get_socket_path('worker'))
sock.close()
print("Backend Unix socket is ready!")
break
else:
# Check TCP port
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
sock.connect((args.backend_host, args.backend_worker_port))
sock.close()
print("Backend TCP port is ready!")
break
except (FileNotFoundError, ConnectionRefusedError, OSError):
if i < max_wait - 1:
print(f"Waiting for backend... ({i+1}/{max_wait})")
time.sleep(1)
else:
print("Backend failed to start within timeout")
backend_proc.terminate()
backend_proc.wait()
sys.exit(1)
# Start cluster client process
from vidai.cluster_client import start_cluster_client
try:
start_cluster_client(args.cluster_host, args.cluster_port, args.token, args.optimize, args.flash, args.weight, args.cluster_shared_dir)
finally:
backend_proc.terminate()
backend_proc.wait()
else:
print("Starting Video AI Analysis Tool...")
print(f"Server will be available at http://{args.host}:{args.port}")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment