-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.py
More file actions
222 lines (176 loc) · 7.96 KB
/
Copy pathdev.py
File metadata and controls
222 lines (176 loc) · 7.96 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import subprocess
import time
import sys
import socket
import os
import shutil
import argparse
from pathlib import Path
import urllib.request
import urllib.error
ROOT = Path(__file__).resolve().parent
BACKEND_DIR = ROOT / "backend"
BACKEND_VENV = BACKEND_DIR / "venv"
BACKEND_PYTHON = BACKEND_VENV / "bin" / "python"
BACKEND_REQUIREMENTS = BACKEND_DIR / "requirements.txt"
BACKEND_SETUP_STAMP = BACKEND_VENV / ".requirements-installed"
FRONTEND_DIR = ROOT / "frontend"
FRONTEND_SETUP_STAMP = FRONTEND_DIR / ".node_modules-installed"
FRONTEND_BUILD_DIR = FRONTEND_DIR / ".next"
def _parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="CodeSentinel local dev orchestrator")
parser.add_argument(
"--no-docker",
action="store_true",
help="Do not start docker-compose services (use system Redis/DB instead)",
)
return parser.parse_args()
def _is_port_free(port: int, host: str = "127.0.0.1") -> bool:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.bind((host, port))
return True
except OSError:
return False
def _pick_free_port(preferred: int, host: str = "127.0.0.1", max_tries: int = 20) -> int:
for offset in range(max_tries):
candidate = preferred + offset
if _is_port_free(candidate, host=host):
return candidate
raise RuntimeError(f"Could not find a free port starting at {preferred}")
def _wait_for_port(port: int, host: str = "127.0.0.1", timeout: int = 60) -> None:
"""Wait for a service to be ready on a port by attempting to connect."""
deadline = time.time() + timeout
while time.time() < deadline:
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(1)
s.connect((host, port))
return # Connection successful, service is ready
except (OSError, socket.timeout):
time.sleep(0.5)
raise RuntimeError(f"Timed out waiting for {host}:{port} to become ready")
def _wait_for_backend_ready(port: int, host: str = "127.0.0.1", timeout: int = 60) -> None:
"""Wait for the backend API to be fully ready by checking the health endpoint."""
deadline = time.time() + timeout
while time.time() < deadline:
try:
url = f"http://{host}:{port}/health"
with urllib.request.urlopen(url, timeout=2) as response:
if response.status == 200:
return # Backend is fully ready
except (urllib.error.URLError, urllib.error.HTTPError, OSError, socket.timeout):
time.sleep(0.5)
raise RuntimeError(f"Timed out waiting for backend at {host}:{port} to be ready")
def _run_checked(command: list[str], cwd: Path) -> None:
subprocess.run(command, cwd=cwd, check=True)
def _is_stale(marker: Path, dependency: Path) -> bool:
return not marker.exists() or marker.stat().st_mtime < dependency.stat().st_mtime
def _ensure_backend_environment() -> None:
if not BACKEND_PYTHON.exists():
print("Creating backend virtual environment...")
_run_checked([sys.executable, "-m", "venv", str(BACKEND_VENV)], cwd=BACKEND_DIR)
if _is_stale(BACKEND_SETUP_STAMP, BACKEND_REQUIREMENTS):
print("Installing backend Python dependencies...")
_run_checked([str(BACKEND_PYTHON), "-m", "pip", "install", "-r", str(BACKEND_REQUIREMENTS)], cwd=BACKEND_DIR)
BACKEND_SETUP_STAMP.touch()
def _ensure_frontend_environment() -> None:
if FRONTEND_BUILD_DIR.exists():
print("Removing stale frontend build cache...")
shutil.rmtree(FRONTEND_BUILD_DIR)
dependency_files = [FRONTEND_DIR / "package.json", FRONTEND_DIR / "yarn.lock"]
marker_is_stale = not FRONTEND_SETUP_STAMP.exists() or any(
FRONTEND_SETUP_STAMP.stat().st_mtime < dependency_file.stat().st_mtime
for dependency_file in dependency_files
if dependency_file.exists()
)
if marker_is_stale:
print("Installing frontend dependencies...")
if shutil.which("yarn"):
_run_checked(["yarn", "install"], cwd=FRONTEND_DIR)
else:
_run_checked(["npm", "install"], cwd=FRONTEND_DIR)
FRONTEND_SETUP_STAMP.touch()
def _start_background_services(skip_docker: bool) -> None:
if skip_docker:
print("Skipping Docker background services (using system services)...")
return
print("Starting background services (Redis, DB) via Docker Compose...")
subprocess.run(["docker", "compose", "up", "-d", "redis", "db"], cwd=ROOT, check=True)
def main():
args = _parse_args()
skip_docker = args.no_docker or os.environ.get("CODESENTINEL_SKIP_DOCKER", "").lower() in {"1", "true", "yes"}
_start_background_services(skip_docker)
_ensure_backend_environment()
_ensure_frontend_environment()
backend_port = _pick_free_port(8001)
if backend_port != 8001:
print(f"Port 8001 is busy; using backend port {backend_port} instead")
common_backend_env = os.environ.copy()
common_backend_env["CODESENTINEL_SANDBOX_NETWORK"] = "host"
frontend_env = os.environ.copy()
frontend_env["NEXT_PUBLIC_API_URL"] = f"http://localhost:{backend_port}"
frontend_env["NEXT_PUBLIC_WS_URL"] = f"ws://localhost:{backend_port}"
commands = [
{
"name": "Celery",
"cmd": [str(BACKEND_PYTHON), "-m", "celery", "-A", "workers.celery_app", "worker", "--loglevel=info", "--concurrency=1"],
"cwd": BACKEND_DIR,
"env": common_backend_env,
},
{
"name": "Backend",
"cmd": [str(BACKEND_PYTHON), "-m", "uvicorn", "main:app", "--reload", "--port", str(backend_port)],
"cwd": BACKEND_DIR,
"env": common_backend_env,
},
{
"name": "Frontend",
"cmd": ["yarn", "dev"] if (FRONTEND_DIR / "yarn.lock").exists() and shutil.which("yarn") else ["npm", "run", "dev"],
"cwd": FRONTEND_DIR,
"env": frontend_env,
}
]
processes = []
# Start Celery and Backend first
for c in commands[:2]:
print(f"Starting {c['name']}...")
p = subprocess.Popen(c["cmd"], cwd=c["cwd"], env=c["env"])
processes.append((c['name'], p))
print(f"Waiting for backend on port {backend_port} to be fully ready...")
_wait_for_backend_ready(backend_port)
print(f"Backend is ready on port {backend_port}")
# Now start Frontend with correct environment variables
frontend_command = commands[2]
print(f"Starting {frontend_command['name']}...")
frontend_process = subprocess.Popen(frontend_command["cmd"], cwd=frontend_command["cwd"], env=frontend_command["env"])
processes.append((frontend_command['name'], frontend_process))
print("\nAll services started! Press Ctrl+C to stop.\n")
try:
# Keep the main thread alive while background processes run
while len(processes) > 0:
time.sleep(1)
# check if anything crashed unexpectedly
for name, p in processes[:]:
if p.poll() is not None:
print(f"[{name}] exited unexpectedly with code {p.returncode}")
processes.remove((name, p))
print(f"Shutting down all remaining services because {name} failed...")
for oname, op in processes:
print(f"Stopping {oname}...")
op.terminate()
sys.exit(1)
except KeyboardInterrupt:
print("\n\nShutting down all services...")
# Terminate all gracefully
for name, p in processes:
print(f"Stopping {name}...")
p.terminate()
# Wait for all to finish
for name, p in processes:
p.wait()
print("All services stopped.")
sys.exit(0)
if __name__ == "__main__":
main()