mgbam commited on
Commit
bd955d7
Β·
verified Β·
1 Parent(s): 41259a8

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +30 -33
app/main.py CHANGED
@@ -1,8 +1,8 @@
1
  """
2
- Sentinel Arbitrage Engine - v17.0 FINAL (Flat Structure)
3
 
4
- This definitive version uses a flat project structure and direct imports
5
- to conform to the environment's startup command, ensuring a successful launch.
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
- # --- DIRECT IMPORTS FOR FLAT STRUCTURE ---
20
- from price_fetcher import PriceFetcher
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 App & Lifespan ---
56
- # We now define the main 'app' object first.
57
- app = FastAPI()
 
 
 
 
 
 
 
 
 
 
 
58
 
59
- @app.on_event("startup")
60
- async def startup_event():
61
- print("πŸš€ Initializing Sentinel Arbitrage Engine v17.0...")
62
- # Using the older startup event which is more compatible with some environments
63
- app.state.client = httpx.AsyncClient()
64
- price_fetcher = PriceFetcher(app.state.client)
65
- arbitrage_analyzer = ArbitrageAnalyzer(app.state.client)
66
- # Start the background task
67
- sio.background_task = sio.start_background_task(run_arbitrage_detector, price_fetcher, arbitrage_analyzer)
68
- print("βœ… Engine is online and hunting for opportunities.")
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}")