fix(cli/agent start): Wait for applications to finish starting before returning

- Added a helper function `wait_until_conn_ready(port)` to wait for the benchmark and agent applications to finish starting
- Improved the CLI's own logging (within the `agent start` command)
This commit is contained in:
Reinier van der Leer
2024-02-15 11:26:26 +01:00
parent 6a09a44ef7
commit 52b93dd84e

25
cli.py
View File

@@ -274,12 +274,20 @@ def start(agent_name, no_setup):
if os.path.exists(agent_dir) and os.path.isfile(run_command) and os.path.isfile(run_bench_command):
os.chdir(agent_dir)
if not no_setup:
click.echo(f"⌛ Running setup for agent '{agent_name}'...")
setup_process = subprocess.Popen(["./setup"], cwd=agent_dir)
setup_process.wait()
click.echo()
subprocess.Popen(["./run_benchmark", "serve"], cwd=agent_dir)
click.echo(f"Benchmark Server starting please wait...")
click.echo("⌛ (Re)starting benchmark server...")
wait_until_conn_ready(8080)
click.echo()
subprocess.Popen(["./run"], cwd=agent_dir)
click.echo(f"Agent '{agent_name}' starting please wait...")
click.echo(f"⌛ (Re)starting agent '{agent_name}'...")
wait_until_conn_ready(8000)
click.echo("✅ Agent application started and available on port 8000")
elif not os.path.exists(agent_dir):
click.echo(
click.style(
@@ -889,5 +897,18 @@ def update(agent_name, hash, branch):
)
)
def wait_until_conn_ready(port: int = 8000):
"""Polls localhost:{port} until it is available for connections"""
import time
import socket
while True:
time.sleep(0.5)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
if s.connect_ex(('localhost', port)) == 0:
break
if __name__ == "__main__":
cli()