Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed behaviour to raise error instead of using default db when re… #157

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions common/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import mongomock
import grpc
from ensembl.production.metadata.grpc import ensembl_metadata_pb2_grpc
from graphql_service.resolver.exceptions import (
GenomeNotFoundError,
FailedToConnectToGrpc,
)

from common.utils import process_release_version

Expand All @@ -36,9 +40,10 @@ def __init__(self, config):
self.config = config
self.mongo_client = MongoDbClient.connect_mongo(self.config)

def get_database_conn(self, grpc_model, uuid, force_grpc=False):
def get_database_conn(self, grpc_model, uuid):
grpc_response = None
chosen_db = self.config.get("mongo_default_db")

chosen_db = None
# Try to connect to gRPC
try:
grpc_response = grpc_model.get_release_by_genome_uuid(uuid)
Expand All @@ -47,23 +52,21 @@ def get_database_conn(self, grpc_model, uuid, force_grpc=False):
logger.debug(
"[get_database_conn] Couldn't connect to gRPC Host: %s", grpc_exp
)
raise FailedToConnectToGrpc(
"Internal server error: Couldn't connect to gRPC Host"
)

if force_grpc:
if grpc_response and grpc_response.release_version:
chosen_db = process_release_version(grpc_response)
else:
if grpc_response and grpc_response.release_version:
chosen_db = process_release_version(grpc_response)
else:
# chosen_db value will fall back to the default value, which is 'mongo_default_db' that is in the config
# if force_grpc is not True
logger.warning(
"[get_database_conn] Falling back to the default Mongo DB: '%s'",
chosen_db,
)

logger.debug("[get_database_conn] Connected to '%s' MongoDB", chosen_db)
data_database_connection = self.mongo_client[chosen_db]
return data_database_connection
logger.warning("[get_database_conn] Release not found")
raise GenomeNotFoundError({"genome_id": uuid})

if chosen_db is not None:
logger.debug("[get_database_conn] Connected to '%s' MongoDB", chosen_db)
data_database_connection = self.mongo_client[chosen_db]
return data_database_connection
raise GenomeNotFoundError({"genome_id": uuid})

@staticmethod
def connect_mongo(config):
Expand Down
5 changes: 3 additions & 2 deletions graphql_service/resolver/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def __init__(self, message: str):
super().__init__(message)


class FailedToConnectToGrpc(grpc.RpcError):
class FailedToConnectToGrpc(GraphQLError):
"""
Exception raised when there is gRPC connection issue.
"""
Expand All @@ -246,4 +246,5 @@ def __init__(self, message: str):
Args:
message: The error message describing the issue.
"""
super().__init__(message)
self.extensions = {"code": "SERVER_CONNECTION_FAILED"}
super().__init__(message, extensions=self.extensions)
Loading