Skip to content
This repository has been archived by the owner on Dec 2, 2023. It is now read-only.

Commit

Permalink
fix: add database timeout and error handling
Browse files Browse the repository at this point in the history
- set database timeout to 5 seconds
- add extra error handling
  • Loading branch information
zsltg committed Nov 5, 2021
1 parent 874be58 commit e1deae7
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
MONGODB_URL=localhost
MONGODB_PORT=27017
MONGODB_PORT=27017
MONGODB_TIMEOUT=5000
1 change: 1 addition & 0 deletions dataapi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Settings(pydantic.BaseSettings):

mongodb_url: str
mongodb_port: str
mongodb_timeout: str

class Config:
"""Settings Configuration"""
Expand Down
11 changes: 8 additions & 3 deletions dataapi/controllers/consent_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
35 changes: 27 additions & 8 deletions dataapi/controllers/dialog_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion dataapi/routers/consent_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions dataapi/routers/data_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion dataapi/services/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e1deae7

Please sign in to comment.