ujwal55 commited on
Commit
15aac81
Β·
verified Β·
1 Parent(s): 72740e6

Update start.py

Browse files
Files changed (1) hide show
  1. start.py +76 -23
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 cleanup_and_exit(signum=None, frame=None):
10
- """Clean up processes and exit"""
11
- print("\n⚠️ Shutting down services...")
12
- # Kill any remaining processes
13
- os.system("pkill -f 'uvicorn'")
14
- os.system("pkill -f 'streamlit'")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  sys.exit(0)
16
 
17
  def main():
18
  print("πŸš€ Starting GitHub Activity Feed...")
19
 
20
- # Set up signal handler for graceful shutdown
21
- signal.signal(signal.SIGINT, cleanup_and_exit)
22
- signal.signal(signal.SIGTERM, cleanup_and_exit)
 
 
 
 
 
 
 
23
 
24
  try:
25
  # Start FastAPI in background
26
  print("πŸ“‘ Starting FastAPI webhook server...")
27
  fastapi_process = subprocess.Popen([
28
- sys.executable, "main.py"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  ], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
30
 
31
- # Wait a bit for FastAPI to start
32
- time.sleep(5)
 
 
 
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 available on port 7861")
48
- print("πŸ–₯️ Streamlit available on port 8501")
 
49
 
50
- # Wait for processes to complete
51
  while True:
52
  # Check if processes are still running
53
  if fastapi_process.poll() is not None:
54
- print("❌ FastAPI process died")
55
  break
56
  if streamlit_process.poll() is not None:
57
- print("❌ Streamlit process died")
58
  break
 
59
  time.sleep(10)
60
 
61
  except KeyboardInterrupt:
62
- cleanup_and_exit()
 
63
  except Exception as e:
64
  print(f"❌ Error: {e}")
65
- cleanup_and_exit()
 
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()