Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
solarw committed Jan 11, 2024
1 parent df62f50 commit 86f8a43
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 22 deletions.
89 changes: 68 additions & 21 deletions propel_client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# ------------------------------------------------------------------------------
"""CLI implementation."""
import concurrent.futures
from contextlib import contextmanager
import json
import os
import sys
Expand Down Expand Up @@ -376,6 +377,8 @@ def agents_deploy( # pylint: disable=too-many-arguments
service_ipfs_hash: str,
tendermint_ingress_enabled: bool,
timeout: int,
do_restart: bool = True,
do_delete: bool = True,
) -> None:
"""
Deploy agent command.
Expand All @@ -392,9 +395,10 @@ def agents_deploy( # pylint: disable=too-many-arguments
:param timeout: int
"""
ctx.invoke(seats_ensure)
click.echo(f"[Agent: {name}] ensure agent deleted")
ctx.invoke(agents_ensure_deleted, name_or_id=name)
click.echo(f"[Agent: {name}] agent deleted")
if do_delete:
click.echo(f"[Agent: {name}] ensure agent deleted")
ctx.invoke(agents_ensure_deleted, name_or_id=name)
click.echo(f"[Agent: {name}] agent deleted")
click.echo(f"[Agent: {name}] create agent")
ctx.invoke(
agents_create,
Expand All @@ -409,6 +413,13 @@ def agents_deploy( # pylint: disable=too-many-arguments
)
ctx.invoke(agents_wait, name_or_id=name, state="DEPLOYED", timeout=timeout)
click.echo(f"[Agent: {name}] agent deployed")
if do_restart:
_restart_and_wait(ctx, name_or_id=name, timeout=timeout)


def _restart_and_wait(ctx, name_or_id: str, timeout: int):
name = name_or_id
click.echo(f"[Agent: {name}] agent restarting")
ctx.invoke(agents_restart, name_or_id=name)
ctx.invoke(agents_wait, name_or_id=name, state="STARTED", timeout=timeout)
click.echo(f"[Agent: {name}] agent started")
Expand Down Expand Up @@ -733,26 +744,62 @@ def service_deploy( # pylint: disable=too-many-arguments
click.echo(
f"Deploy {len(keys_list)} agents for service with variables {','.join(variable_names)}"
)

def _deploy(idx, key_id, executor):
agent_name = f"{name}_agent_{idx}"
click.echo(f"[Agent: {agent_name}] Deploying with keey id {key_id}")
f = executor.submit(
ctx.invoke,
agents_deploy,
key=key_id,
name=agent_name,
variables=",".join(variable_names) if variable_names else None,
chain_id=chain_id,
token_id=token_id,
ingress_enabled=ingress_enabled,
service_ipfs_hash=service_ipfs_hash,
tendermint_ingress_enabled=tendermint_ingress_enabled,
timeout=timeout,
do_restart=False,
do_delete=False,
)
return f, agent_name

def _delete(idx, _, executor):
agent_name = f"{name}_agent_{idx}"
click.echo(f"[Agent: {agent_name}] deleting")
f = executor.submit(
ctx.invoke,
agents_ensure_deleted,
name_or_id=agent_name,
timeout=timeout,
)
return f, agent_name

def _restart(idx, key_id, executor):
agent_name = f"{name}_agent_{idx}"
click.echo(f"[Agent: {agent_name}] deleting")
f = executor.submit(
_restart_and_wait,
ctx,
name_or_id=agent_name,
timeout=timeout,
)
return f, agent_name

_run_agents_command(keys_list, _delete)
click.echo("All agents deleted")
_run_agents_command(keys_list, _deploy)
click.echo("All agents deployed")
_run_agents_command(keys_list, _restart)
click.echo("All agents restarted")


def _run_agents_command(keys_list, fn):
with ThreadPoolExecutor(max_workers=len(keys_list)) as executor:
futures = {}
for idx, key_id in enumerate(keys_list):
agent_name = f"{name}_agent_{idx}"
click.echo(
f"[Agent: {agent_name}] Deploying agent {agent_name} with key {key_id}"
)
f = executor.submit(
ctx.invoke,
agents_deploy,
key=key_id,
name=agent_name,
variables=",".join(variable_names) if variable_names else None,
chain_id=chain_id,
token_id=token_id,
ingress_enabled=ingress_enabled,
service_ipfs_hash=service_ipfs_hash,
tendermint_ingress_enabled=tendermint_ingress_enabled,
timeout=timeout,
)
f, agent_name = fn(idx, key_id, executor)
futures[f] = agent_name
exceptions = {}
for future in concurrent.futures.as_completed(futures):
Expand All @@ -765,7 +812,7 @@ def service_deploy( # pylint: disable=too-many-arguments
executor.shutdown(wait=False)
break
if exceptions:
click.echo("ERROR: Agent deploy errors!")
click.echo("ERROR: Agent errors!")
raise SystemExit(1)


Expand Down
10 changes: 9 additions & 1 deletion propel_client/propel.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def __init__(
super().__init__(message, *args)


class LogRetry(Retry):
"""
Adding extra logs before making a retry request
"""
def __init__(self, *args, **kwargs):
print("RETRY", args, kwargs)
super().__init__(*args, **kwargs)

class PropelClient:
"""Propel client."""

Expand Down Expand Up @@ -86,7 +94,7 @@ def __init__(

self._http_session = requests.Session()

retry_object = Retry(
retry_object = LogRetry(
total=retries,
backoff_factor=backoff_factor,
connect=retries,
Expand Down

0 comments on commit 86f8a43

Please sign in to comment.