feat: add cold debugging
Browse files
app.py
CHANGED
@@ -45,28 +45,59 @@ def greet_json():
|
|
45 |
"checks": {}
|
46 |
}
|
47 |
return health_status
|
48 |
-
|
|
|
49 |
@app.post("/v1/generateTripPlan", response_model=PlanResponse)
|
50 |
def generate_trip_plan(request: PlanRequest):
|
51 |
data_importer.coldStartDatabase()
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
|
72 |
@app.post("/v1/addYoutubeLink", response_model=YoutubeLinkResponse)
|
|
|
45 |
"checks": {}
|
46 |
}
|
47 |
return health_status
|
48 |
+
MAX_RETRIES = 3
|
49 |
+
RETRY_DELAY = 2 # seconds
|
50 |
@app.post("/v1/generateTripPlan", response_model=PlanResponse)
|
51 |
def generate_trip_plan(request: PlanRequest):
|
52 |
data_importer.coldStartDatabase()
|
53 |
+
|
54 |
+
for attempt in range(MAX_RETRIES):
|
55 |
+
try:
|
56 |
+
logger.info(f"Generating trip plan - attempt {attempt + 1}/{MAX_RETRIES}")
|
57 |
+
trip_plan = asyncio.run(agent.query_with_rag(request))
|
58 |
+
return PlanResponse(
|
59 |
+
tripOverview=trip_plan.tripOverview,
|
60 |
+
query_params=request,
|
61 |
+
retrieved_data=trip_plan.retrieved_data,
|
62 |
+
trip_plan=trip_plan.trip_plan,
|
63 |
+
preparation=trip_plan.preparation,
|
64 |
+
meta={
|
65 |
+
"status": "success",
|
66 |
+
"timestamp": datetime.utcnow().isoformat(),
|
67 |
+
"attempt": attempt + 1
|
68 |
+
}
|
69 |
+
)
|
70 |
+
except (asyncio.TimeoutError, Timeout, ConnectionError) as timeout_error:
|
71 |
+
logger.warning(f"Timeout error on attempt {attempt + 1}: {timeout_error}")
|
72 |
+
|
73 |
+
# If this was the last attempt, raise the error
|
74 |
+
if attempt == MAX_RETRIES - 1:
|
75 |
+
logger.error(f"All {MAX_RETRIES} attempts failed due to timeout")
|
76 |
+
raise HTTPException(
|
77 |
+
status_code=504, # Gateway Timeout
|
78 |
+
detail={
|
79 |
+
"error": "Request timeout",
|
80 |
+
"message": f"Failed to generate trip plan after {MAX_RETRIES} attempts",
|
81 |
+
"details": "The service is experiencing high load. Please try again later."
|
82 |
+
}
|
83 |
+
)
|
84 |
+
|
85 |
+
# Wait before retrying
|
86 |
+
logger.info(f"Retrying in {RETRY_DELAY} seconds...")
|
87 |
+
time.sleep(RETRY_DELAY)
|
88 |
+
|
89 |
+
except Exception as e:
|
90 |
+
# For non-timeout errors, don't retry
|
91 |
+
logger.error(f"Non-timeout error in generate_trip_plan: {e}")
|
92 |
+
raise HTTPException(
|
93 |
+
status_code=500,
|
94 |
+
detail={
|
95 |
+
"error": "Internal server error",
|
96 |
+
"message": "Failed to generate trip plan",
|
97 |
+
"details": str(e),
|
98 |
+
"attempt": attempt + 1
|
99 |
+
}
|
100 |
+
)
|
101 |
|
102 |
|
103 |
@app.post("/v1/addYoutubeLink", response_model=YoutubeLinkResponse)
|