def test_get_process_status_positive(self): """Test get_process_status for positive result.""" proc_id_mock = mock.Mock() proc_id_mock.poll = mock.Mock(return_value=-1) result = get_process_status(proc_id_mock) expected_result = ProcessState.FINISHED self.assertEqual(result, expected_result) proc_id_mock.poll = mock.Mock(return_value=1) result = get_process_status(proc_id_mock) expected_result = ProcessState.FAILED self.assertEqual(result, expected_result)
def get_agent_status(agent_id: str) -> Tuple[JSONLike, int]: """Get the status of the running agent Node.""" status_str = str(ProcessState.NOT_STARTED).replace("ProcessState.", "") tty_str = "" error_str = "" # agent_id will not be in lists if we haven't run it yet if (agent_id in app_context.agent_processes and app_context.agent_processes[agent_id] is not None): status_str = str( get_process_status(app_context.agent_processes[agent_id])).replace( "ProcessState.", "") if agent_id in app_context.agent_tty: total_num_lines = len(app_context.agent_tty[agent_id]) for i in range(max(0, total_num_lines - max_log_lines), total_num_lines): tty_str += app_context.agent_tty[agent_id][i] else: tty_str = "" tty_str = tty_str.replace("\n", "<br>") if agent_id in app_context.agent_error: total_num_lines = len(app_context.agent_error[agent_id]) for i in range(max(0, total_num_lines - max_log_lines), total_num_lines): error_str += app_context.agent_error[agent_id][i] else: error_str = "" error_str = error_str.replace("\n", "<br>") return { "status": status_str, "tty": tty_str, "error": error_str }, 200 # (OK)
def start_agent(agent_id: str, connection_id: PublicId): """Start a local agent running.""" # Test if it is already running in some form if agent_id in app_context.agent_processes: if ( get_process_status(app_context.agent_processes[agent_id]) != ProcessState.RUNNING ): # pragma: no cover if app_context.agent_processes[agent_id] is not None: app_context.agent_processes[agent_id].terminate() app_context.agent_processes[agent_id].wait() del app_context.agent_processes[agent_id] del app_context.agent_tty[agent_id] del app_context.agent_error[agent_id] else: return ( {"detail": "Agent {} is already running".format(agent_id)}, 400, ) # 400 Bad request agent_dir = os.path.join(app_context.agents_dir, agent_id) if connection_id is not None and connection_id != "": connections = get_local_items(agent_id, CONNECTION)[0] has_named_connection = False for element in connections: if element["public_id"] == connection_id: has_named_connection = True if has_named_connection: agent_process = call_aea_async( [ sys.executable, "-m", "aea.cli", "run", "--connections", str(connection_id), ], agent_dir, ) else: return ( { "detail": "Trying to run agent {} with non-existent connection: {}".format( agent_id, connection_id ) }, 400, ) # 400 Bad request else: agent_process = call_aea_async( [sys.executable, "-m", "aea.cli", "run", "--install-deps"], agent_dir ) if agent_process is None: return ( {"detail": "Failed to run agent {}".format(agent_id)}, 400, ) # 400 Bad request app_context.agent_processes[agent_id] = agent_process app_context.agent_tty[agent_id] = [] app_context.agent_error[agent_id] = [] tty_read_thread = threading.Thread( target=read_tty, args=(app_context.agent_processes[agent_id], app_context.agent_tty[agent_id],), ) tty_read_thread.start() error_read_thread = threading.Thread( target=read_error, args=( app_context.agent_processes[agent_id], app_context.agent_error[agent_id], ), ) error_read_thread.start() return agent_id, 201 # 200 (OK)