Spaces:
Sleeping
Sleeping
| import subprocess | |
| import sys | |
| import os | |
| import time | |
| import signal | |
| import httpx | |
| def cleanup(signum, frame): | |
| print("Stopping services...", flush=True) | |
| sys.exit(0) | |
| signal.signal(signal.SIGINT, cleanup) | |
| signal.signal(signal.SIGTERM, cleanup) | |
| def wait_for_gateway(url="http://127.0.0.1:8000/", timeout=60, interval=2): | |
| """Wait until the MCP Gateway is responding before starting Streamlit.""" | |
| print(f"β³ Waiting for Gateway at {url} (timeout={timeout}s)...", flush=True) | |
| start = time.time() | |
| while time.time() - start < timeout: | |
| try: | |
| resp = httpx.get(url, timeout=3.0) | |
| if resp.status_code == 200: | |
| print("β Gateway is healthy!", flush=True) | |
| return True | |
| except Exception: | |
| pass | |
| time.sleep(interval) | |
| print("β οΈ Gateway did not respond in time. Starting Streamlit anyway...", flush=True) | |
| return False | |
| def main(): | |
| print("π Starting Sentinel Monolith...", flush=True) | |
| # 1. Start the MCP Gateway (which now includes all microservices) | |
| gateway_cmd = [sys.executable, "mcp_gateway.py"] | |
| gateway_process = subprocess.Popen( | |
| gateway_cmd, | |
| cwd=os.getcwd(), | |
| stdout=sys.stdout, | |
| stderr=sys.stderr, | |
| ) | |
| print(f"β Gateway started (PID: {gateway_process.pid})", flush=True) | |
| # 2. Start the Monitor (runs in background loop) | |
| monitor_cmd = [sys.executable, "monitor.py"] | |
| monitor_process = subprocess.Popen( | |
| monitor_cmd, | |
| cwd=os.getcwd(), | |
| stdout=sys.stdout, | |
| stderr=sys.stderr, | |
| ) | |
| print(f"β Monitor started (PID: {monitor_process.pid})", flush=True) | |
| # 3. Wait for gateway to be healthy before starting Streamlit | |
| wait_for_gateway() | |
| # 4. Start Streamlit (Frontend) | |
| print("β Starting Streamlit on port 7860...", flush=True) | |
| streamlit_cmd = [ | |
| sys.executable, "-m", "streamlit", "run", "app.py", | |
| "--server.port", "7860", | |
| "--server.address", "0.0.0.0", | |
| "--server.headless", "true", | |
| "--browser.serverAddress", "0.0.0.0", | |
| "--server.enableCORS", "false", | |
| "--server.enableXsrfProtection", "false" | |
| ] | |
| # We use subprocess.run for the foreground process | |
| subprocess.run(streamlit_cmd, check=False) | |
| # Cleanup when streamlit exits | |
| gateway_process.terminate() | |
| monitor_process.terminate() | |
| if __name__ == "__main__": | |
| main() | |