Sadeep Sachintha commited on
Commit ·
754db9c
1
Parent(s): d8de1b4
debug: add network connection and DNS diagnostic to lifespan startup
Browse files
main.py
CHANGED
|
@@ -37,6 +37,36 @@ scheduler = AsyncIOScheduler()
|
|
| 37 |
|
| 38 |
@asynccontextmanager
|
| 39 |
async def lifespan(app: FastAPI):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# 1. Initialize Database
|
| 41 |
await init_db()
|
| 42 |
|
|
|
|
| 37 |
|
| 38 |
@asynccontextmanager
|
| 39 |
async def lifespan(app: FastAPI):
|
| 40 |
+
# 0. Connection Diagnostic
|
| 41 |
+
logger.info("--- STARTING NETWORK RESOLUTION & CONNECTION DIAGNOSTIC ---")
|
| 42 |
+
import aiohttp
|
| 43 |
+
|
| 44 |
+
# Check DNS resolution
|
| 45 |
+
for host in ["api.telegram.org", "v6.exchangerate-api.com"]:
|
| 46 |
+
try:
|
| 47 |
+
ips = socket.getaddrinfo(host, 443)
|
| 48 |
+
logger.info(f"DNS Resolution for {host}: {ips}")
|
| 49 |
+
except Exception as e:
|
| 50 |
+
logger.error(f"DNS Resolution failed for {host}: {e}")
|
| 51 |
+
|
| 52 |
+
# Try different socket configurations
|
| 53 |
+
configs = [
|
| 54 |
+
("Default (No family forced)", {}),
|
| 55 |
+
("Forced IPv4 (AF_INET)", {"family": socket.AF_INET}),
|
| 56 |
+
("Forced IPv6 (AF_INET6)", {"family": socket.AF_INET6})
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
for name, kwargs in configs:
|
| 60 |
+
try:
|
| 61 |
+
logger.info(f"Attempting connection with configuration: {name}")
|
| 62 |
+
connector = aiohttp.TCPConnector(**kwargs)
|
| 63 |
+
async with aiohttp.ClientSession(connector=connector) as sess:
|
| 64 |
+
async with sess.get("https://api.telegram.org/bot8962490919:AAFtXUpwmYe8SrtkLjvsDFTntz2El9ckPs0/getMe", timeout=5) as r:
|
| 65 |
+
logger.info(f"Configuration '{name}' SUCCESS: Status {r.status}, body: {await r.text()}")
|
| 66 |
+
except Exception as e:
|
| 67 |
+
logger.error(f"Configuration '{name}' FAILED: {type(e).__name__}: {e}")
|
| 68 |
+
logger.info("--- ENDING NETWORK RESOLUTION & CONNECTION DIAGNOSTIC ---")
|
| 69 |
+
|
| 70 |
# 1. Initialize Database
|
| 71 |
await init_db()
|
| 72 |
|