fmab777 commited on
Commit
581ea2b
·
verified ·
1 Parent(s): 46693ee

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +31 -7
main.py CHANGED
@@ -1337,15 +1337,33 @@ async def lifespan(app: Starlette):
1337
  async def health_check(request: Request) -> PlainTextResponse:
1338
  """Simple health check endpoint."""
1339
  global OPENROUTER_MODEL, GEMINI_MODEL, APIFY_ACTOR_ID, _apify_token_exists, _gemini_primary_enabled, _openrouter_fallback_enabled
 
1340
  bot_status = "Not Initialized"
1341
  bot_username = "N/A"
1342
- if ptb_app and ptb_app.bot and ptb_app.initialized: # Check if initialized
 
 
1343
  try:
1344
- # Use a short timeout for health check get_me
1345
- bot_info = await ptb_app.bot.get_me() # No explicit timeout in ptb get_me, relies on request timeout
1346
- bot_username = f"@{bot_info.username}" if bot_info else "Error fetching bot info"
1347
- if ptb_app.running: bot_status = "Running"
1348
- else: bot_status = "Initialized but Not Running"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1349
  except TimedOut:
1350
  bot_status = "Timeout checking status"
1351
  logger.warning("Health check: Timeout getting bot info.")
@@ -1353,14 +1371,20 @@ async def health_check(request: Request) -> PlainTextResponse:
1353
  bot_status = f"Network Error checking status: {ne}"
1354
  logger.warning(f"Health check: NetworkError getting bot info: {ne}")
1355
  except Exception as e:
 
1356
  bot_status = f"Error checking status: {type(e).__name__}"
1357
  logger.warning(f"Health check: Error getting bot info: {e}")
 
 
 
 
 
1358
 
1359
  return PlainTextResponse(
1360
  f"TG Bot Summariser - Status: {bot_status} ({bot_username})\n"
1361
  f"Primary Model (Gemini): {GEMINI_MODEL if _gemini_primary_enabled else 'DISABLED'}\n"
1362
  f"Fallback Model (OpenRouter): {OPENROUTER_MODEL if _openrouter_fallback_enabled else 'DISABLED'}\n"
1363
- f"YT Fallback 1 (Supadata): {'Enabled' if SUPADATA_API_KEY else 'Disabled'}\n"
1364
  f"YT Fallback 2 (Apify Actor): {APIFY_ACTOR_ID if _apify_token_exists else 'DISABLED'}\n"
1365
  f"Web Scrape 1 (Direct+BS4): Enabled\n"
1366
  f"Web Scrape 2 (urltotext): {'Enabled' if _urltotext_key_exists else 'Disabled'}\n"
 
1337
  async def health_check(request: Request) -> PlainTextResponse:
1338
  """Simple health check endpoint."""
1339
  global OPENROUTER_MODEL, GEMINI_MODEL, APIFY_ACTOR_ID, _apify_token_exists, _gemini_primary_enabled, _openrouter_fallback_enabled
1340
+ global _urltotext_key_exists, _rapidapi_key_exists, SUPADATA_API_KEY # Added missing globals
1341
  bot_status = "Not Initialized"
1342
  bot_username = "N/A"
1343
+
1344
+ # Check if ptb_app exists first
1345
+ if ptb_app:
1346
  try:
1347
+ # Check if the application is running
1348
+ if ptb_app.running:
1349
+ # Try to get bot info if running
1350
+ # Use a shorter timeout specifically for the health check get_me if possible,
1351
+ # otherwise rely on the default bot request timeout.
1352
+ # Note: ptb_app.bot.get_me doesn't accept a timeout directly.
1353
+ bot_info = await ptb_app.bot.get_me()
1354
+ bot_username = f"@{bot_info.username}" if bot_info and bot_info.username else "Info Fetch Error"
1355
+ bot_status = "Running"
1356
+ else:
1357
+ # If ptb_app exists but isn't running, it's likely initialized but stopped or not started yet
1358
+ bot_status = "Initialized but Not Running"
1359
+ # Try getting bot info even if not running, might work if initialized
1360
+ if ptb_app.bot:
1361
+ try:
1362
+ bot_info = await ptb_app.bot.get_me()
1363
+ bot_username = f"@{bot_info.username}" if bot_info and bot_info.username else "Info Fetch Error"
1364
+ except Exception:
1365
+ bot_username = "Info Fetch Error (Not Running)"
1366
+
1367
  except TimedOut:
1368
  bot_status = "Timeout checking status"
1369
  logger.warning("Health check: Timeout getting bot info.")
 
1371
  bot_status = f"Network Error checking status: {ne}"
1372
  logger.warning(f"Health check: NetworkError getting bot info: {ne}")
1373
  except Exception as e:
1374
+ # Catch other potential errors during status check
1375
  bot_status = f"Error checking status: {type(e).__name__}"
1376
  logger.warning(f"Health check: Error getting bot info: {e}")
1377
+ else:
1378
+ # ptb_app itself is None (likely very early startup or major failure)
1379
+ bot_status = "Not Initialized"
1380
+ bot_username = "N/A"
1381
+
1382
 
1383
  return PlainTextResponse(
1384
  f"TG Bot Summariser - Status: {bot_status} ({bot_username})\n"
1385
  f"Primary Model (Gemini): {GEMINI_MODEL if _gemini_primary_enabled else 'DISABLED'}\n"
1386
  f"Fallback Model (OpenRouter): {OPENROUTER_MODEL if _openrouter_fallback_enabled else 'DISABLED'}\n"
1387
+ f"YT Fallback 1 (Supadata): {'Enabled' if SUPADATA_API_KEY else 'Disabled'}\n" # Check key existence
1388
  f"YT Fallback 2 (Apify Actor): {APIFY_ACTOR_ID if _apify_token_exists else 'DISABLED'}\n"
1389
  f"Web Scrape 1 (Direct+BS4): Enabled\n"
1390
  f"Web Scrape 2 (urltotext): {'Enabled' if _urltotext_key_exists else 'Disabled'}\n"