Skip to content

Commit fe3f835

Browse files
committedMar 22, 2024
fix(cli): Add timeout to agent start command
- Add `timeout` parameter (default 30) to `wait_until_conn_ready(..)` function - Apply `isort` and `black` formatting
1 parent 6dd76af commit fe3f835

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed
 

‎cli.py

+36-17
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,10 @@ def setup():
9898
)
9999
)
100100
click.echo(
101-
click.style(
102-
' git config --global user.name "Your (user)name"', fg="red"
103-
)
101+
click.style(' git config --global user.name "Your (user)name"', fg="red")
104102
)
105103
click.echo(
106-
click.style(
107-
' git config --global user.email "Your email"', fg="red"
108-
)
104+
click.style(' git config --global user.email "Your email"', fg="red")
109105
)
110106
install_error = True
111107

@@ -181,7 +177,9 @@ def setup():
181177
click.style("\t2. Navigate to https://github.com/settings/tokens", fg="red")
182178
)
183179
click.echo(click.style("\t3. Click on 'Generate new token'.", fg="red"))
184-
click.echo(click.style("\t4. Click on 'Generate new token (classic)'.", fg="red"))
180+
click.echo(
181+
click.style("\t4. Click on 'Generate new token (classic)'.", fg="red")
182+
)
185183
click.echo(
186184
click.style(
187185
"\t5. Fill out the form to generate a new token. Ensure you select the 'repo' scope.",
@@ -236,7 +234,10 @@ def create(agent_name):
236234

237235
existing_arena_files = [name.lower() for name in os.listdir("./arena/")]
238236

239-
if not os.path.exists(new_agent_dir) and not new_agent_name in existing_arena_files:
237+
if (
238+
not os.path.exists(new_agent_dir)
239+
and not new_agent_name in existing_arena_files
240+
):
240241
shutil.copytree("./autogpts/forge", new_agent_dir)
241242
click.echo(
242243
click.style(
@@ -271,7 +272,11 @@ def start(agent_name, no_setup):
271272
agent_dir = os.path.join(script_dir, f"autogpts/{agent_name}")
272273
run_command = os.path.join(agent_dir, "run")
273274
run_bench_command = os.path.join(agent_dir, "run_benchmark")
274-
if os.path.exists(agent_dir) and os.path.isfile(run_command) and os.path.isfile(run_bench_command):
275+
if (
276+
os.path.exists(agent_dir)
277+
and os.path.isfile(run_command)
278+
and os.path.isfile(run_bench_command)
279+
):
275280
os.chdir(agent_dir)
276281
if not no_setup:
277282
click.echo(f"⌛ Running setup for agent '{agent_name}'...")
@@ -331,6 +336,7 @@ def stop():
331336
except subprocess.CalledProcessError:
332337
click.echo("No process is running on port 8080")
333338

339+
334340
@agent.command()
335341
def list():
336342
"""List agents command"""
@@ -417,7 +423,7 @@ def benchmark_categories_list():
417423
)
418424
# Use it as the base for the glob pattern, excluding 'deprecated' directory
419425
for data_file in glob.glob(glob_path, recursive=True):
420-
if 'deprecated' not in data_file:
426+
if "deprecated" not in data_file:
421427
with open(data_file, "r") as f:
422428
try:
423429
data = json.load(f)
@@ -461,7 +467,7 @@ def benchmark_tests_list():
461467
)
462468
# Use it as the base for the glob pattern, excluding 'deprecated' directory
463469
for data_file in glob.glob(glob_path, recursive=True):
464-
if 'deprecated' not in data_file:
470+
if "deprecated" not in data_file:
465471
with open(data_file, "r") as f:
466472
try:
467473
data = json.load(f)
@@ -598,6 +604,7 @@ def benchmark_tests_details(test_name):
598604
print(f"IOError: file could not be read: {data_file}")
599605
continue
600606

607+
601608
@cli.group()
602609
def arena():
603610
"""Commands to enter the arena"""
@@ -760,7 +767,7 @@ def enter(agent_name, branch):
760767

761768
# Create a PR into the parent repository
762769
g = Github(github_access_token)
763-
repo_name = github_repo_url.replace("https://github.com/", '')
770+
repo_name = github_repo_url.replace("https://github.com/", "")
764771
repo = g.get_repo(repo_name)
765772
parent_repo = repo.parent
766773
if parent_repo:
@@ -838,8 +845,8 @@ def enter(agent_name, branch):
838845
def update(agent_name, hash, branch):
839846
import json
840847
import os
841-
from datetime import datetime
842848
import subprocess
849+
from datetime import datetime
843850

844851
# Check if the agent_name.json file exists in the arena directory
845852
agent_json_file = f"./arena/{agent_name}.json"
@@ -898,16 +905,28 @@ def update(agent_name, hash, branch):
898905
)
899906

900907

901-
def wait_until_conn_ready(port: int = 8000):
902-
"""Polls localhost:{port} until it is available for connections"""
903-
import time
908+
def wait_until_conn_ready(port: int = 8000, timeout: int = 30):
909+
"""
910+
Polls localhost:{port} until it is available for connections
911+
912+
Params:
913+
port: The port for which to wait until it opens
914+
timeout: Timeout in seconds; maximum amount of time to wait
915+
916+
Raises:
917+
TimeoutError: If the timeout (seconds) expires before the port opens
918+
"""
904919
import socket
920+
import time
905921

922+
start = time.time()
906923
while True:
907924
time.sleep(0.5)
908925
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
909-
if s.connect_ex(('localhost', port)) == 0:
926+
if s.connect_ex(("localhost", port)) == 0:
910927
break
928+
if time.time() > start + timeout:
929+
raise TimeoutError(f"Port {port} did not open within {timeout} seconds")
911930

912931

913932
if __name__ == "__main__":

0 commit comments

Comments
 (0)