Skip to content

Commit

Permalink
Set ACTIVE_PORT once an open port has been found
Browse files Browse the repository at this point in the history
Subsequent calls to `get_open_port()` will use the open port that was initially found on app startup
  • Loading branch information
ryderwishart committed Jun 19, 2023
1 parent 7053c4a commit e0f63d3
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,21 @@ def seed_uuid(seed: str) -> str:

def is_port_in_use(port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(("localhost", port)) == 0
return s.connect_ex(("localhost", port)) == 0 # TODO: use base URL from env


def get_open_port():
port = os.getenv("PORT", "5000")
# check ACTIVE_PORT first
port = os.getenv("ACTIVE_PORT", None)
if port is not None:
print(f"Active port found: {port}")
return int(port)

port = int(os.getenv("PORT", "5023"))
while is_port_in_use(port):
print(f"Port {port} is in use, trying next port...")
port += 1

# set ACTIVE_PORT to the port we found
os.environ["ACTIVE_PORT"] = str(port)
return port

0 comments on commit e0f63d3

Please sign in to comment.