amaye15 commited on
Commit
fa54638
·
1 Parent(s): 4107020

Debug - Socket

Browse files
Files changed (1) hide show
  1. app/main.py +5 -13
app/main.py CHANGED
@@ -13,7 +13,7 @@ from fastapi import FastAPI, Depends # Import FastAPI itself
13
  from .database import connect_db, disconnect_db, database, metadata, users
14
  from .api import router as api_router
15
  from . import schemas, auth, dependencies
16
-
17
  # --- Import SQLAlchemy helpers for DDL generation ---
18
  from sqlalchemy.schema import CreateTable
19
  from sqlalchemy.dialects import sqlite # Assuming SQLite, adjust if needed
@@ -126,22 +126,16 @@ async def listen_to_websockets(token: str, notification_state: list):
126
  logger.info(f"[{ws_listener_id}] Attempting to connect to WebSocket: {ws_url}")
127
 
128
  try:
129
- # --- CORRECTED CONTEXT MANAGER USAGE ---
130
- # Use websockets.connect directly, passing timeout parameters
131
- # open_timeout controls connection establishment timeout
132
  async with websockets.connect(ws_url, open_timeout=15.0) as websocket:
133
- # --- END CORRECTION ---
134
  logger.info(f"[{ws_listener_id}] WebSocket connected successfully to {ws_url}")
135
 
136
- # --- Add a check after connection to see active connections ---
137
- # This helps confirm the backend sees the connection before receiving
138
- await asyncio.sleep(0.5) # Small delay to allow backend registration
139
- logger.info(f"[{ws_listener_id}] Connections according to manager after connect: {manager.active_connections}")
140
- # --- End check ---
141
 
142
  while True:
143
  try:
144
- # Add recv timeout (optional, but good practice)
145
  message_str = await asyncio.wait_for(websocket.recv(), timeout=300.0) # e.g., 5 min timeout
146
  logger.info(f"[{ws_listener_id}] Received raw message: {message_str}")
147
  try:
@@ -164,9 +158,7 @@ async def listen_to_websockets(token: str, notification_state: list):
164
  logger.error(f"[{ws_listener_id}] Error processing received message: {parse_err}")
165
 
166
  except asyncio.TimeoutError:
167
- # No message received within timeout, keep connection alive (ping might be needed for long silences)
168
  logger.debug(f"[{ws_listener_id}] WebSocket recv timed out, continuing loop.")
169
- # Consider sending a ping if needed: await websocket.ping()
170
  continue
171
  except websockets.ConnectionClosedOK:
172
  logger.info(f"[{ws_listener_id}] WebSocket connection closed normally.")
 
13
  from .database import connect_db, disconnect_db, database, metadata, users
14
  from .api import router as api_router
15
  from . import schemas, auth, dependencies
16
+ from .websocket import manager
17
  # --- Import SQLAlchemy helpers for DDL generation ---
18
  from sqlalchemy.schema import CreateTable
19
  from sqlalchemy.dialects import sqlite # Assuming SQLite, adjust if needed
 
126
  logger.info(f"[{ws_listener_id}] Attempting to connect to WebSocket: {ws_url}")
127
 
128
  try:
 
 
 
129
  async with websockets.connect(ws_url, open_timeout=15.0) as websocket:
 
130
  logger.info(f"[{ws_listener_id}] WebSocket connected successfully to {ws_url}")
131
 
132
+ # --- REMOVE or COMMENT OUT this debug line ---
133
+ # await asyncio.sleep(0.5)
134
+ # logger.info(f"[{ws_listener_id}] Connections according to manager after connect: {manager.active_connections}")
135
+ # --- End removal ---
 
136
 
137
  while True:
138
  try:
 
139
  message_str = await asyncio.wait_for(websocket.recv(), timeout=300.0) # e.g., 5 min timeout
140
  logger.info(f"[{ws_listener_id}] Received raw message: {message_str}")
141
  try:
 
158
  logger.error(f"[{ws_listener_id}] Error processing received message: {parse_err}")
159
 
160
  except asyncio.TimeoutError:
 
161
  logger.debug(f"[{ws_listener_id}] WebSocket recv timed out, continuing loop.")
 
162
  continue
163
  except websockets.ConnectionClosedOK:
164
  logger.info(f"[{ws_listener_id}] WebSocket connection closed normally.")