def _get_dsa_client(self, instrument_device, dsa_instance): """ Launch the agent and return a client """ fake_process = FakeProcess() fake_process.container = self.container clients = DataAcquisitionManagementServiceDependentClients(fake_process) config_builder = ExternalDatasetAgentConfigurationBuilder(clients) try: config_builder.set_agent_instance_object(dsa_instance) self.agent_config = config_builder.prepare() log.trace("Using dataset agent configuration: %s", pprint.pformat(self.agent_config)) except Exception as e: log.error('failed to launch: %s', e, exc_info=True) raise ServerError('failed to launch') dispatcher = ProcessDispatcherServiceClient() launcher = AgentLauncher(dispatcher) log.debug("Launching agent process!") process_id = launcher.launch(self.agent_config, config_builder._get_process_definition()._id) if not process_id: raise ServerError("Launched external dataset agent instance but no process_id") config_builder.record_launch_parameters(self.agent_config) launcher.await_launch(10.0) return ResourceAgentClient(instrument_device._id, process=FakeProcess())
def __init__(self, timeout_spawn): """ @param timeout_spawn Default timeout in secs for the RUNNING event. """ self._timeout_spawn = timeout_spawn self._pd_client = ProcessDispatcherServiceClient() self._agent_launcher = AgentLauncher(self._pd_client)
def _get_dsa_client(self, instrument_device, dsa_instance): """ Launch the agent and return a client """ fake_process = FakeProcess() fake_process.container = self.container clients = DataAcquisitionManagementServiceDependentClients( fake_process) config_builder = ExternalDatasetAgentConfigurationBuilder(clients) try: config_builder.set_agent_instance_object(dsa_instance) self.agent_config = config_builder.prepare() except Exception as e: log.error('failed to launch: %s', e, exc_info=True) raise ServerError('failed to launch') self._dsa_pid = self.dams.start_external_dataset_agent_instance( dsa_instance._id) log.debug("_get_dsa_client CFG") return ResourceAgentClient(instrument_device._id, process=FakeProcess()) dispatcher = ProcessDispatcherServiceClient() launcher = AgentLauncher(dispatcher) log.debug("Launching agent process!") self._dsa_pid = launcher.launch( self.agent_config, config_builder._get_process_definition()._id) if not self._dsa_pid: raise ServerError( "Launched external dataset agent instance but no process_id") config_builder.record_launch_parameters(self.agent_config) launcher.await_launch(10.0) return ResourceAgentClient(instrument_device._id, process=FakeProcess())
class Launcher(object): """ Helper for launching platform and instrument agent processes. """ def __init__(self, timeout_spawn): """ @param timeout_spawn Default timeout in secs for the RUNNING event. """ self._timeout_spawn = timeout_spawn self._pd_client = ProcessDispatcherServiceClient() self._agent_launcher = AgentLauncher(self._pd_client) def destroy(self): if self._pd_client: self._pd_client.close() self._pd_client = None self._agent_launcher = None def launch_platform(self, agt_id, agent_config, timeout_spawn=None): """ Launches a platform agent. @param agt_id Some ID mainly used for logging @param agent_config Agent configuration @param timeout_spawn Timeout in secs for the RUNNING event (by default, the value given in constructor). If None or zero, no wait is performed. @return process ID """ timeout_spawn = timeout_spawn or self._timeout_spawn log.debug("launch_platform: agt_id=%r, timeout_spawn=%s", agt_id, timeout_spawn) name = 'PlatformAgent_%s' % agt_id pdef = ProcessDefinition(name=name) pdef.executable = { 'module': 'ion.agents.platform.platform_agent', 'class': 'PlatformAgent' } pdef_id = self._pd_client.create_process_definition(process_definition=pdef) pid = self._agent_launcher.launch(agent_config, pdef_id) if timeout_spawn: log.debug("launch_platform: agt_id=%r: waiting for RUNNING", agt_id) self._agent_launcher.await_launch(timeout_spawn) log.debug("launch_platform: agt_id=%r: RUNNING", agt_id) return pid def launch_instrument(self, agt_id, agent_config, timeout_spawn=None): """ Launches an instrument agent. @param agt_id Some ID mainly used for logging @param agent_config Agent configuration @param timeout_spawn Timeout in secs for the RUNNING event (by default, the value given in constructor). If None or zero, no wait is performed. @return process ID """ timeout_spawn = timeout_spawn or self._timeout_spawn log.debug("launch_instrument: agt_id=%r, timeout_spawn=%s", agt_id, timeout_spawn) name = 'InstrumentAgent_%s' % agt_id pdef = ProcessDefinition(name=name) pdef.executable = { 'module': 'ion.agents.instrument.instrument_agent', 'class': 'InstrumentAgent' } pdef_id = self._pd_client.create_process_definition(process_definition=pdef) pid = self._agent_launcher.launch(agent_config, pdef_id) if timeout_spawn: log.debug("launch_instrument: agt_id=%r: waiting for RUNNING", agt_id) self._agent_launcher.await_launch(timeout_spawn) log.debug("launch_instrument: agt_id=%r: RUNNING", agt_id) return pid def cancel_process(self, pid, timeout_cancel=None): """ Helper to terminate a process """ pinfo = self._pd_client.read_process(pid) if pinfo.process_state != ProcessStateEnum.RUNNING: log.debug("cancel_process: pid=%r is not RUNNING", pid) return log.debug("cancel_process: canceling pid=%r", pid) self._pd_client.cancel_process(pid) if timeout_cancel: log.debug("waiting %s seconds for preocess to cancel", timeout_cancel) psg = ProcessStateGate(self._pd_client.read_process, pid, ProcessStateEnum.TERMINATED) if not psg.await(timeout_cancel): log.debug("Process %r failed to get to TERMINATED in %s seconds", pid, timeout_cancel)
class Launcher(object): """ Helper for launching platform and instrument agent processes. """ def __init__(self, timeout_spawn): """ @param timeout_spawn Default timeout in secs for the RUNNING event. """ self._timeout_spawn = timeout_spawn self._pd_client = ProcessDispatcherServiceClient() self._agent_launcher = AgentLauncher(self._pd_client) def destroy(self): if self._pd_client: self._pd_client.close() self._pd_client = None self._agent_launcher = None def launch_platform(self, agt_id, agent_config, timeout_spawn=None): """ Launches a platform agent. @param agt_id Some ID mainly used for logging @param agent_config Agent configuration @param timeout_spawn Timeout in secs for the RUNNING event (by default, the value given in constructor). If None or zero, no wait is performed. @return process ID """ timeout_spawn = timeout_spawn or self._timeout_spawn log.debug("launch_platform: agt_id=%r, timeout_spawn=%s", agt_id, timeout_spawn) name = 'PlatformAgent_%s' % agt_id pdef = ProcessDefinition(name=name) pdef.executable = { 'module': 'ion.agents.platform.platform_agent', 'class': 'PlatformAgent' } pdef_id = self._pd_client.create_process_definition( process_definition=pdef) pid = self._agent_launcher.launch(agent_config, pdef_id) if timeout_spawn: log.debug("launch_platform: agt_id=%r: waiting for RUNNING", agt_id) self._agent_launcher.await_launch(timeout_spawn) log.debug("launch_platform: agt_id=%r: RUNNING", agt_id) return pid def launch_instrument(self, agt_id, agent_config, timeout_spawn=None): """ Launches an instrument agent. @param agt_id Some ID mainly used for logging @param agent_config Agent configuration @param timeout_spawn Timeout in secs for the RUNNING event (by default, the value given in constructor). If None or zero, no wait is performed. @return process ID """ timeout_spawn = timeout_spawn or self._timeout_spawn log.debug("launch_instrument: agt_id=%r, timeout_spawn=%s", agt_id, timeout_spawn) name = 'InstrumentAgent_%s' % agt_id pdef = ProcessDefinition(name=name) pdef.executable = { 'module': 'ion.agents.instrument.instrument_agent', 'class': 'InstrumentAgent' } pdef_id = self._pd_client.create_process_definition( process_definition=pdef) pid = self._agent_launcher.launch(agent_config, pdef_id) if timeout_spawn: log.debug("launch_instrument: agt_id=%r: waiting for RUNNING", agt_id) self._agent_launcher.await_launch(timeout_spawn) log.debug("launch_instrument: agt_id=%r: RUNNING", agt_id) return pid def cancel_process(self, pid, timeout_cancel=None): """ Helper to terminate a process """ pinfo = self._pd_client.read_process(pid) if pinfo.process_state != ProcessStateEnum.RUNNING: log.debug("cancel_process: pid=%r is not RUNNING", pid) return log.debug("cancel_process: canceling pid=%r", pid) self._pd_client.cancel_process(pid) if timeout_cancel: log.debug("waiting %s seconds for preocess to cancel", timeout_cancel) psg = ProcessStateGate(self._pd_client.read_process, pid, ProcessStateEnum.TERMINATED) if not psg. await (timeout_cancel): log.debug( "Process %r failed to get to TERMINATED in %s seconds", pid, timeout_cancel)
def __init__(self): self._pd_client = ProcessDispatcherServiceClient() self._agent_launcher = AgentLauncher(self._pd_client)
class Launcher(object): """ Helper for launching platform and instrument agent processes. """ def __init__(self): self._pd_client = ProcessDispatcherServiceClient() self._agent_launcher = AgentLauncher(self._pd_client) def destroy(self): if self._pd_client: self._pd_client.close() self._pd_client = None self._agent_launcher = None def launch_platform(self, agt_id, agent_config, timeout_spawn=30): """ Launches a platform agent. @param agt_id Some ID mainly used for logging @param agent_config Agent configuration @param timeout_spawn Timeout in secs for the SPAWN event (by default 30). If None or zero, no wait is performed. @return process ID """ log.debug("launch platform: agt_id=%r, timeout_spawn=%s", agt_id, timeout_spawn) name = 'PlatformAgent_%s' % agt_id pdef = ProcessDefinition(name=name) pdef.executable = { 'module': 'ion.agents.platform.platform_agent', 'class': 'PlatformAgent' } pdef_id = self._pd_client.create_process_definition(process_definition=pdef) pid = self._agent_launcher.launch(agent_config, pdef_id) if timeout_spawn: self._agent_launcher.await_launch(timeout_spawn) return pid def launch_instrument(self, agt_id, agent_config, timeout_spawn=30): """ Launches an instrument agent. @param agt_id Some ID mainly used for logging @param agent_config Agent configuration @param timeout_spawn Timeout in secs for the SPAWN event (by default 30). If None or zero, no wait is performed. @return process ID """ log.debug("launch instrument: agt_id=%r, timeout_spawn=%s", agt_id, timeout_spawn) name = 'InstrumentAgent_%s' % agt_id pdef = ProcessDefinition(name=name) pdef.executable = { 'module': 'ion.agents.instrument.instrument_agent', 'class': 'InstrumentAgent' } pdef_id = self._pd_client.create_process_definition(process_definition=pdef) pid = self._agent_launcher.launch(agent_config, pdef_id) if timeout_spawn: self._agent_launcher.await_launch(timeout_spawn) return pid def cancel_process(self, pid): """ Helper to terminate a process """ self._pd_client.cancel_process(pid)