Skip to content

Commit

Permalink
Merge branch 'main' into feat/wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
angrybayblade committed Mar 28, 2024
2 parents 40aa767 + add7193 commit 4347d4f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
10 changes: 10 additions & 0 deletions operate/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

from aea.helpers.logging import setup_logger
from clea import group, params, run
from docker.errors import APIError
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
Expand Down Expand Up @@ -143,6 +144,15 @@ async def _call(request: Request) -> JSONResponse:
while retries < DEFAULT_MAX_RETRIES:
try:
return await f(request)
except APIError as e:
logger.error(f"Error {e}\n{traceback.format_exc()}")
error = {"traceback": traceback.format_exc()}
if "has active endpoints" in e.explanation:
error["error"] = "Service is already running"
else:
error["error"] = str(e)
errors.append(error)
return errors
except Exception as e: # pylint: disable=broad-except
errors.append(
{"error": str(e), "traceback": traceback.format_exc()}
Expand Down
30 changes: 22 additions & 8 deletions operate/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
VENVS_DIR,
)
from autonomy.deploy.generators.docker_compose.base import DockerComposeGenerator
from docker import from_env

from operate.constants import (
DEPLOYMENT,
Expand All @@ -67,13 +68,6 @@

# pylint: disable=no-member,redefined-builtin,too-many-instance-attributes

_ACTIONS = {
"status": 0,
"build": 1,
"deploy": 2,
"stop": 3,
}

DUMMY_MULTISIG = "0xm"


Expand All @@ -96,6 +90,20 @@ def mkdirs(build_dir: Path) -> None:
continue


def remove_service_network(service_name: str) -> None:
"""Remove service network cache."""
client = from_env()
network_names = (
f"deployment_service_{service_name}_localnet",
f"abci_build_service_{service_name}_localnet",
)
for network in client.networks.list():
if network.attrs["Name"] not in network_names:
continue
print("Deleting network: " + network.attrs["Name"])
client.api.remove_network(net_id=network.attrs["Id"])


# TODO: Backport to autonomy
class ServiceBuilder(BaseServiceBuilder):
"""Service builder patch."""
Expand Down Expand Up @@ -203,14 +211,20 @@ def build(self, force: bool = True) -> None:
:param force: Remove existing deployment and build a new one
:return: Deployment object
"""
service = Service.load(path=self.path)
# Remove network from cache if exists, this will raise an error
# if the service is still running so we can do an early exit
remove_service_network(
service_name=service.helper.config.name,
)

build = self.path / DEPLOYMENT
if build.exists() and not force:
return
if build.exists() and force:
shutil.rmtree(build)
mkdirs(build_dir=build)

service = Service.load(path=self.path)
keys_file = self.path / KEYS_JSON
keys_file.write_text(
json.dumps([key.json for key in service.keys], indent=4),
Expand Down
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,6 @@ ignore_missing_imports = True

[mypy-web3.*]
ignore_missing_imports = True

[mypy-docker.*]
ignore_missing_imports = True

0 comments on commit 4347d4f

Please sign in to comment.