-
Notifications
You must be signed in to change notification settings - Fork 410
/
launch.py
74 lines (58 loc) · 1.67 KB
/
launch.py
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
import subprocess
import os
import time
import webbrowser
import sys
sys.path.append(os.getcwd())
import uvicorn
import threading
import asyncio
import logging
from server import app
server = None
sys.path.append((os.path.dirname(os.path.abspath(__file__))))
logging.getLogger("uvicorn.error").disabled = True
logging.getLogger("uvicorn.access").disabled = True
def start_server():
global server
config = uvicorn.Config(app, host="0.0.0.0", port=8000, log_level="critical")
server = uvicorn.Server(config)
thread = threading.Thread(target=server.run)
thread.start()
print("Server started")
def stop_server():
global server
if server:
server.should_exit = True
server.force_exit = True
try:
asyncio.run(server.shutdown())
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(server.shutdown())
loop.close()
except asyncio.CancelledError:
pass
print("Server stopped")
else:
print("Server is not running")
def run_npm(open: bool=False):
# Change directory to the 'web' subdirectory
os.chdir('agenthub')
# Run npm run dev asynchronously
if "node_modules" not in os.listdir():
install_process = subprocess.Popen(['npm', 'install'])
install_process.wait()
subprocess.Popen(['npm', 'run', 'dev'])
if open:
time.sleep(5)
webbrowser.open_new_tab("http://localhost:3000")
if __name__ == "__main__":
start_server()
run_npm(True)
try:
while True:
time.sleep(1)
finally:
stop_server()