-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_all.py
More file actions
executable file
·269 lines (219 loc) · 8.24 KB
/
start_all.py
File metadata and controls
executable file
·269 lines (219 loc) · 8.24 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
#!/usr/bin/env python3
"""
Email Client CLI - Cross-platform startup script
Starts all components: main processor, admin backend, and admin frontend
"""
import os
import sys
import subprocess
import time
import signal
import atexit
import platform
import shutil
import requests
from pathlib import Path
from datetime import datetime
# ANSI color codes
RED = '\033[0;31m'
GREEN = '\033[0;32m'
YELLOW = '\033[1;33m'
BLUE = '\033[0;34m'
NC = '\033[0m' # No Color
# Store process handles
processes = []
def print_status(message):
"""Print status message with timestamp."""
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(f"{BLUE}[{timestamp}]{NC} {message}")
def print_success(message):
"""Print success message."""
print(f"{GREEN}✓{NC} {message}")
def print_error(message):
"""Print error message."""
print(f"{RED}✗{NC} {message}")
def print_warning(message):
"""Print warning message."""
print(f"{YELLOW}!{NC} {message}")
def cleanup():
"""Kill all child processes on exit."""
print_status("Shutting down all services...")
for process in processes:
if process.poll() is None: # Process is still running
try:
process.terminate()
process.wait(timeout=5)
except subprocess.TimeoutExpired:
process.kill()
print_success("All services stopped")
def check_command(command):
"""Check if a command exists."""
return shutil.which(command) is not None
def wait_for_service(url, name, max_attempts=30):
"""Wait for a service to be ready."""
print_status(f"Waiting for {name} to be ready...")
for attempt in range(max_attempts):
try:
response = requests.get(url, timeout=1)
if response.status_code in [200, 404]:
print_success(f"{name} is ready")
return True
except requests.exceptions.RequestException:
pass
print(".", end="", flush=True)
time.sleep(1)
print()
print_error(f"{name} failed to start")
return False
def setup_venv(path, requirements_file):
"""Set up virtual environment if it doesn't exist."""
venv_path = path / "venv"
if not venv_path.exists():
print_warning("Virtual environment not found. Creating...")
subprocess.run([sys.executable, "-m", "venv", str(venv_path)], check=True)
# Install requirements
if platform.system() == "Windows":
pip_path = venv_path / "Scripts" / "pip.exe"
else:
pip_path = venv_path / "bin" / "pip"
subprocess.run([str(pip_path), "install", "-q", "-r", str(requirements_file)], check=True)
print_success("Virtual environment created")
return venv_path
def get_python_executable(venv_path):
"""Get the python executable path for the virtual environment."""
if platform.system() == "Windows":
return venv_path / "Scripts" / "python.exe"
else:
return venv_path / "bin" / "python"
def main():
"""Main function to start all services."""
# Register cleanup
atexit.register(cleanup)
signal.signal(signal.SIGINT, lambda s, f: sys.exit(0))
signal.signal(signal.SIGTERM, lambda s, f: sys.exit(0))
# Get script directory
script_dir = Path(__file__).parent.absolute()
os.chdir(script_dir)
print()
print("=" * 60)
print("Email Client CLI - Starting All Services")
print("=" * 60)
print()
# Check prerequisites
print_status("Checking prerequisites...")
if not check_command("python3") and not check_command("python"):
print_error("Python 3 is not installed")
sys.exit(1)
if not check_command("node"):
print_error("Node.js is not installed")
sys.exit(1)
if not check_command("npm"):
print_error("npm is not installed")
sys.exit(1)
# Check .env file
if not (script_dir / ".env").exists():
print_error(".env file not found. Please copy .env.example and configure it.")
sys.exit(1)
print_success("All prerequisites met")
# Create log directory
log_dir = script_dir / "logs"
log_dir.mkdir(exist_ok=True)
# Start main email processor
print_status("Starting email processor...")
main_venv = setup_venv(script_dir, script_dir / "requirements.txt")
main_python = get_python_executable(main_venv)
with open(log_dir / "email_processor.log", "w") as log_file:
process = subprocess.Popen(
[str(main_python), "main.py"],
stdout=log_file,
stderr=subprocess.STDOUT,
cwd=script_dir
)
processes.append(process)
print_success(f"Email processor started (PID: {process.pid})")
# Start admin panel backend
print_status("Starting admin panel backend...")
backend_dir = script_dir / "admin_panel" / "backend"
backend_venv = setup_venv(backend_dir, backend_dir / "requirements.txt")
backend_python = get_python_executable(backend_venv)
# Set up environment with PYTHONPATH for imports
backend_env = os.environ.copy()
pythonpath_parts = []
if 'PYTHONPATH' in backend_env:
pythonpath_parts.append(backend_env['PYTHONPATH'])
pythonpath_parts.extend([
str(backend_dir),
str(backend_dir.parent.parent) # Root project directory
])
backend_env['PYTHONPATH'] = os.pathsep.join(pythonpath_parts)
with open(log_dir / "admin_backend.log", "w") as log_file:
process = subprocess.Popen(
[str(backend_python), "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"],
stdout=log_file,
stderr=subprocess.STDOUT,
cwd=backend_dir,
env=backend_env
)
processes.append(process)
print_success(f"Admin backend starting (PID: {process.pid})")
# Wait for backend
if not wait_for_service("http://localhost:8000/health", "Admin Backend"):
cleanup()
sys.exit(1)
# Start admin panel frontend
print_status("Starting admin panel frontend...")
frontend_dir = script_dir / "admin_panel" / "frontend"
# Check node_modules
if not (frontend_dir / "node_modules").exists():
print_warning("Node modules not found. Installing dependencies...")
subprocess.run(["npm", "install"], cwd=frontend_dir, check=True)
with open(log_dir / "admin_frontend.log", "w") as log_file:
process = subprocess.Popen(
["npm", "run", "dev", "--", "--host", "0.0.0.0"],
stdout=log_file,
stderr=subprocess.STDOUT,
cwd=frontend_dir
)
processes.append(process)
print_success(f"Admin frontend starting (PID: {process.pid})")
# Wait for frontend
if not wait_for_service("http://localhost:5173", "Admin Frontend"):
cleanup()
sys.exit(1)
# Print success message
print()
print(f"{GREEN}{'=' * 60}{NC}")
print(f"{GREEN}Email Client CLI System is running!{NC}")
print(f"{GREEN}{'=' * 60}{NC}")
print()
print("🚀 Services:")
print(" 📧 Email Processor: Running (checking every 5 minutes)")
print(" 🔧 Admin Backend: http://localhost:8000 (API docs: http://localhost:8000/docs)")
print(" 🌐 Admin Frontend: http://localhost:5173")
print()
print("📋 Default Login:")
print(" Email: admin@example.com")
print(" Password: changeme")
print()
print("📁 Logs:")
print(f" Email Processor: {log_dir}/email_processor.log")
print(f" Admin Backend: {log_dir}/admin_backend.log")
print(f" Admin Frontend: {log_dir}/admin_frontend.log")
print()
print("Press Ctrl+C to stop all services")
print()
# Keep running
try:
while True:
# Check if any process has died
for i, process in enumerate(processes):
if process.poll() is not None:
print_error(f"Process {i} has stopped unexpectedly")
cleanup()
sys.exit(1)
time.sleep(1)
except KeyboardInterrupt:
print()
print_status("Received interrupt signal")
if __name__ == "__main__":
main()