Skip to content

Commit 869fb02

Browse files
committed
split on aea interaction on aea command and agent runner
1 parent d795a47 commit 869fb02

File tree

3 files changed

+211
-19
lines changed

3 files changed

+211
-19
lines changed

operate/cli.py

+5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919

2020
"""Operate app CLI module."""
2121
import asyncio
22+
import contextlib
2223
import logging
24+
import multiprocessing
2325
import os
2426
import signal
27+
import sys
2528
import traceback
2629
import typing as t
2730
import uuid
@@ -1096,6 +1099,8 @@ def qs_analyse_logs( # pylint: disable=too-many-arguments
10961099

10971100
def main() -> None:
10981101
"""CLI entry point."""
1102+
if "freeze_support" in multiprocessing.__dict__:
1103+
multiprocessing.freeze_support()
10991104
run(cli=_operate)
11001105

11011106

operate/services/deployment_runner.py

+48-16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# ------------------------------------------------------------------------------
2020
"""Source code to run and stop deployments created."""
2121
import json
22+
import multiprocessing
2223
import os
2324
import platform
2425
import shutil # nosec
@@ -95,12 +96,33 @@ class BaseDeploymentRunner(AbstractDeploymentRunner, metaclass=ABCMeta):
9596
TM_CONTROL_URL = constants.TM_CONTROL_URL
9697
SLEEP_BEFORE_TM_KILL = 2 # seconds
9798

98-
def _run_aea(self, *args: str, cwd: Path) -> Any:
99+
def _run_aea_command(self, *args: str, cwd: Path) -> Any:
99100
"""Run aea command."""
100-
# TODO: Patch for Windows failing hash (add -s). Revert once it's fixed on OpenAutonomy / OpenAEA
101-
# The fix is also implemented in PyInstallerHostDeploymentRunner._start_agent and
102-
# on HostPythonHostDeploymentRunner._start_agent
103-
return self._run_cmd(args=[self._aea_bin, "-s", *args], cwd=cwd)
101+
cmd = " ".join(args)
102+
print("Runnin aea command: ", cmd, " at ", str(cwd))
103+
p = multiprocessing.Process(
104+
target=self.__class__._call_aea_command,
105+
args=(cwd, args),
106+
)
107+
p.start()
108+
p.join()
109+
if p.exitcode != 0:
110+
raise RuntimeError(
111+
f"aea command `{cmd}`execution failed with exit code: {p.exitcode}"
112+
)
113+
114+
@staticmethod
115+
def _call_aea_command(cwd, args):
116+
try:
117+
import os
118+
119+
os.chdir(cwd)
120+
from aea.cli.core import cli as call_aea
121+
122+
call_aea(args, standalone_mode=False)
123+
except:
124+
print_exc()
125+
raise
104126

105127
@staticmethod
106128
def _run_cmd(args: t.List[str], cwd: t.Optional[Path] = None) -> None:
@@ -159,7 +181,7 @@ def _setup_agent(self) -> None:
159181
working_dir = self._work_directory
160182
env = self._prepare_agent_env()
161183

162-
self._run_aea(
184+
self._run_aea_command(
163185
"init",
164186
"--reset",
165187
"--author",
@@ -171,17 +193,19 @@ def _setup_agent(self) -> None:
171193
cwd=working_dir,
172194
)
173195

174-
self._run_aea("fetch", env["AEA_AGENT"], "--alias", "agent", cwd=working_dir)
196+
self._run_aea_command(
197+
"fetch", env["AEA_AGENT"], "--alias", "agent", cwd=working_dir
198+
)
175199

176200
# Add keys
177201
shutil.copy(
178202
working_dir / "ethereum_private_key.txt",
179203
working_dir / "agent" / "ethereum_private_key.txt",
180204
)
181205

182-
self._run_aea("add-key", "ethereum", cwd=working_dir / "agent")
206+
self._run_aea_command("add-key", "ethereum", cwd=working_dir / "agent")
183207

184-
self._run_aea("issue-certificates", cwd=working_dir / "agent")
208+
self._run_aea_command("issue-certificates", cwd=working_dir / "agent")
185209

186210
def start(self) -> None:
187211
"""Start the deployment."""
@@ -229,7 +253,7 @@ def _start_agent(self) -> None:
229253

230254
@property
231255
@abstractmethod
232-
def _aea_bin(self) -> str:
256+
def _agent_runner_bin(self) -> str:
233257
"""Return aea_bin path."""
234258
raise NotImplementedError
235259

@@ -238,7 +262,7 @@ class PyInstallerHostDeploymentRunner(BaseDeploymentRunner):
238262
"""Deployment runner within pyinstaller env."""
239263

240264
@property
241-
def _aea_bin(self) -> str:
265+
def _agent_runner_bin(self) -> str:
242266
"""Return aea_bin path."""
243267
abin = str(Path(os.path.dirname(sys.executable)) / "aea_bin") # type: ignore # pylint: disable=protected-access
244268
return abin
@@ -256,7 +280,11 @@ def _start_agent(self) -> None:
256280
env["PYTHONIOENCODING"] = "utf8"
257281
env = {**os.environ, **env}
258282
process = subprocess.Popen( # pylint: disable=consider-using-with # nosec
259-
args=[self._aea_bin, "-s", "run"], # TODO: Patch for Windows failing hash
283+
args=[
284+
self._agent_runner_bin,
285+
"-s",
286+
"run",
287+
], # TODO: Patch for Windows failing hash
260288
cwd=working_dir / "agent",
261289
stdout=subprocess.DEVNULL,
262290
stderr=subprocess.DEVNULL,
@@ -313,7 +341,7 @@ class PyInstallerHostDeploymentRunnerWindows(PyInstallerHostDeploymentRunner):
313341
"""Windows deployment runner."""
314342

315343
@property
316-
def _aea_bin(self) -> str:
344+
def _agent_runner_bin(self) -> str:
317345
"""Return aea_bin path."""
318346
abin = str(Path(os.path.dirname(sys.executable)) / "aea_win.exe") # type: ignore # pylint: disable=protected-access
319347
return abin
@@ -328,7 +356,7 @@ class HostPythonHostDeploymentRunner(BaseDeploymentRunner):
328356
"""Deployment runner for host installed python."""
329357

330358
@property
331-
def _aea_bin(self) -> str:
359+
def _agent_runner_bin(self) -> str:
332360
"""Return aea_bin path."""
333361
return str(self._venv_dir / "bin" / "aea")
334362

@@ -340,7 +368,11 @@ def _start_agent(self) -> None:
340368
env["PYTHONIOENCODING"] = "utf8"
341369

342370
process = subprocess.Popen( # pylint: disable=consider-using-with # nosec
343-
args=[self._aea_bin, "-s", "run"], # TODO: Patch for Windows failing hash
371+
args=[
372+
self._agent_runner_bin,
373+
"-s",
374+
"run",
375+
], # TODO: Patch for Windows failing hash
344376
cwd=str(working_dir / "agent"),
345377
env={**os.environ, **env},
346378
creationflags=(
@@ -413,7 +445,7 @@ def _setup_agent(self) -> None:
413445
self._setup_venv()
414446
super()._setup_agent()
415447
# Install agent dependencies
416-
self._run_aea(
448+
self._run_aea_command(
417449
"-v",
418450
"debug",
419451
"install",

0 commit comments

Comments
 (0)