Spaces:
Sleeping
Sleeping
Update start.py
Browse files
start.py
CHANGED
@@ -1,35 +1,81 @@
|
|
1 |
-
#!/usr/bin/env python3
|
2 |
-
|
3 |
import subprocess
|
4 |
import sys
|
5 |
import time
|
6 |
import os
|
7 |
import signal
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
def
|
10 |
-
"""Clean up processes
|
11 |
-
print("\n
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
sys.exit(0)
|
16 |
|
17 |
def main():
|
18 |
print("π Starting GitHub Activity Feed...")
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
try:
|
25 |
# Start FastAPI in background
|
26 |
print("π‘ Starting FastAPI webhook server...")
|
27 |
fastapi_process = subprocess.Popen([
|
28 |
-
sys.executable, "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
30 |
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
33 |
|
34 |
# Start Streamlit
|
35 |
print("π₯οΈ Starting Streamlit dashboard...")
|
@@ -40,29 +86,36 @@ def main():
|
|
40 |
"--server.headless=true",
|
41 |
"--global.disableWatchdogWarning=true",
|
42 |
"--server.enableCORS=false",
|
43 |
-
"--server.enableXsrfProtection=false"
|
44 |
-
|
|
|
|
|
|
|
45 |
|
46 |
print("β
Both services started successfully!")
|
47 |
-
print("π‘ FastAPI
|
48 |
-
print("π₯οΈ Streamlit available on port 8501")
|
|
|
49 |
|
50 |
-
#
|
51 |
while True:
|
52 |
# Check if processes are still running
|
53 |
if fastapi_process.poll() is not None:
|
54 |
-
print("β FastAPI process
|
55 |
break
|
56 |
if streamlit_process.poll() is not None:
|
57 |
-
print("β Streamlit process
|
58 |
break
|
|
|
59 |
time.sleep(10)
|
60 |
|
61 |
except KeyboardInterrupt:
|
62 |
-
|
|
|
63 |
except Exception as e:
|
64 |
print(f"β Error: {e}")
|
65 |
-
|
|
|
66 |
|
67 |
if __name__ == "__main__":
|
68 |
main()
|
|
|
|
|
|
|
1 |
import subprocess
|
2 |
import sys
|
3 |
import time
|
4 |
import os
|
5 |
import signal
|
6 |
+
import atexit
|
7 |
+
|
8 |
+
# Global process references
|
9 |
+
processes = []
|
10 |
|
11 |
+
def cleanup_processes():
|
12 |
+
"""Clean up all processes"""
|
13 |
+
print("\nπ§Ή Cleaning up processes...")
|
14 |
+
|
15 |
+
# Kill our spawned processes
|
16 |
+
for proc in processes:
|
17 |
+
try:
|
18 |
+
if proc and proc.poll() is None:
|
19 |
+
proc.terminate()
|
20 |
+
proc.wait(timeout=5)
|
21 |
+
except:
|
22 |
+
pass
|
23 |
+
|
24 |
+
# Kill any remaining processes by name
|
25 |
+
os.system("pkill -f 'uvicorn' 2>/dev/null")
|
26 |
+
os.system("pkill -f 'streamlit' 2>/dev/null")
|
27 |
+
print("β
Cleanup completed")
|
28 |
+
|
29 |
+
def signal_handler(signum, frame):
|
30 |
+
"""Handle shutdown signals"""
|
31 |
+
print(f"\nβ οΈ Received signal {signum}, shutting down...")
|
32 |
+
cleanup_processes()
|
33 |
sys.exit(0)
|
34 |
|
35 |
def main():
|
36 |
print("π Starting GitHub Activity Feed...")
|
37 |
|
38 |
+
# Register cleanup function
|
39 |
+
atexit.register(cleanup_processes)
|
40 |
+
|
41 |
+
# Set up signal handlers
|
42 |
+
signal.signal(signal.SIGINT, signal_handler)
|
43 |
+
signal.signal(signal.SIGTERM, signal_handler)
|
44 |
+
|
45 |
+
# Clean up any existing processes
|
46 |
+
cleanup_processes()
|
47 |
+
time.sleep(2)
|
48 |
|
49 |
try:
|
50 |
# Start FastAPI in background
|
51 |
print("π‘ Starting FastAPI webhook server...")
|
52 |
fastapi_process = subprocess.Popen([
|
53 |
+
sys.executable, "-c", """
|
54 |
+
import uvicorn
|
55 |
+
from api import app
|
56 |
+
import socket
|
57 |
+
|
58 |
+
def find_free_port(start=7861):
|
59 |
+
for port in range(start, start + 20):
|
60 |
+
try:
|
61 |
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
62 |
+
s.bind(('', port))
|
63 |
+
print(f'FastAPI running on port {port}')
|
64 |
+
return port
|
65 |
+
except OSError:
|
66 |
+
continue
|
67 |
+
return 8000
|
68 |
+
|
69 |
+
port = find_free_port()
|
70 |
+
uvicorn.run(app, host='0.0.0.0', port=port, log_level='warning', access_log=False)
|
71 |
+
"""
|
72 |
], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
73 |
|
74 |
+
processes.append(fastapi_process)
|
75 |
+
|
76 |
+
# Wait for FastAPI to start
|
77 |
+
print("β³ Waiting for FastAPI to initialize...")
|
78 |
+
time.sleep(8)
|
79 |
|
80 |
# Start Streamlit
|
81 |
print("π₯οΈ Starting Streamlit dashboard...")
|
|
|
86 |
"--server.headless=true",
|
87 |
"--global.disableWatchdogWarning=true",
|
88 |
"--server.enableCORS=false",
|
89 |
+
"--server.enableXsrfProtection=false",
|
90 |
+
"--browser.gatherUsageStats=false"
|
91 |
+
], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
92 |
+
|
93 |
+
processes.append(streamlit_process)
|
94 |
|
95 |
print("β
Both services started successfully!")
|
96 |
+
print("π‘ FastAPI webhook server is running")
|
97 |
+
print("π₯οΈ Streamlit dashboard available on port 8501")
|
98 |
+
print("π Monitoring services...")
|
99 |
|
100 |
+
# Monitor processes
|
101 |
while True:
|
102 |
# Check if processes are still running
|
103 |
if fastapi_process.poll() is not None:
|
104 |
+
print("β FastAPI process stopped")
|
105 |
break
|
106 |
if streamlit_process.poll() is not None:
|
107 |
+
print("β Streamlit process stopped")
|
108 |
break
|
109 |
+
|
110 |
time.sleep(10)
|
111 |
|
112 |
except KeyboardInterrupt:
|
113 |
+
print("\nβ οΈ Received keyboard interrupt")
|
114 |
+
cleanup_processes()
|
115 |
except Exception as e:
|
116 |
print(f"β Error: {e}")
|
117 |
+
cleanup_processes()
|
118 |
+
sys.exit(1)
|
119 |
|
120 |
if __name__ == "__main__":
|
121 |
main()
|