-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_dev.py
More file actions
126 lines (100 loc) · 3.74 KB
/
start_dev.py
File metadata and controls
126 lines (100 loc) · 3.74 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
import os
import signal
import shutil
import subprocess
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parent
BACKEND = ROOT / "backend"
FRONTEND = ROOT / "frontend"
PYTHON = BACKEND / "venv" / "Scripts" / "python.exe"
ALEMBIC = BACKEND / "venv" / "Scripts" / "alembic.exe"
NPM = shutil.which("npm.cmd") or shutil.which("npm")
processes: list[tuple[str, subprocess.Popen]] = []
def step(message: str) -> None:
print(f"\n==> {message}", flush=True)
def require_path(path: Path, message: str) -> None:
if not path.exists():
raise RuntimeError(f"{message}: {path}")
def run_once(command: list[str], cwd: Path) -> None:
print(f"$ {' '.join(command)}", flush=True)
subprocess.run(command, cwd=cwd, check=True)
def start_process(name: str, command: list[str], cwd: Path) -> None:
print(f"Starting {name}: {' '.join(command)}", flush=True)
process = subprocess.Popen(command, cwd=cwd)
processes.append((name, process))
def stop_processes() -> None:
for name, process in reversed(processes):
if process.poll() is not None:
continue
print(f"Stopping {name} (PID {process.pid})...", flush=True)
if os.name == "nt":
subprocess.run(
["taskkill", "/PID", str(process.pid), "/T", "/F"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False,
)
else:
process.send_signal(signal.SIGTERM)
try:
process.wait(timeout=10)
except subprocess.TimeoutExpired:
process.kill()
def stop_docker_services() -> None:
subprocess.run(["docker", "compose", "stop"], cwd=ROOT, check=False)
def main() -> int:
require_path(PYTHON, "Backend virtual environment was not found")
require_path(ALEMBIC, "Alembic executable was not found")
require_path(FRONTEND / "package.json", "Frontend package.json was not found")
if not NPM:
raise RuntimeError("npm was not found. Install Node.js or make sure npm is available in PATH.")
try:
step("Starting Postgres and Redis")
run_once(["docker", "compose", "up", "-d"], ROOT)
step("Applying database migrations")
run_once([str(ALEMBIC), "upgrade", "head"], ROOT)
step("Starting backend, worker, and frontend")
start_process(
"backend API",
[
str(PYTHON),
"-m",
"uvicorn",
"main:app",
"--reload",
"--host",
"127.0.0.1",
"--port",
"8000",
],
BACKEND,
)
start_process("RQ worker", [str(PYTHON), "worker.py"], BACKEND)
start_process(
"frontend",
[NPM, "run", "dev", "--", "--host", "127.0.0.1"],
FRONTEND,
)
print("\nPDReader is running.", flush=True)
print("Frontend: http://127.0.0.1:5173", flush=True)
print("Backend: http://127.0.0.1:8000", flush=True)
print("\nPress Ctrl+C to stop everything.", flush=True)
while True:
for name, process in processes:
exit_code = process.poll()
if exit_code is not None:
raise RuntimeError(f"{name} stopped unexpectedly with exit code {exit_code}")
time.sleep(2)
except KeyboardInterrupt:
print("\nStop requested.", flush=True)
return 0
finally:
step("Stopping app services")
stop_processes()
step("Stopping Docker services")
stop_docker_services()
print("\nAll PDReader services stopped.", flush=True)
if __name__ == "__main__":
raise SystemExit(main())