Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Upload folder using huggingface_hub
Browse files- main.py +3 -8
- modules/firebase/listener.py +16 -8
main.py
CHANGED
|
@@ -23,17 +23,12 @@ logging.basicConfig(level=logging.INFO)
|
|
| 23 |
|
| 24 |
@asynccontextmanager
|
| 25 |
async def fn_lifespan(app: FastAPI):
|
| 26 |
-
# π’ Startup logic
|
| 27 |
logging.info("π Starting Firestore listener...")
|
| 28 |
start_firestore_listener()
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# π΄ Shutdown logic (optional)
|
| 33 |
-
logging.info("π Shutting down Firestore listener...")
|
| 34 |
-
# stop_firestore_listener() if you have one
|
| 35 |
-
|
| 36 |
-
app = FastAPI(title="Sanatan AI Unified Server")
|
| 37 |
limiter = Limiter(key_func=get_remote_address)
|
| 38 |
app.state.limiter = limiter
|
| 39 |
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
|
|
|
| 23 |
|
| 24 |
@asynccontextmanager
|
| 25 |
async def fn_lifespan(app: FastAPI):
|
|
|
|
| 26 |
logging.info("π Starting Firestore listener...")
|
| 27 |
start_firestore_listener()
|
| 28 |
+
yield
|
| 29 |
+
logging.info("π Firestore listener shutdown (no explicit cleanup needed).")
|
| 30 |
|
| 31 |
+
app = FastAPI(title="Sanatan AI Unified Server",lifespan=fn_lifespan)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
limiter = Limiter(key_func=get_remote_address)
|
| 33 |
app.state.limiter = limiter
|
| 34 |
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
modules/firebase/listener.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import time
|
| 2 |
from firebase_admin import firestore as admin_firestore
|
| 3 |
import asyncio
|
|
@@ -51,13 +53,19 @@ def handle_changes(doc_snapshot, changes, read_time):
|
|
| 51 |
|
| 52 |
|
| 53 |
def start_firestore_listener():
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
# Run in background thread (so it doesn't block FastAPI)
|
| 63 |
# threading.Thread(target=start_firestore_listener, daemon=True).start()
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import threading
|
| 3 |
import time
|
| 4 |
from firebase_admin import firestore as admin_firestore
|
| 5 |
import asyncio
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
def start_firestore_listener():
|
| 56 |
+
"""Start Firestore listener in a background thread."""
|
| 57 |
+
def _listen():
|
| 58 |
+
collection_ref = db.collection("work_updates")
|
| 59 |
+
collection_ref.on_snapshot(handle_changes)
|
| 60 |
+
logging.info("π Firestore listener active.")
|
| 61 |
+
|
| 62 |
+
# Keep the thread alive to maintain snapshot streaming
|
| 63 |
+
while True:
|
| 64 |
+
time.sleep(60)
|
| 65 |
+
|
| 66 |
+
thread = threading.Thread(target=_listen, daemon=True)
|
| 67 |
+
thread.start()
|
| 68 |
+
logging.info("π Firestore listener thread started.")
|
| 69 |
+
|
| 70 |
# Run in background thread (so it doesn't block FastAPI)
|
| 71 |
# threading.Thread(target=start_firestore_listener, daemon=True).start()
|