-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_commands.py
More file actions
311 lines (253 loc) · 8.14 KB
/
Copy pathserver_commands.py
File metadata and controls
311 lines (253 loc) · 8.14 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"""
Server Commands
===============
Commands for managing the Auto Code web-backend server daemon.
Supports start, stop, status, and logs subcommands.
"""
import os
import signal
import subprocess
import sys
from pathlib import Path
from ui import print_status
# Default server configuration
DEFAULT_HOST = "0.0.0.0"
DEFAULT_PORT = 8000
DEFAULT_LOG_LINES = 50
# Relative path from the project root to the web-backend
_WEB_BACKEND_DIR = "apps/web-backend"
def _get_pid_file(project_dir: str) -> Path:
"""Return the path to the server PID file."""
pid_env = os.getenv("AUTO_CLAUDE_PID_FILE")
if pid_env:
return Path(pid_env)
return Path(project_dir) / "auto-claude-web.pid"
def _get_log_file(project_dir: str) -> Path:
"""Return the path to the server log file."""
log_env = os.getenv("AUTO_CLAUDE_LOG_FILE")
if log_env:
return Path(log_env)
return Path(project_dir) / ".auto-claude" / "server" / "server.log"
def _read_pid(pid_file: Path) -> int | None:
"""Read a PID from the given file; returns None if missing or invalid."""
if not pid_file.exists():
return None
try:
return int(pid_file.read_text(encoding="utf-8").strip())
except (OSError, ValueError):
return None
def _is_running(pid_file: Path) -> bool:
"""Check whether the process described by the PID file is still running."""
pid = _read_pid(pid_file)
if pid is None:
return False
try:
os.kill(pid, 0)
return True
except ProcessLookupError:
# Stale PID file
try:
pid_file.unlink()
except OSError:
pass
return False
except PermissionError:
# Process exists but belongs to a different user
return True
except OSError:
return False
def handle_server_start_command(
project_dir: str,
host: str = DEFAULT_HOST,
port: int = DEFAULT_PORT,
) -> bool:
"""
Start the Auto Code web-backend server as a background process.
Args:
project_dir: Project root directory
host: Host to bind (default: 0.0.0.0)
port: Port to listen on (default: 8000)
Returns:
True if the server started successfully
"""
pid_file = _get_pid_file(project_dir)
log_file = _get_log_file(project_dir)
if _is_running(pid_file):
pid = _read_pid(pid_file)
print_status(f"Server is already running (PID {pid})", "warning")
return False
web_backend_dir = Path(project_dir) / _WEB_BACKEND_DIR
if not web_backend_dir.exists():
print_status(
f"Web backend directory not found: {web_backend_dir}", "error"
)
return False
# Ensure log directory exists
log_file.parent.mkdir(parents=True, exist_ok=True)
env = os.environ.copy()
env["AUTO_CLAUDE_PID_FILE"] = str(pid_file)
try:
log_fd = open(log_file, "a", encoding="utf-8") # noqa: WPS515
except OSError as exc:
print_status(f"Cannot open log file {log_file}: {exc}", "error")
return False
try:
proc = subprocess.Popen(
[
sys.executable,
"-m",
"uvicorn",
"main:app",
"--host",
host,
"--port",
str(port),
],
cwd=str(web_backend_dir),
env=env,
stdout=log_fd,
stderr=log_fd,
start_new_session=True,
)
except FileNotFoundError:
log_fd.close()
print_status(
"uvicorn not found. Install with: pip install uvicorn", "error"
)
return False
except OSError as exc:
log_fd.close()
print_status(f"Failed to start server: {exc}", "error")
return False
log_fd.close()
# Write PID file from the parent process so CLI can track it
try:
pid_file.parent.mkdir(parents=True, exist_ok=True)
pid_file.write_text(str(proc.pid), encoding="utf-8")
except OSError as exc:
print_status(f"Warning: could not write PID file: {exc}", "warning")
print_status(
f"Server started (PID {proc.pid}) on {host}:{port}", "success"
)
print_status(f"Logs: {log_file}", "info")
return True
def handle_server_stop_command(project_dir: str) -> bool:
"""
Stop the running Auto Code web-backend server.
Args:
project_dir: Project root directory
Returns:
True if the server was stopped (or was not running)
"""
pid_file = _get_pid_file(project_dir)
if not _is_running(pid_file):
print_status("Server is not running", "info")
return True
pid = _read_pid(pid_file)
if pid is None:
print_status("Could not read server PID", "error")
return False
try:
os.kill(pid, signal.SIGTERM)
except ProcessLookupError:
print_status(f"Process {pid} not found; cleaning up PID file", "warning")
try:
pid_file.unlink()
except OSError:
pass
return True
except PermissionError:
print_status(
f"Permission denied sending SIGTERM to PID {pid}", "error"
)
return False
except OSError as exc:
print_status(f"Failed to stop server: {exc}", "error")
return False
print_status(f"SIGTERM sent to server (PID {pid})", "success")
return True
def handle_server_status_command(project_dir: str) -> bool:
"""
Show the current status of the Auto Code web-backend server.
Args:
project_dir: Project root directory
Returns:
True if the server is running, False if stopped
"""
pid_file = _get_pid_file(project_dir)
log_file = _get_log_file(project_dir)
running = _is_running(pid_file)
print_status("Auto Code Server Status", "info")
print()
if running:
pid = _read_pid(pid_file)
print(f" Status : \033[32mRunning\033[0m")
print(f" PID : {pid}")
else:
print(f" Status : \033[31mStopped\033[0m")
print(f" PID file : {pid_file}")
print(f" Log file : {log_file}")
print()
return running
def handle_server_logs_command(
project_dir: str,
lines: int = DEFAULT_LOG_LINES,
) -> bool:
"""
Display the tail of the server log file.
Args:
project_dir: Project root directory
lines: Number of log lines to show (default: 50)
Returns:
True if successful
"""
log_file = _get_log_file(project_dir)
if not log_file.exists():
print_status(f"Log file not found: {log_file}", "warning")
return False
try:
content = log_file.read_text(encoding="utf-8", errors="replace")
except OSError as exc:
print_status(f"Cannot read log file: {exc}", "error")
return False
all_lines = content.splitlines()
tail = all_lines[-lines:] if len(all_lines) > lines else all_lines
print_status(
f"Last {len(tail)} lines from {log_file}", "info"
)
print()
for line in tail:
print(line)
return True
def handle_server_command(
subcommand: str,
project_dir: str,
host: str = DEFAULT_HOST,
port: int = DEFAULT_PORT,
log_lines: int = DEFAULT_LOG_LINES,
) -> bool:
"""
Dispatch a server subcommand to the appropriate handler.
Args:
subcommand: One of start, stop, status, logs
project_dir: Project root directory
host: Host to bind when starting (default: 0.0.0.0)
port: Port when starting (default: 8000)
log_lines: Number of log lines to show with 'logs' subcommand
Returns:
True if the operation succeeded
"""
handlers = {
"start": lambda: handle_server_start_command(project_dir, host, port),
"stop": lambda: handle_server_stop_command(project_dir),
"status": lambda: handle_server_status_command(project_dir),
"logs": lambda: handle_server_logs_command(project_dir, log_lines),
}
handler = handlers.get(subcommand)
if handler is None:
valid = ", ".join(handlers.keys())
print_status(
f"Unknown server subcommand '{subcommand}'. Valid: {valid}", "error"
)
return False
return handler()