coderuday21 commited on
Commit
a64e7fd
·
1 Parent(s): 5383f21

Make HF startup path simpler and add explicit startup logging

Browse files
Files changed (2) hide show
  1. Dockerfile +5 -5
  2. app/main.py +10 -0
Dockerfile CHANGED
@@ -19,7 +19,7 @@ COPY requirements.txt .
19
  RUN pip install --no-cache-dir -r requirements.txt
20
 
21
  # Cache-bust: increment to force a fresh COPY on HF Spaces
22
- ENV APP_BUILD=2
23
 
24
  # Copy application code
25
  COPY . .
@@ -29,10 +29,10 @@ RUN mkdir -p data/overlays && chown -R appuser:appuser /app
29
 
30
  USER appuser
31
 
32
- # HF Spaces expects port 7860; Render expects 10000
33
- # Use PORT env var with 7860 as default (works for both)
34
  ENV PORT=7860
 
35
  EXPOSE 7860
36
 
37
- # Single process: uvicorn only (simpler for HF Spaces; avoid gunicorn restart issues)
38
- CMD ["sh", "-c", "exec uvicorn app.main:app --host 0.0.0.0 --port ${PORT}"]
 
19
  RUN pip install --no-cache-dir -r requirements.txt
20
 
21
  # Cache-bust: increment to force a fresh COPY on HF Spaces
22
+ ENV APP_BUILD=3
23
 
24
  # Copy application code
25
  COPY . .
 
29
 
30
  USER appuser
31
 
32
+ # HF Spaces expects port 7860.
 
33
  ENV PORT=7860
34
+ ENV PYTHONUNBUFFERED=1
35
  EXPOSE 7860
36
 
37
+ # Use direct exec form so container startup is simpler and logs flush reliably.
38
+ CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860", "--log-level", "info"]
app/main.py CHANGED
@@ -29,9 +29,12 @@ from .models import User, DetectionRun
29
  from .notifier import send_notification, send_test_email
30
 
31
  import logging
 
32
  logger = logging.getLogger(__name__)
33
 
34
 
 
 
35
  _IST = timezone(timedelta(hours=5, minutes=30))
36
 
37
 
@@ -45,6 +48,7 @@ def _isoformat_ist(dt):
45
 
46
  # Create tables and run migrations without crashing the app (HF Spaces can restart if startup fails)
47
  try:
 
48
  Base.metadata.create_all(bind=engine, checkfirst=True)
49
  with engine.connect() as conn:
50
  for col, col_type in [
@@ -60,6 +64,7 @@ try:
60
  conn.commit()
61
  except Exception:
62
  conn.rollback()
 
63
  except Exception as e:
64
  import logging
65
  logging.getLogger("uvicorn.error").warning("Startup migration skipped: %s", e)
@@ -73,6 +78,11 @@ def health():
73
  from datetime import datetime
74
  return {"status": "ok", "version": "2.1.0", "server_time_ist": _isoformat_ist(datetime.now(timezone.utc))}
75
 
 
 
 
 
 
76
  # Mount static files
77
  STATIC_DIR = Path(__file__).resolve().parent.parent / "static"
78
  TEMPLATES_DIR = Path(__file__).resolve().parent.parent / "templates"
 
29
  from .notifier import send_notification, send_test_email
30
 
31
  import logging
32
+ logging.basicConfig(level=logging.INFO)
33
  logger = logging.getLogger(__name__)
34
 
35
 
36
+ logger.info("Importing AI Change Detection app module")
37
+
38
  _IST = timezone(timedelta(hours=5, minutes=30))
39
 
40
 
 
48
 
49
  # Create tables and run migrations without crashing the app (HF Spaces can restart if startup fails)
50
  try:
51
+ logger.info("Running database initialization")
52
  Base.metadata.create_all(bind=engine, checkfirst=True)
53
  with engine.connect() as conn:
54
  for col, col_type in [
 
64
  conn.commit()
65
  except Exception:
66
  conn.rollback()
67
+ logger.info("Database initialization complete")
68
  except Exception as e:
69
  import logging
70
  logging.getLogger("uvicorn.error").warning("Startup migration skipped: %s", e)
 
78
  from datetime import datetime
79
  return {"status": "ok", "version": "2.1.0", "server_time_ist": _isoformat_ist(datetime.now(timezone.utc))}
80
 
81
+
82
+ @app.on_event("startup")
83
+ def log_startup():
84
+ logger.info("FastAPI startup event completed")
85
+
86
  # Mount static files
87
  STATIC_DIR = Path(__file__).resolve().parent.parent / "static"
88
  TEMPLATES_DIR = Path(__file__).resolve().parent.parent / "templates"