Skip to content

Commit

Permalink
refactor: if client exists close and reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
mehmetcanay committed Nov 4, 2024
1 parent 1dd4626 commit 1611324
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions datastew/repository/weaviate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import List, Tuple, Union, Optional

import weaviate
from weaviate import WeaviateClient
from weaviate.util import generate_uuid5
from weaviate.classes.query import Filter, QueryReference, MetadataQuery

Expand All @@ -20,10 +21,14 @@ class WeaviateRepository(BaseRepository):

def __init__(self, mode="memory", path=None, port=80, http_port=8079, grpc_port=50050):
self.mode = mode
self.client: Union[None, WeaviateClient] = None
try:
if mode == "memory":
# Check if there is an existing instance of Weaviate client for the default ports
if self._is_port_in_use(http_port) and self._is_port_in_use(grpc_port):
# if the client exists, first close then re-connect
if self.client:
self.client.close()
self.client = weaviate.connect_to_local(port=http_port, grpc_port=grpc_port)
else:
self.client = weaviate.connect_to_embedded(persistence_data_path="db")
Expand All @@ -32,6 +37,9 @@ def __init__(self, mode="memory", path=None, port=80, http_port=8079, grpc_port=
raise ValueError("Path must be provided for disk mode.")
# Check if there is an existing instance of Weaviate client for the default ports
if self._is_port_in_use(http_port) and self._is_port_in_use(grpc_port):
# if the client exists, first close then re-connect
if self.client:
self.client.close()
self.client = weaviate.connect_to_local(port=http_port, grpc_port=grpc_port)
else:
self.client = weaviate.connect_to_embedded(persistence_data_path=path)
Expand Down

0 comments on commit 1611324

Please sign in to comment.