Sejal Barshikar commited on
Commit
27ef441
·
1 Parent(s): 642fd7d

Add extensive logging to debug API startup

Browse files
Files changed (1) hide show
  1. app_combined.py +79 -12
app_combined.py CHANGED
@@ -3,18 +3,63 @@ import time
3
  import os
4
  import signal
5
  import sys
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  def run_api():
8
- print("Starting FastAPI server")
9
- api_process = subprocess.Popen(
10
- [sys.executable, "-m", "uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8000"],
11
- stdout=subprocess.PIPE,
12
- stderr=subprocess.PIPE
13
- )
14
- return api_process
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  def run_streamlit():
17
- print("Starting Streamlit app")
 
18
  streamlit_process = subprocess.Popen(
19
  [
20
  sys.executable, "-m", "streamlit", "run",
@@ -23,7 +68,8 @@ def run_streamlit():
23
  "--server.address=0.0.0.0",
24
  "--server.headless=true",
25
  "--browser.serverAddress=0.0.0.0",
26
- "--browser.gatherUsageStats=false"
 
27
  ]
28
  )
29
  return streamlit_process
@@ -33,19 +79,40 @@ def signal_handler(sig, frame):
33
  sys.exit(0)
34
 
35
  if __name__ == "__main__":
 
 
 
36
  signal.signal(signal.SIGINT, signal_handler)
37
  signal.signal(signal.SIGTERM, signal_handler)
 
 
38
  api_proc = run_api()
39
- time.sleep(5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  streamlit_proc = run_streamlit()
41
 
42
- print("Application is running!")
43
  print("FastAPI: http://localhost:8000")
44
  print("Streamlit: http://localhost:7860")
 
 
45
  try:
46
  streamlit_proc.wait()
47
  except KeyboardInterrupt:
48
- print("\nShutting down...")
49
  finally:
50
  api_proc.terminate()
51
  streamlit_proc.terminate()
 
3
  import os
4
  import signal
5
  import sys
6
+ import requests
7
+
8
+ def check_api_ready(max_attempts=30):
9
+ print("CHECKING API STATUS")
10
+
11
+ for i in range(max_attempts):
12
+ try:
13
+ print(f"Attempt {i+1}/{max_attempts}: Checking http://localhost:8000/")
14
+ response = requests.get("http://localhost:8000/", timeout=2)
15
+ if response.status_code == 200:
16
+ print(f"API IS READY! (took {i+1} seconds)")
17
+ return True
18
+ except requests.exceptions.ConnectionError as e:
19
+ print(f" Connection refused - API not ready yet")
20
+ except Exception as e:
21
+ print(f" Error: {e}")
22
+
23
+ time.sleep(1)
24
+
25
+ print("✗ API FAILED TO START AFTER 30 SECONDS!")
26
+ return False
27
 
28
  def run_api():
29
+ print("STARTING FASTAPI SERVER")
30
+
31
+ try:
32
+ api_process = subprocess.Popen(
33
+ [sys.executable, "-m", "uvicorn", "api.main:app",
34
+ "--host", "0.0.0.0", "--port", "8000", "--log-level", "info"],
35
+ stdout=subprocess.PIPE,
36
+ stderr=subprocess.STDOUT,
37
+ text=True,
38
+ bufsize=1
39
+ )
40
+
41
+ import threading
42
+ def log_api_output():
43
+ print("\n[API OUTPUT START]")
44
+ for line in iter(api_process.stdout.readline, ''):
45
+ if line:
46
+ print(f"[API] {line.strip()}")
47
+
48
+ log_thread = threading.Thread(target=log_api_output, daemon=True)
49
+ log_thread.start()
50
+
51
+ print(f"API process started with PID: {api_process.pid}")
52
+ return api_process
53
+
54
+ except Exception as e:
55
+ print(f"ERROR STARTING API: {e}")
56
+ import traceback
57
+ traceback.print_exc()
58
+ return None
59
 
60
  def run_streamlit():
61
+ print("STARTING STREAMLIT")
62
+
63
  streamlit_process = subprocess.Popen(
64
  [
65
  sys.executable, "-m", "streamlit", "run",
 
68
  "--server.address=0.0.0.0",
69
  "--server.headless=true",
70
  "--browser.serverAddress=0.0.0.0",
71
+ "--browser.gatherUsageStats=false",
72
+ "--server.enableCORS=false"
73
  ]
74
  )
75
  return streamlit_process
 
79
  sys.exit(0)
80
 
81
  if __name__ == "__main__":
82
+ print("APPLICATION STARTUP")
83
+
84
+ # Handle signals
85
  signal.signal(signal.SIGINT, signal_handler)
86
  signal.signal(signal.SIGTERM, signal_handler)
87
+
88
+ # Start API
89
  api_proc = run_api()
90
+
91
+ if api_proc is None:
92
+ print("FATAL: Could not start API process")
93
+ sys.exit(1)
94
+
95
+ # Wait for API
96
+ api_ready = check_api_ready(max_attempts=30)
97
+
98
+ if not api_ready:
99
+ print("FATAL: API did not become ready")
100
+ print("Terminating API process")
101
+ api_proc.terminate()
102
+ sys.exit(1)
103
+
104
+ # Start Streamlit
105
  streamlit_proc = run_streamlit()
106
 
107
+ print("BOTH SERVICES RUNNING")
108
  print("FastAPI: http://localhost:8000")
109
  print("Streamlit: http://localhost:7860")
110
+
111
+ # Wait
112
  try:
113
  streamlit_proc.wait()
114
  except KeyboardInterrupt:
115
+ print("\nShutting down")
116
  finally:
117
  api_proc.terminate()
118
  streamlit_proc.terminate()