Skip to content

Commit

Permalink
fixed event loops issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mkashwin committed Sep 23, 2024
1 parent 19de601 commit d5d1e59
Show file tree
Hide file tree
Showing 2 changed files with 301 additions and 296 deletions.
18 changes: 11 additions & 7 deletions 07_uns_graphql/src/uns_graphql/backend/graphdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import logging

from neo4j import AsyncDriver, AsyncGraphDatabase, Record
from neo4j.exceptions import Neo4jError

from uns_graphql.graphql_config import GraphDBConfig

Expand Down Expand Up @@ -58,15 +59,18 @@ async def get_graphdb_driver(cls, retry: int = 0) -> AsyncDriver:
AsyncDriver: The Neo4j async driver.
"""
LOGGER.debug("GraphDB driver requested")
if cls._graphdb_driver is None:
LOGGER.info("Creating a new GraphDB driver")
cls._graphdb_driver = AsyncGraphDatabase.driver(
uri=GraphDBConfig.conn_url, auth=(GraphDBConfig.user, GraphDBConfig.password), database=GraphDBConfig.database
)
try:
if cls._graphdb_driver is None:
LOGGER.info("Creating a new GraphDB driver")
cls._graphdb_driver = AsyncGraphDatabase.driver(
uri=GraphDBConfig.conn_url,
auth=(GraphDBConfig.user, GraphDBConfig.password),
database=GraphDBConfig.database,
)

await cls._graphdb_driver.verify_connectivity()
LOGGER.debug("GraphDB driver connectivity verified")
except Exception as ex:
except Neo4jError as ex:
LOGGER.error("Failed to verify GraphDB driver connectivity: %s", str(ex), stack_info=True, exc_info=True)
# In case of connectivity failure, close the existing driver and create a new one
await cls.release_graphdb_driver()
Expand All @@ -91,7 +95,7 @@ async def release_graphdb_driver(cls):
try:
await cls._graphdb_driver.close()
LOGGER.info("GraphDB driver closed successfully")
except Exception as ex:
except Neo4jError as ex:
LOGGER.error("Error closing the GraphDB driver: %s", str(ex), stack_info=True, exc_info=True)
finally:
cls._graphdb_driver = None
Expand Down
Loading

0 comments on commit d5d1e59

Please sign in to comment.