Spaces:
Running
Running
Update app/main.py
Browse files- app/main.py +30 -33
app/main.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
"""
|
2 |
-
Sentinel Arbitrage Engine -
|
3 |
|
4 |
-
This definitive version
|
5 |
-
|
6 |
"""
|
7 |
import asyncio
|
8 |
import os
|
@@ -16,9 +16,8 @@ import socketio
|
|
16 |
from fastapi import FastAPI
|
17 |
from fastapi.staticfiles import StaticFiles
|
18 |
|
19 |
-
|
20 |
-
from
|
21 |
-
from arbitrage_analyzer import ArbitrageAnalyzer
|
22 |
|
23 |
OPPORTUNITY_THRESHOLD = 0.0015
|
24 |
|
@@ -52,39 +51,37 @@ async def run_arbitrage_detector(price_fetcher, analyzer):
|
|
52 |
print(f"β ERROR in engine loop: {e}")
|
53 |
await asyncio.sleep(15)
|
54 |
|
55 |
-
# --- FastAPI
|
56 |
-
|
57 |
-
app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
|
70 |
-
@app.on_event("shutdown")
|
71 |
-
async def shutdown_event():
|
72 |
-
print("β³ Shutting down engine...")
|
73 |
-
sio.background_task.cancel()
|
74 |
-
await app.state.client.aclose()
|
75 |
-
print("Engine shut down gracefully.")
|
76 |
|
77 |
-
# --- Socket.IO Event Handlers ---
|
78 |
@sio.event
|
79 |
async def connect(sid, environ):
|
80 |
print(f"β
Client connected: {sid}")
|
81 |
|
82 |
@sio.event
|
83 |
async def disconnect(sid):
|
84 |
-
print(f"π₯ Client disconnected: {sid}")
|
85 |
-
|
86 |
-
# --- Mount the apps ---
|
87 |
-
# The primary app is the Socket.IO server, which wraps our FastAPI app.
|
88 |
-
app = socketio.ASGIApp(sio, other_asgi_app=app)
|
89 |
-
# Serve the static files.
|
90 |
-
sio.mount("/", StaticFiles(directory="static", html=True), name="static")
|
|
|
1 |
"""
|
2 |
+
Sentinel Arbitrage Engine - v16.0 FINAL (Correct Object Naming)
|
3 |
|
4 |
+
This is the definitive version with the correct object names to match
|
5 |
+
the startup script, ensuring a successful launch.
|
6 |
"""
|
7 |
import asyncio
|
8 |
import os
|
|
|
16 |
from fastapi import FastAPI
|
17 |
from fastapi.staticfiles import StaticFiles
|
18 |
|
19 |
+
from .price_fetcher import PriceFetcher
|
20 |
+
from .arbitrage_analyzer import ArbitrageAnalyzer
|
|
|
21 |
|
22 |
OPPORTUNITY_THRESHOLD = 0.0015
|
23 |
|
|
|
51 |
print(f"β ERROR in engine loop: {e}")
|
52 |
await asyncio.sleep(15)
|
53 |
|
54 |
+
# --- FastAPI Lifespan (for background task) ---
|
55 |
+
@asynccontextmanager
|
56 |
+
async def lifespan(app: FastAPI):
|
57 |
+
print("π Initializing Sentinel Arbitrage Engine v16.0...")
|
58 |
+
async with httpx.AsyncClient() as client:
|
59 |
+
price_fetcher = PriceFetcher(client)
|
60 |
+
arbitrage_analyzer = ArbitrageAnalyzer(client)
|
61 |
+
sio.background_task = sio.start_background_task(run_arbitrage_detector, price_fetcher, arbitrage_analyzer)
|
62 |
+
print("β
Engine is online and hunting for opportunities.")
|
63 |
+
yield
|
64 |
+
print("β³ Shutting down engine...")
|
65 |
+
sio.background_task.cancel()
|
66 |
+
try: await sio.background_task
|
67 |
+
except asyncio.CancelledError: print("Engine shut down gracefully.")
|
68 |
|
69 |
+
# --- FastAPI App & Socket.IO Mount ---
|
70 |
+
# THE FIX: We name the main FastAPI instance 'app' to match the startup script.
|
71 |
+
app = FastAPI(lifespan=lifespan)
|
72 |
+
|
73 |
+
# The primary app is the Socket.IO server, which wraps our FastAPI app.
|
74 |
+
# The startup script should point to THIS object.
|
75 |
+
app = socketio.ASGIApp(sio, other_asgi_app=app)
|
76 |
+
|
77 |
+
# Serve the static files. This route is now handled by the wrapped app.
|
78 |
+
sio.mount("/", StaticFiles(directory="static", html=True), name="static")
|
79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
|
|
81 |
@sio.event
|
82 |
async def connect(sid, environ):
|
83 |
print(f"β
Client connected: {sid}")
|
84 |
|
85 |
@sio.event
|
86 |
async def disconnect(sid):
|
87 |
+
print(f"π₯ Client disconnected: {sid}")
|
|
|
|
|
|
|
|
|
|
|
|