File size: 2,431 Bytes
5509816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
dispatchAI Startup Script - starts all services
Run this when the PC boots or after a restart.
"""
import subprocess
import time
import sys
import os

BASE_DIR = "C:/Users/RAK/dispatch-ai"
VENV_PYTHON = f"{BASE_DIR}/.venv/Scripts/python.exe"
CADDY_PATH = "C:/Users/RAK/AppData/Local/Microsoft/WinGet/Packages/CaddyServer.Caddy_Microsoft.Winget.Source_8wekyb3d8bbwe/caddy.exe"

def start_service(name, command, check_url=None, wait=3):
    """Start a background service."""
    print(f"  Starting {name}...", end=" ")
    try:
        proc = subprocess.Popen(
            command,
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
            shell=True,
        )
        time.sleep(wait)
        if check_url:
            import urllib.request
            try:
                urllib.request.urlopen(check_url, timeout=5)
                print(f"OK (PID {proc.pid})")
            except:
                print(f"STARTED (PID {proc.pid}) - health check pending")
        else:
            print(f"OK (PID {proc.pid})")
        return proc.pid
    except Exception as e:
        print(f"FAILED: {e}")
        return None

def main():
    print("=" * 50)
    print("dispatchAI Startup")
    print("=" * 50)

    pids = {}

    # 1. API Gateway
    pids["gateway"] = start_service(
        "API Gateway",
        f'cd {BASE_DIR} && {VENV_PYTHON} -m uvicorn api.gateway:app --host 0.0.0.0 --port 8081',
        check_url="http://127.0.0.1:8081/",
    )

    # 2. Phone Proxies
    phones = ["R3CN50EY64N", "R3CN50GRK5X", "R3CN50GTEYH"]
    for i, serial in enumerate(phones):
        port = 5000 + i
        pids[f"phone_{i}"] = start_service(
            f"Phone Proxy {serial}",
            f'cd {BASE_DIR} && {VENV_PYTHON} api/phone_proxy_v2.py {serial} {port}',
            check_url=f"http://127.0.0.1:{port}/health",
        )

    # 3. Caddy (HTTPS)
    pids["caddy"] = start_service(
        "Caddy HTTPS",
        f'"{CADDY_PATH}" run --config {BASE_DIR}/api/Caddyfile',
    )

    print()
    print("=" * 50)
    print("All services started!")
    print(f"  API: http://api.dispatchai.ai:8081")
    print(f"  HTTPS: https://api.dispatchai.ai")
    print(f"  Phones: 3 proxies on ports 5000-5002")
    print("=" * 50)

    # Save PIDs
    with open(f"{BASE_DIR}/data/service_pids.json", "w") as f:
        import json
        json.dump(pids, f, indent=2)

if __name__ == "__main__":
    main()