|
1 | 1 | import json |
2 | 2 | import logging |
3 | 3 | import socket |
| 4 | +import time |
4 | 5 | from typing import Any, Dict, List, Literal, Optional, Sequence, Union |
5 | 6 |
|
6 | 7 | import weaviate |
7 | 8 | from typing_extensions import deprecated |
8 | 9 | from weaviate import WeaviateClient |
9 | 10 | from weaviate.classes.query import Filter, MetadataQuery, QueryReference |
10 | 11 | from weaviate.collections import Collection |
| 12 | +from weaviate.exceptions import WeaviateStartUpError |
11 | 13 | from weaviate.util import generate_uuid5 |
12 | 14 |
|
13 | 15 | from datastew.embedding import Vectorizer |
@@ -772,19 +774,33 @@ def _is_port_in_use(port) -> bool: |
772 | 774 | return s.connect_ex(("localhost", port)) == 0 |
773 | 775 |
|
774 | 776 | def _connect_to_memory(self, path: str, http_port: int, grpc_port: int) -> WeaviateClient: |
775 | | - try: |
776 | | - if path is None: |
777 | | - raise ValueError("Path must be provided for disk mode.") |
778 | | - if self._is_port_in_use(http_port) and self._is_port_in_use(grpc_port): |
| 777 | + if path is None: |
| 778 | + raise ValueError("Path must be provided for disk mode.") |
| 779 | + |
| 780 | + # If there's already a Weaviate instance listening; just connect to it. |
| 781 | + if self._is_port_in_use(http_port) and self._is_port_in_use(grpc_port): |
| 782 | + try: |
779 | 783 | return weaviate.connect_to_local(port=http_port, grpc_port=grpc_port, headers=self.headers) |
780 | | - else: |
| 784 | + except Exception as e: |
| 785 | + raise ConnectionError(f"Failed to initialize Weaviate client (embedded): {e}") from e |
| 786 | + |
| 787 | + last_exc: Exception | None = None |
| 788 | + for _ in range(3): |
| 789 | + try: |
781 | 790 | return weaviate.connect_to_embedded( |
782 | 791 | persistence_data_path=path, |
783 | 792 | headers=self.headers, |
784 | 793 | environment_variables={"ENABLE_MODULES": "text2vec-ollama"}, |
785 | 794 | ) |
786 | | - except Exception as e: |
787 | | - raise ConnectionError(f"Failed to initialize Weaviate client: {e}") |
| 795 | + except WeaviateStartUpError as e: |
| 796 | + last_exc = e |
| 797 | + time.sleep(2.0) |
| 798 | + except Exception as e: |
| 799 | + raise ConnectionError(f"Failed to initialize Weaviate client (embedded): {e}") from e |
| 800 | + |
| 801 | + raise ConnectionError( |
| 802 | + f"Failed to initalize Weaviate client (embedded) after retries: {last_exc}" |
| 803 | + ) from last_exc |
788 | 804 |
|
789 | 805 | def _connect_to_remote(self, path: str, port: int) -> WeaviateClient: |
790 | 806 | try: |
|
0 commit comments