RoyAalekh commited on
Commit
3c631de
·
1 Parent(s): 6587131

🛡️ Make app startup resilient - don't fail on Supabase connection issues

Browse files

✅ FIXES:
- App now starts even if Supabase connection fails
- Master tree database always initializes (146 species)
- Added graceful degradation - runs in 'limited mode' without DB
- Enhanced health endpoint with environment debug info
- Non-blocking startup for better deployment reliability

This ensures the app starts successfully on HuggingFace Spaces even with connection issues,
allowing us to debug the Supabase URL/key problems via the /health endpoint.

Files changed (1) hide show
  1. app.py +34 -15
app.py CHANGED
@@ -182,23 +182,31 @@ class TreeUpdate(BaseModel):
182
  async def startup_event():
183
  """Initialize application"""
184
  try:
185
- # Test Supabase connection
186
- if not db.test_connection():
187
- logger.error("Failed to connect to Supabase database")
188
- raise Exception("Database connection failed")
189
-
190
- # Initialize database schema
191
- db.initialize_database()
192
-
193
- # Initialize master tree database
194
  create_master_tree_database()
 
195
 
196
- # Log success
197
- tree_count = db.get_tree_count()
198
- logger.info(f"TreeTrack Supabase Edition initialized - {tree_count} trees in database")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
 
200
  except Exception as e:
201
- logger.error(f"Application startup failed: {e}")
 
202
  raise
203
 
204
 
@@ -210,12 +218,23 @@ async def health_check():
210
  connection_ok = db.test_connection()
211
  tree_count = db.get_tree_count() if connection_ok else 0
212
 
 
 
 
 
 
213
  return {
214
- "status": "healthy" if connection_ok else "unhealthy",
215
- "database": "connected" if connection_ok else "disconnected",
216
  "trees": tree_count,
 
217
  "timestamp": datetime.now().isoformat(),
218
  "version": "3.0.0",
 
 
 
 
 
219
  }
220
  except Exception as e:
221
  logger.error(f"Health check failed: {e}")
 
182
  async def startup_event():
183
  """Initialize application"""
184
  try:
185
+ # Initialize master tree database (always works - local SQLite)
 
 
 
 
 
 
 
 
186
  create_master_tree_database()
187
+ logger.info("Master tree database initialized with 146 species")
188
 
189
+ # Test Supabase connection (non-blocking)
190
+ try:
191
+ if db.test_connection():
192
+ # Initialize database schema if connection works
193
+ db.initialize_database()
194
+
195
+ # Log success
196
+ tree_count = db.get_tree_count()
197
+ logger.info(f"TreeTrack Supabase Edition initialized - {tree_count} trees in database")
198
+ else:
199
+ logger.warning("Supabase connection failed - running in limited mode")
200
+ logger.warning("Database operations will fail until Supabase is configured")
201
+ except Exception as db_error:
202
+ logger.error(f"Supabase connection error: {db_error}")
203
+ logger.warning("App starting in limited mode - only master tree database available")
204
+
205
+ logger.info("TreeTrack application startup complete")
206
 
207
  except Exception as e:
208
+ logger.error(f"Critical application startup failed: {e}")
209
+ # Only fail if master database fails (shouldn't happen)
210
  raise
211
 
212
 
 
218
  connection_ok = db.test_connection()
219
  tree_count = db.get_tree_count() if connection_ok else 0
220
 
221
+ # Include environment debug info
222
+ import os
223
+ supabase_url = os.getenv("SUPABASE_URL", "NOT_SET")
224
+ supabase_anon_key = os.getenv("SUPABASE_ANON_KEY", "NOT_SET")
225
+
226
  return {
227
+ "status": "healthy" if connection_ok else "limited",
228
+ "database": "connected" if connection_ok else "disconnected",
229
  "trees": tree_count,
230
+ "master_database": "available",
231
  "timestamp": datetime.now().isoformat(),
232
  "version": "3.0.0",
233
+ "environment": {
234
+ "supabase_url": supabase_url[:50] + "..." if len(supabase_url) > 50 else supabase_url,
235
+ "supabase_anon_key": "SET" if supabase_anon_key != "NOT_SET" else "NOT_SET",
236
+ "port": os.getenv("PORT", "7860")
237
+ }
238
  }
239
  except Exception as e:
240
  logger.error(f"Health check failed: {e}")