diff --git a/.env b/.env index 6599ab3..f0a7929 100644 --- a/.env +++ b/.env @@ -1,2 +1,3 @@ MONGODB_URL=localhost -MONGODB_PORT=27017 \ No newline at end of file +MONGODB_PORT=27017 +MONGODB_TIMEOUT=5000 \ No newline at end of file diff --git a/dataapi/config.py b/dataapi/config.py index c039248..59ed32e 100644 --- a/dataapi/config.py +++ b/dataapi/config.py @@ -16,6 +16,7 @@ class Settings(pydantic.BaseSettings): mongodb_url: str mongodb_port: str + mongodb_timeout: str class Config: """Settings Configuration""" diff --git a/dataapi/controllers/consent_controller.py b/dataapi/controllers/consent_controller.py index d52121f..d9cdc92 100644 --- a/dataapi/controllers/consent_controller.py +++ b/dataapi/controllers/consent_controller.py @@ -13,9 +13,14 @@ async def record_consent( db: motor_asyncio.AsyncIOMotorClient, dialog_id: str, consent: bool ) -> utils.OrjsonResponse: if consent: - result = await db.chatbot.dialog.update_one( - {"_id": dialog_id}, {"$set": {"consent_received": consent}} - ) + try: + result = await db.chatbot.dialog.update_one( + {"_id": dialog_id}, {"$set": {"consent_received": consent}} + ) + except pymongo.errors.ServerSelectionTimeoutError: + return utils.OrjsonResponse( + status_code=fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR + ) if result.matched_count: return utils.OrjsonResponse(status_code=fastapi.status.HTTP_200_OK) else: diff --git a/dataapi/controllers/dialog_controller.py b/dataapi/controllers/dialog_controller.py index 9cdaa5e..bcb8302 100644 --- a/dataapi/controllers/dialog_controller.py +++ b/dataapi/controllers/dialog_controller.py @@ -25,8 +25,13 @@ async def fetch_dialogs( query["$and"].append({"language": language}) if customer_id: query["$and"].append({"customer_id": customer_id}) - count = await db.chatbot.dialog.count_documents(query) - cursor = db.chatbot.dialog.find(query) + try: + count = await db.chatbot.dialog.count_documents(query) + cursor = db.chatbot.dialog.find(query) + except pymongo.errors.ServerSelectionTimeoutError: + return utils.OrjsonResponse( + status_code=fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR + ) cursor.sort("date", -1).skip(offset).limit(limit) hits = await cursor.to_list(length=None) params = fastapi_pagination.LimitOffsetParams(limit=limit, offset=0) @@ -39,9 +44,14 @@ async def fetch_dialogs( async def fetch_dialog( db: motor_asyncio.AsyncIOMotorClient, customer_id: str, dialog_id: str ) -> utils.OrjsonResponse: - dialog_entry = await db.chatbot.dialog.find_one( - {"_id": dialog_id, "customer_id": customer_id} - ) + try: + dialog_entry = await db.chatbot.dialog.find_one( + {"_id": dialog_id, "customer_id": customer_id} + ) + except pymongo.errors.ServerSelectionTimeoutError: + return utils.OrjsonResponse( + status_code=fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR + ) if dialog_entry: if dialog_entry["consent_received"]: return utils.OrjsonResponse( @@ -60,6 +70,10 @@ async def create_dialog( new_dialog = await db.chatbot.dialog.insert_one(dialog_entry) except pymongo.errors.DuplicateKeyError: return utils.OrjsonResponse(status_code=fastapi.status.HTTP_409_CONFLICT) + except pymongo.errors.ServerSelectionTimeoutError: + return utils.OrjsonResponse( + status_code=fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR + ) created_dialog = await db.chatbot.dialog.find_one({"_id": new_dialog.inserted_id}) return utils.OrjsonResponse( status_code=fastapi.status.HTTP_201_CREATED, content=created_dialog @@ -69,9 +83,14 @@ async def create_dialog( async def remove_dialog( db: motor_asyncio.AsyncIOMotorClient, customer_id: str, dialog_id: str ) -> utils.OrjsonResponse: - result = await db.chatbot.dialog.delete_one( - {"_id": dialog_id, "customer_id": customer_id} - ) + try: + result = await db.chatbot.dialog.delete_one( + {"_id": dialog_id, "customer_id": customer_id} + ) + except pymongo.errors.ServerSelectionTimeoutError: + return utils.OrjsonResponse( + status_code=fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR + ) if result.deleted_count: return utils.OrjsonResponse(status_code=fastapi.status.HTTP_200_OK) else: diff --git a/dataapi/routers/consent_router.py b/dataapi/routers/consent_router.py index 6d6d669..40bcf2a 100644 --- a/dataapi/routers/consent_router.py +++ b/dataapi/routers/consent_router.py @@ -15,7 +15,7 @@ @router.post( "/consents/{dialog_id}", response_description="Record consent", - responses={404: {"model": None}}, + responses={404: {"model": None}, 500: {"model": None}}, ) async def record_consent( dialog_id: str, diff --git a/dataapi/routers/data_router.py b/dataapi/routers/data_router.py index 8ae2c1b..a1c0da9 100644 --- a/dataapi/routers/data_router.py +++ b/dataapi/routers/data_router.py @@ -17,6 +17,7 @@ @router.get( "/data/", response_model=fastapi_pagination.LimitOffsetPage[dialog_model.DialogModel], + responses={500: {"model": None}}, ) async def fetch_dialogs( language: typing.Optional[str] = None, @@ -40,7 +41,7 @@ async def fetch_dialogs( @router.get( "/data/{customer_id}/{dialog_id}", - responses={403: {"model": None}, 404: {"model": None}}, + responses={403: {"model": None}, 404: {"model": None}, 500: {"model": None}}, ) async def fetch_dialog( customer_id: str, @@ -61,7 +62,7 @@ async def fetch_dialog( response_description="Add new dialog", response_model=dialog_model.DialogBaseModel, status_code=201, - responses={409: {"model": None}}, + responses={409: {"model": None}, 500: {"model": None}}, ) async def create_dialog( customer_id: str, @@ -89,7 +90,7 @@ async def create_dialog( @router.delete( "/data/{customer_id}/{dialog_id}", - responses={404: {"model": None}}, + responses={404: {"model": None}, 500: {"model": None}}, ) async def remove_dialog( customer_id: str, diff --git a/dataapi/services/mongodb.py b/dataapi/services/mongodb.py index 213a9bd..9edd763 100644 --- a/dataapi/services/mongodb.py +++ b/dataapi/services/mongodb.py @@ -19,7 +19,8 @@ async def get_database() -> motor_asyncio.AsyncIOMotorClient: async def connect(): db.client = motor_asyncio.AsyncIOMotorClient( - str("mongodb://{}:{}".format(settings.mongodb_url, settings.mongodb_port)) + str("mongodb://{}:{}".format(settings.mongodb_url, settings.mongodb_port)), + serverSelectionTimeoutMS=settings.mongodb_timeout, ) diff --git a/docker-compose.yml b/docker-compose.yml index 9c96570..88703d2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,6 +10,7 @@ services: environment: MONGODB_URL: db MONGODB_PORT: 27017 + MONGODB_TIMEOUT: 5000 db: image: mongo:5.0.3 container_name: dataapi-db