-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
151 lines (125 loc) · 4.15 KB
/
Copy pathrun.py
File metadata and controls
151 lines (125 loc) · 4.15 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
from dotenv import load_dotenv
import os
load_dotenv(dotenv_path=os.path.join(os.path.dirname(os.path.abspath(__file__)), '.env'), override=True)
from utils.logger import setup_logger
import subprocess
import signal
import sys
import threading
import time
from knowledge.config_manager import ConfigManager
from monitor import SystemMonitor
import ascii_art
logger = setup_logger()
system_monitor = SystemMonitor()
cleanup_executed = False
config_manager = ConfigManager()
def run_bot():
logger.info("Starting bot...")
try:
required_files = ['config/bot_config.json']
for file in required_files:
if not os.path.exists(file):
logger.warning(f"Configuration file not found: {file}")
logger.info("Using default configuration")
env = os.environ.copy()
process = subprocess.Popen(
[sys.executable, "main.py"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
bufsize=1,
env=env
)
logger.info("Bot started correctly")
return process
except Exception as e:
logger.error(f"Error starting bot: {str(e)}")
return None
def run_frontend():
logger.info("Starting frontend server...")
try:
env = os.environ.copy()
process = subprocess.Popen(
[sys.executable, "frontend/app.py"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
bufsize=1,
env=env
)
logger.info("Frontend started correctly")
return process
except Exception as e:
logger.error(f"Error starting frontend: {str(e)}")
return None
def cleanup(processes):
global cleanup_executed
if cleanup_executed:
return
logger.info("Stopping all services...")
system_monitor.stop_monitoring()
for process in processes:
if process and process.poll() is None:
try:
process.terminate()
process.wait(timeout=5)
except subprocess.TimeoutExpired:
process.kill()
logger.info("All services stopped")
cleanup_executed = True
def monitor_process_output(process, name):
if process is None:
return
try:
for line in iter(process.stdout.readline, ''):
if line:
if "ERROR" in line or "Error" in line:
logger.error(f"{name}: {line}")
else:
logger.info(f"{name}: {line}")
except Exception as e:
logger.error(f"Error reading output of {name}: {str(e)}")
def main():
ascii_art.print_logo()
bot_config = config_manager.get_bot_config()
print("\n" + "="*50)
logger.info("==================================================")
logger.info("Virtual Assistant system starting")
logger.info("==================================================")
print("="*50 + "\n")
processes = []
def signal_handler(signum, frame):
cleanup(processes)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
try:
bot_process = run_bot()
if bot_process:
processes.append(bot_process)
bot_monitor = threading.Thread(
target=monitor_process_output,
args=(bot_process, "Bot"),
daemon=True
)
bot_monitor.start()
frontend_process = run_frontend()
if frontend_process:
processes.append(frontend_process)
frontend_monitor = threading.Thread(
target=monitor_process_output,
args=(frontend_process, "Frontend"),
daemon=True
)
frontend_monitor.start()
system_monitor.start_monitoring()
while all(p.poll() is None for p in processes):
time.sleep(0.1)
except KeyboardInterrupt:
logger.info("Signal received")
except Exception as e:
logger.error(f"Critical error: {str(e)}")
finally:
cleanup(processes)
if __name__ == "__main__":
main()