Skip to content

Commit 8eb451e

Browse files
committed
feat(backend): refactor list_roadmaps to use async executor for database calls
- Wrap synchronous database call in an executor to prevent blocking the event loop. - Enhance error handling to log exceptions and raise HTTPException with detailed messages. - Improve logging for better traceability of the list_roadmaps function.
1 parent b8a7853 commit 8eb451e

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

commitly-backend/app/api/roadmap.py

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,35 @@ async def list_roadmaps(
6262
logger = logging.getLogger(__name__)
6363
try:
6464
logger.info(f"list_roadmaps: Starting with page={page}, page_size={page_size}")
65-
items, total_count = await service.list_catalog(
66-
page=page,
67-
page_size=page_size,
68-
language=language,
69-
tag=tag,
70-
difficulty=difficulty,
71-
min_rating=min_rating,
72-
min_views=min_views,
73-
min_syncs=min_syncs,
74-
sort=sort,
75-
)
65+
66+
# Call service method - wrap sync database call in executor to prevent blocking
67+
import asyncio
68+
69+
# Create a synchronous wrapper function
70+
def call_list_catalog():
71+
try:
72+
logger.info("list_roadmaps: Calling result_store.list_catalog")
73+
return service._result_store.list_catalog(
74+
page=page,
75+
page_size=page_size,
76+
language=language,
77+
tag=tag,
78+
difficulty=difficulty,
79+
min_rating=min_rating,
80+
min_views=min_views,
81+
min_syncs=min_syncs,
82+
sort=sort,
83+
)
84+
except Exception as e:
85+
logger.error(
86+
f"list_roadmaps: Error in result_store.list_catalog: {e}",
87+
exc_info=True,
88+
)
89+
raise
90+
91+
loop = asyncio.get_event_loop()
92+
items, total_count = await loop.run_in_executor(None, call_list_catalog)
93+
7694
logger.info(f"list_roadmaps: Retrieved {len(items)} items, total={total_count}")
7795

7896
total_pages = math.ceil(total_count / page_size) if total_count > 0 else 0
@@ -85,8 +103,14 @@ async def list_roadmaps(
85103
total_pages=total_pages,
86104
)
87105
except Exception as e:
88-
logger.error(f"list_roadmaps: Error - {type(e).__name__}: {e}", exc_info=True)
89-
raise
106+
logger.error(
107+
f"list_roadmaps: Error - {type(e).__name__}: {e}",
108+
exc_info=True,
109+
)
110+
raise HTTPException(
111+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
112+
detail=f"Failed to retrieve catalog: {str(e)}",
113+
) from e
90114

91115

92116
@router.get("/cached/{owner}/{repo}", response_model=RoadmapResponse)

0 commit comments

Comments
 (0)