Allow the user to change ports (#417)

* Allow the user to change the app port at runtime

* Allow the user to change the app port (backend and frontend) at runtime
This commit is contained in:
Francesco Ambrosini 2023-02-01 06:21:44 +01:00 committed by GitHub
parent 6c2f4deb70
commit ea28f336da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 13 deletions

View File

@ -65,12 +65,12 @@ def run(
frontend_port = utils.get_config().port if port is None else port frontend_port = utils.get_config().port if port is None else port
backend_port = utils.get_api_port() backend_port = utils.get_api_port()
# If something is running on the ports, ask the user if they want to kill it. # If something is running on the ports, ask the user if they want to kill or change it.
if utils.is_process_on_port(frontend_port): if utils.is_process_on_port(frontend_port):
utils.terminate_port(frontend_port, "frontend") frontend_port = utils.change_or_terminate_port(frontend_port, "frontend")
if utils.is_process_on_port(backend_port): if utils.is_process_on_port(backend_port):
utils.terminate_port(backend_port, "backend") backend_port = utils.change_or_terminate_port(backend_port, "backend")
# Check that the app is initialized. # Check that the app is initialized.
if frontend and not utils.is_initialized(): if frontend and not utils.is_initialized():
@ -101,9 +101,9 @@ def run(
# Run the frontend and backend. # Run the frontend and backend.
try: try:
if frontend: if frontend:
frontend_cmd(app.app, Path.cwd(), port) frontend_cmd(app.app, Path.cwd(), frontend_port)
if backend: if backend:
backend_cmd(app.__name__, loglevel=loglevel) backend_cmd(app.__name__, port=int(backend_port), loglevel=loglevel)
finally: finally:
utils.kill_process_on_port(frontend_port) utils.kill_process_on_port(frontend_port)
utils.kill_process_on_port(backend_port) utils.kill_process_on_port(backend_port)

View File

@ -639,53 +639,72 @@ def kill_process_on_port(port):
get_process_on_port(port).kill() # type: ignore get_process_on_port(port).kill() # type: ignore
def terminate_port(port, _type): def change_or_terminate_port(port, _type) -> str:
"""Terminate the port. """Terminate or change the port.
Args: Args:
port: The port. port: The port.
_type: The type of the port. _type: The type of the port.
Returns:
The new port or the current one.
""" """
console.print( console.print(
f"Something is already running on port [bold underline]{port}[/bold underline]. This is the port the {_type} runs on." f"Something is already running on port [bold underline]{port}[/bold underline]. This is the port the {_type} runs on."
) )
frontend_action = Prompt.ask("Kill it?", choices=["y", "n"]) frontend_action = Prompt.ask("Kill or change it?", choices=["k", "c", "n"])
if frontend_action == "y": if frontend_action == "k":
kill_process_on_port(port) kill_process_on_port(port)
return port
elif frontend_action == "c":
new_port = Prompt.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.print(
f"The {_type} will run on port [bold underline]{new_port}[/bold underline]."
)
return new_port
else: else:
console.print("Exiting...") console.print("Exiting...")
sys.exit() sys.exit()
def run_backend(app_name: str, loglevel: constants.LogLevel = constants.LogLevel.ERROR): def run_backend(
app_name: str, port: int, loglevel: constants.LogLevel = constants.LogLevel.ERROR
):
"""Run the backend. """Run the backend.
Args: Args:
app_name: The app name. app_name: The app name.
port: The app port
loglevel: The log level. loglevel: The log level.
""" """
uvicorn.run( uvicorn.run(
f"{app_name}:{constants.APP_VAR}.{constants.API_VAR}", f"{app_name}:{constants.APP_VAR}.{constants.API_VAR}",
host=constants.BACKEND_HOST, host=constants.BACKEND_HOST,
port=get_api_port(), port=port,
log_level=loglevel, log_level=loglevel,
reload=True, reload=True,
) )
def run_backend_prod( def run_backend_prod(
app_name: str, loglevel: constants.LogLevel = constants.LogLevel.ERROR app_name: str, port: int, loglevel: constants.LogLevel = constants.LogLevel.ERROR
): ):
"""Run the backend. """Run the backend.
Args: Args:
app_name: The app name. app_name: The app name.
port: The app port
loglevel: The log level. loglevel: The log level.
""" """
num_workers = get_num_workers() num_workers = get_num_workers()
command = constants.RUN_BACKEND_PROD + [ command = constants.RUN_BACKEND_PROD + [
"--bind", "--bind",
f"0.0.0.0:{get_api_port()}", f"0.0.0.0:{port}",
"--workers", "--workers",
str(num_workers), str(num_workers),
"--threads", "--threads",