(endpoint, server="gremlin")
| 2087 | |
| 2088 | |
| 2089 | def check_server_ready(endpoint, server="gremlin"): |
| 2090 | def _check_gremlin_task(endpoint): |
| 2091 | from gremlin_python.driver.client import Client |
| 2092 | |
| 2093 | if "MY_POD_NAME" in os.environ: |
| 2094 | # inner kubernetes env |
| 2095 | if endpoint == "localhost" or endpoint == "127.0.0.1": |
| 2096 | # now, used in macOS with docker-desktop kubernetes cluster, |
| 2097 | # which external ip is 'localhost' when service type is 'LoadBalancer' |
| 2098 | logger.info("In kubernetes env, gremlin server is ready.") |
| 2099 | return True |
| 2100 | |
| 2101 | try: |
| 2102 | client = Client(f"ws://{endpoint}/gremlin", "g") |
| 2103 | # May throw |
| 2104 | client.submit("g.V().limit(1)").all().result() |
| 2105 | logger.info("Gremlin server is ready.") |
| 2106 | finally: |
| 2107 | try: |
| 2108 | client.close() |
| 2109 | except: # noqa: E722 |
| 2110 | pass |
| 2111 | return True |
| 2112 | |
| 2113 | def _check_cypher_task(endpoint): |
| 2114 | from neo4j import GraphDatabase |
| 2115 | |
| 2116 | if "MY_POD_NAME" in os.environ: |
| 2117 | # inner kubernetes env |
| 2118 | if endpoint == "localhost" or endpoint == "127.0.0.1": |
| 2119 | logger.info("In kubernetes env, cypher server is ready.") |
| 2120 | return True |
| 2121 | |
| 2122 | try: |
| 2123 | logger.debug("Try to connect to cypher server.") |
| 2124 | driver = GraphDatabase.driver(f"neo4j://{endpoint}", auth=("", "")) |
| 2125 | # May throw |
| 2126 | driver.verify_connectivity() |
| 2127 | logger.info("Checked connectivity to cypher server.") |
| 2128 | finally: |
| 2129 | try: |
| 2130 | driver.close() |
| 2131 | except: # noqa: E722 |
| 2132 | pass |
| 2133 | return True |
| 2134 | |
| 2135 | executor = ThreadPoolExecutor(max_workers=20) |
| 2136 | |
| 2137 | begin_time = time.time() |
| 2138 | while True: |
| 2139 | if server == "gremlin": |
| 2140 | t = executor.submit(_check_gremlin_task, endpoint) |
| 2141 | elif server == "cypher": |
| 2142 | t = executor.submit(_check_cypher_task, endpoint) |
| 2143 | else: |
| 2144 | raise ValueError( |
| 2145 | f"Unsupported server type: {server} other than 'gremlin' or 'cypher'" |
| 2146 | ) |
no test coverage detected