|
1 | 1 | from typing import Annotated |
2 | 2 |
|
| 3 | +import backoff |
3 | 4 | from fastapi import APIRouter, Depends, File, Response, UploadFile |
4 | 5 |
|
5 | 6 | from src.app.api.dependencies import get_settings |
|
34 | 35 | settings = get_settings() |
35 | 36 |
|
36 | 37 |
|
| 38 | +def backoff_hdlr(details): |
| 39 | + logger.info( |
| 40 | + "Backing off {wait:0.1f} seconds after {tries} tries " |
| 41 | + "calling function {target} with args {args} and kwargs " |
| 42 | + "{kwargs}".format(**details) |
| 43 | + ) |
| 44 | + |
| 45 | + |
37 | 46 | @router.post("/files/content") |
| 47 | +@backoff.on_exception( |
| 48 | + wait_gen=backoff.expo, |
| 49 | + exception=Exception, |
| 50 | + logger=logger, |
| 51 | + max_tries=3, |
| 52 | + max_time=180, |
| 53 | + jitter=backoff.random_jitter, |
| 54 | + on_backoff=backoff_hdlr, |
| 55 | + factor=2, |
| 56 | +) |
38 | 57 | async def extract_files_content( |
39 | 58 | files: Annotated[list[UploadFile], File()], |
40 | 59 | response: Response, |
@@ -65,7 +84,7 @@ async def extract_files_content( |
65 | 84 | except Exception as e: |
66 | 85 | logger.error(f"Error in extractor schema: {e}") |
67 | 86 | response.status_code = 204 |
68 | | - return None |
| 87 | + raise e |
69 | 88 |
|
70 | 89 |
|
71 | 90 | @router.post("/search_extracts") |
@@ -208,6 +227,16 @@ async def tutor_search( |
208 | 227 | return resp |
209 | 228 |
|
210 | 229 |
|
| 230 | +@backoff.on_exception( |
| 231 | + wait_gen=backoff.expo, |
| 232 | + exception=Exception, |
| 233 | + logger=logger, |
| 234 | + max_tries=3, |
| 235 | + max_time=180, |
| 236 | + jitter=backoff.random_jitter, |
| 237 | + on_backoff=backoff_hdlr, |
| 238 | + factor=2, |
| 239 | +) |
211 | 240 | @router.post("/syllabus") |
212 | 241 | async def create_syllabus( |
213 | 242 | body: TutorSyllabusRequest, lang: str = "en" |
@@ -260,6 +289,16 @@ async def create_syllabus( |
260 | 289 | """ |
261 | 290 |
|
262 | 291 |
|
| 292 | +@backoff.on_exception( |
| 293 | + wait_gen=backoff.expo, |
| 294 | + exception=Exception, |
| 295 | + logger=logger, |
| 296 | + max_tries=3, |
| 297 | + max_time=180, |
| 298 | + jitter=backoff.random_jitter, |
| 299 | + on_backoff=backoff_hdlr, |
| 300 | + factor=2, |
| 301 | +) |
263 | 302 | @router.post("/syllabus/feedback") |
264 | 303 | async def handle_syllabus_feedback( |
265 | 304 | body: SyllabusFeedback, chatfactory=Depends(get_chat_service) |
@@ -300,3 +339,4 @@ async def handle_syllabus_feedback( |
300 | 339 |
|
301 | 340 | except Exception as e: |
302 | 341 | logger.error(f"Error in chat schema: {e}") |
| 342 | + raise e |
0 commit comments