From 15dc7f4552b58319aedad91003bc77adc4463465 Mon Sep 17 00:00:00 2001 From: wassaf shahzad Date: Tue, 26 Mar 2024 01:38:28 +0100 Subject: [PATCH] added feature to automatically change port (#2867) --- reflex/reflex.py | 4 ++-- reflex/utils/processes.py | 33 ++++++++------------------------- 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/reflex/reflex.py b/reflex/reflex.py index e130a17df..a328162fb 100644 --- a/reflex/reflex.py +++ b/reflex/reflex.py @@ -169,10 +169,10 @@ def _run( # If something is running on the ports, ask the user if they want to kill or change it. if frontend and processes.is_process_on_port(frontend_port): - frontend_port = processes.change_or_terminate_port(frontend_port, "frontend") + frontend_port = processes.change_port(frontend_port, "frontend") if backend and processes.is_process_on_port(backend_port): - backend_port = processes.change_or_terminate_port(backend_port, "backend") + backend_port = processes.change_port(backend_port, "backend") # Apply the new ports to the config. if frontend_port != str(config.frontend_port): diff --git a/reflex/utils/processes.py b/reflex/utils/processes.py index 025f22ee7..2f38a141e 100644 --- a/reflex/utils/processes.py +++ b/reflex/utils/processes.py @@ -87,41 +87,24 @@ def kill_process_on_port(port): get_process_on_port(port).kill() # type: ignore -def change_or_terminate_port(port, _type) -> str: - """Terminate or change the port. +def change_port(port: str, _type: str) -> str: + """Change the port. Args: port: The port. _type: The type of the port. Returns: - The new port or the current one. + The new port. - - Raises: - Exit: If the user wants to exit. """ + new_port = str(int(port) + 1) + if is_process_on_port(new_port): + return change_port(new_port, _type) console.info( - f"Something is already running on port [bold underline]{port}[/bold underline]. This is the port the {_type} runs on." + f"The {_type} will run on port [bold underline]{new_port}[/bold underline]." ) - frontend_action = console.ask("Kill or change it?", choices=["k", "c", "n"]) - if frontend_action == "k": - kill_process_on_port(port) - return port - elif frontend_action == "c": - new_port = console.ask("Specify the new port") - - # Check if also the new port is used - if is_process_on_port(new_port): - return change_or_terminate_port(new_port, _type) - else: - console.info( - f"The {_type} will run on port [bold underline]{new_port}[/bold underline]." - ) - return new_port - else: - console.log("Exiting...") - raise typer.Exit() + return new_port def new_process(args, run: bool = False, show_logs: bool = False, **kwargs):