fix: fetch klines synchronously before returning response
The background task wasn't completing before the response was returned. Now we fetch klines synchronously (await) before returning the simulation.
This commit is contained in:
@@ -117,6 +117,12 @@ async def start_simulation(
|
|||||||
settings = get_settings()
|
settings = get_settings()
|
||||||
simulation_id = str(uuid.uuid4())
|
simulation_id = str(uuid.uuid4())
|
||||||
|
|
||||||
|
# Create AVE client for klines fetching
|
||||||
|
ave_client = AveCloudClient(
|
||||||
|
api_key=settings.AVE_API_KEY,
|
||||||
|
plan=settings.AVE_API_PLAN,
|
||||||
|
)
|
||||||
|
|
||||||
simulation_config = {
|
simulation_config = {
|
||||||
"bot_id": bot_id,
|
"bot_id": bot_id,
|
||||||
"token": config.token,
|
"token": config.token,
|
||||||
@@ -145,42 +151,30 @@ async def start_simulation(
|
|||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(simulation)
|
db.refresh(simulation)
|
||||||
|
|
||||||
# Fetch klines synchronously first so user can see the chart immediately
|
# Fetch klines SYNCHRONOUSLY so user can see chart immediately
|
||||||
# Then run simulation in background for signal processing
|
try:
|
||||||
async def fetch_klines_and_run():
|
token_id = f"{config.token}-{config.chain}"
|
||||||
try:
|
klines_data = await ave_client.get_klines(
|
||||||
# Fetch klines for chart display
|
token_id,
|
||||||
token_id = f"{config.token}-{config.chain}"
|
interval=config.kline_interval,
|
||||||
ave_client = AveCloudClient(
|
limit=500
|
||||||
api_key=settings.AVE_API_KEY,
|
)
|
||||||
plan=settings.AVE_API_PLAN,
|
klines_for_chart = [
|
||||||
)
|
{"time": k.get("time"), "close": k.get("close")}
|
||||||
klines_data = await ave_client.get_klines(
|
for k in sorted(klines_data, key=lambda x: x.get("time", 0))
|
||||||
token_id,
|
]
|
||||||
interval=config.kline_interval,
|
# Update simulation with klines
|
||||||
limit=500
|
simulation.klines = klines_for_chart
|
||||||
)
|
db.commit()
|
||||||
klines_for_chart = [
|
db.refresh(simulation)
|
||||||
{"time": k.get("time"), "close": k.get("close")}
|
logger.info(f"Fetched {len(klines_for_chart)} klines for simulation {simulation_id}")
|
||||||
for k in sorted(klines_data, key=lambda x: x.get("time", 0))
|
except Exception as e:
|
||||||
]
|
logger.error(f"Failed to fetch klines: {e}")
|
||||||
|
|
||||||
# Save klines to DB immediately
|
# Run simulation in background for signal processing
|
||||||
db = SessionLocal()
|
background_tasks.add_task(
|
||||||
try:
|
run_simulation_sync, simulation_id, str(settings.DATABASE_URL), bot_id, simulation_config
|
||||||
sim = db.query(Simulation).filter(Simulation.id == simulation_id).first()
|
)
|
||||||
if sim:
|
|
||||||
sim.klines = klines_for_chart
|
|
||||||
db.commit()
|
|
||||||
finally:
|
|
||||||
db.close()
|
|
||||||
|
|
||||||
# Now run the full simulation in background
|
|
||||||
run_simulation_sync(simulation_id, str(settings.DATABASE_URL), bot_id, simulation_config)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Failed to fetch klines: {e}")
|
|
||||||
|
|
||||||
background_tasks.add_task(fetch_klines_and_run)
|
|
||||||
|
|
||||||
return simulation
|
return simulation
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user