Skip to content

Commit 9489c3d

Browse files
committed
add check for qdrant port availability
1 parent 4e8a763 commit 9489c3d

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

Diff for: cli.py

+46-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import click
44
import dotenv
5+
import errno
56
import importlib
67
import pathlib
78
import re
89
import requests
910
import shutil
11+
import socket
1012
import subprocess
1113
import typing
1214

@@ -100,6 +102,36 @@ def ping_service(url: str, service_name: str, headers: typing.Dict[str, any] = d
100102
return True
101103

102104

105+
def check_port(port: int) -> bool:
106+
"""
107+
Check if a given port on localhost is available for use.
108+
109+
This function attempts to bind to the specified port on the local machine to determine its availability.
110+
If the port is already in use or an error occurs during the check, it logs an appropriate error message.
111+
112+
Args:
113+
port (int): The port number to check.
114+
115+
Returns:
116+
bool: `True` if the port is available, `False` otherwise.
117+
118+
Raises:
119+
socket.error: Captures errors during socket binding.
120+
"""
121+
is_available = True
122+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
123+
try:
124+
s.bind(("127.0.0.1", port))
125+
except socket.error as e:
126+
if e.errno == errno.EADDRINUSE:
127+
click.echo(click.style(f"ERROR: Port {port} is already in use", fg="red"), err=True)
128+
else:
129+
click.echo(click.style(f"ERROR: An error ocurred when checking port ({port}) availability: {e}", fg="red"), err=True)
130+
is_available = False
131+
s.close()
132+
return is_available
133+
134+
103135
def check_docker(debug: bool = False) -> bool:
104136
"""
105137
Verify that Docker is installed and its daemon is running.
@@ -331,11 +363,20 @@ def setup(
331363

332364
click.echo(click.style(f"(Step {step}/{total_steps}) Setting Vector DB settings...", fg="yellow"))
333365

334-
external_qdrant = click.confirm(
335-
f"Do you have a running QdrantDB instance?",
336-
default=False,
337-
show_default=True
338-
)
366+
while True:
367+
external_qdrant = click.confirm(
368+
f"Do you have a running QdrantDB instance?",
369+
default=False,
370+
show_default=True
371+
)
372+
if not external_qdrant:
373+
# TODO: check port is not in use
374+
port_available = check_port(6333)
375+
if not port_available:
376+
click.echo(click.style(f"Cannot deploy a QdrantDB instance", fg="red"), err=True)
377+
continue
378+
break
379+
339380
qdrantdb_url = "http://127.0.0.1:6333"
340381
if external_qdrant:
341382
while True:

0 commit comments

Comments
 (0)