-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch.py
More file actions
121 lines (109 loc) · 3.8 KB
/
launch.py
File metadata and controls
121 lines (109 loc) · 3.8 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
"""Control Tower Lite — Start broker + open dashboard.
Usage:
python launch.py # Start broker, open dashboard
python launch.py --fresh # Wipe DB and start fresh
python launch.py --status # Check if broker is running
python launch.py --stop # Stop the broker
"""
import json
import os
import signal
import subprocess
import sys
import time
import urllib.request
import urllib.error
from pathlib import Path
SCRIPT_DIR = Path(__file__).parent.resolve()
PORT = int(os.environ.get("CONTROL_TOWER_PORT", "7899"))
DB_PATH = os.environ.get("CONTROL_TOWER_DB", str(Path.home() / ".control-tower-lite.db"))
BASE_URL = f"http://127.0.0.1:{PORT}"
def is_running():
"""Check if broker is responding."""
try:
req = urllib.request.Request(f"{BASE_URL}/health")
with urllib.request.urlopen(req, timeout=2) as resp:
data = json.loads(resp.read())
return data.get("status") == "ok"
except Exception:
return False
def show_status():
"""Print broker status."""
if is_running():
try:
req = urllib.request.Request(f"{BASE_URL}/health")
with urllib.request.urlopen(req, timeout=2) as resp:
data = json.loads(resp.read())
uptime = data.get("uptime", 0)
mins = int(uptime // 60)
print(f"✓ Broker running on port {PORT} (uptime: {mins}m)")
print(f" Dashboard: {BASE_URL}/dashboard")
except Exception:
print(f"✓ Broker running on port {PORT}")
else:
print(f"✗ Broker not running on port {PORT}")
def stop_broker():
"""Stop the broker by sending SIGTERM."""
if not is_running():
print("Broker is not running")
return
# Try to find the process
if sys.platform == "win32":
os.system(f'powershell -Command "Get-Process -Id (Get-NetTCPConnection -LocalPort {PORT} -ErrorAction SilentlyContinue).OwningProcess -ErrorAction SilentlyContinue | Stop-Process -Force"')
else:
os.system(f"lsof -ti:{PORT} | xargs kill -TERM 2>/dev/null")
time.sleep(1)
if is_running():
print("✗ Failed to stop broker")
else:
print("✓ Broker stopped")
def start_broker(fresh=False):
"""Start the broker daemon."""
if is_running():
print(f"Broker already running on port {PORT}")
print(f"Dashboard: {BASE_URL}/dashboard")
return
if fresh:
db = Path(DB_PATH)
for suffix in ["", "-wal", "-shm"]:
p = Path(str(db) + suffix)
if p.exists():
p.unlink()
print("✓ Database wiped")
broker_path = str(SCRIPT_DIR / "broker.py")
if sys.platform == "win32":
subprocess.Popen(
[sys.executable, broker_path],
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.DETACHED_PROCESS,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
else:
subprocess.Popen(
[sys.executable, broker_path],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
start_new_session=True
)
# Wait for broker to start
for _ in range(20):
time.sleep(0.25)
if is_running():
print(f"✓ Broker started on port {PORT}")
print(f" Dashboard: {BASE_URL}/dashboard")
# Open dashboard in browser
import webbrowser
webbrowser.open(f"{BASE_URL}/dashboard")
return
print("✗ Broker failed to start within 5s")
def main():
args = sys.argv[1:]
if "--status" in args:
show_status()
elif "--stop" in args:
stop_broker()
elif "--help" in args or "-h" in args:
print(__doc__)
else:
start_broker(fresh="--fresh" in args)
if __name__ == "__main__":
main()