def start_agent(self, asset_id='', arguments=None): asset_obj = self._validate_resource_id("asset_id", asset_id, RT.Instrument) if not asset_obj.agent_info: raise BadRequest("Cannot find agent information") agent_info = asset_obj.agent_info[0] agent_cfg = agent_info.get("config") or {} if arguments and type(arguments) is dict: dict_merge(agent_cfg, arguments, inplace=True) dataset_info = self.get_asset_data(asset_id, data_filter=dict(get_info=True, include_data=False)) dataset_id = dataset_info["dataset_id"] agent_cfg["dataset_id"] = dataset_id agent_cfg["dataset_info"] = {} log.info("Start agent for %s with dataset %s", asset_id, dataset_id) if dataset_info and dataset_info["info"]: log.info("Agent RESTART %s. Dataset exists, last sample date: %s", asset_id, dataset_info["info"]["ts_last_str"]) agent_cfg["dataset_info"] = dataset_info["info"] log.info("Using agent_info: %s", agent_info) agent_ctl = AgentControl() agent_pid = agent_ctl.launch_agent(asset_id, agent_info["agent_type"], agent_cfg) asset_obj.agent_state = {} # We assume only 1 agent per asset asset_obj.agent_state[agent_pid] = dict(start_ts=get_ion_ts()) self.rr.update(asset_obj) log.info("Agent started for %s with pid=%s", asset_id, agent_pid) return agent_pid
def start_agent(self, asset_id='', arguments=None): asset_obj = self._validate_resource_id("asset_id", asset_id, RT.Instrument) if not asset_obj.agent_info: raise BadRequest("Cannot find agent information") agent_info = asset_obj.agent_info[0] agent_cfg = agent_info.get("config") or {} if arguments and type(arguments) is dict: dict_merge(agent_cfg, arguments, inplace=True) dataset_info = self.get_asset_data(asset_id, data_filter=dict( get_info=True, include_data=False)) dataset_id = dataset_info["dataset_id"] agent_cfg["dataset_id"] = dataset_id agent_cfg["dataset_info"] = {} log.info("Start agent for %s with dataset %s", asset_id, dataset_id) if dataset_info and dataset_info["info"]: log.info("Agent RESTART %s. Dataset exists, last sample date: %s", asset_id, dataset_info["info"]["ts_last_str"]) agent_cfg["dataset_info"] = dataset_info["info"] log.info("Using agent_info: %s", agent_info) agent_ctl = AgentControl() agent_pid = agent_ctl.launch_agent(asset_id, agent_info["agent_type"], agent_cfg) asset_obj.agent_state = {} # We assume only 1 agent per asset asset_obj.agent_state[agent_pid] = dict(start_ts=get_ion_ts()) self.rr.update(asset_obj) log.info("Agent started for %s with pid=%s", asset_id, agent_pid) return agent_pid
def stop_agent(self, asset_id=''): asset_obj = self._validate_resource_id("asset_id", asset_id, RT.Instrument) log.info("Stop agent for %s", asset_id) try: agent_ctl = AgentControl(resource_id=asset_id) agent_ctl.terminate_agent() finally: if StreamingAgentClient.is_agent_active(asset_id): log.warn("Removing agent directory entry for %s", asset_id) proc_id = StreamingAgentClient._get_agent_process_id(asset_id) self.container.directory.unregister_safe("/Agents", proc_id) asset_obj.agent_state = {} self.rr.update(asset_obj)