def make_beat_msg(pm, CFG, log=logging): beat_msg = {} beat_msg['eeagent_id'] = CFG.eeagent.name # include node ID if it is present in config node_id = CFG.eeagent.get('node_id') if node_id is not None: beat_msg['node_id'] = CFG.eeagent.node_id # include timestamp in UTC, in iso8601/rfc3339 beat_msg['timestamp'] = datetime.now(utc).isoformat() beat_processes = [] # we can have many process managers per eeagent, walk them all to get all the processes pm.poll() processes = pm.get_all() for pname in processes: p = processes[pname] (name, round) = unmake_id(p.get_name()) try: state = p.get_state() except: log.exception("Had a problem getting process state") raise beat_p = {'upid': name, 'round': round, 'state': state, 'msg': p.get_error_message()} beat_processes.append(beat_p) beat_msg['processes'] = beat_processes return beat_msg
def lookup_id(self, process_name, ignore_round=False): if ignore_round: process_upid, process_round = unmake_id(process_name) for name, proc in self._known_pws.iteritems(): upid, round = unmake_id(name) if process_upid == upid: return proc else: return None else: if process_name not in self._known_pws: return None return self._known_pws[process_name]
def make_beat_msg(pm, CFG, log=logging): beat_msg = {} beat_msg['eeagent_id'] = CFG.eeagent.name # include node ID if it is present in config node_id = CFG.eeagent.get('node_id') if node_id is not None: beat_msg['node_id'] = CFG.eeagent.node_id # include timestamp in UTC, in iso8601/rfc3339 beat_msg['timestamp'] = datetime.now(utc).isoformat() beat_processes = [] # we can have many process managers per eeagent, walk them all to get all the processes pm.poll() processes = pm.get_all() for pname in processes: p = processes[pname] (name, round) = unmake_id(p.get_name()) try: state = p.get_state() except: log.exception("Had a problem getting process state") raise beat_p = { 'upid': name, 'round': round, 'state': state, 'msg': p.get_error_message() } beat_processes.append(beat_p) beat_msg['processes'] = beat_processes return beat_msg
def launch_process(self, u_pid, round, run_type, parameters): if run_type != self.CFG.eeagent.launch_type.name: raise EEAgentParameterException("Unknown run type %s" % (run_type)) # check to see if download_code enabled if (parameters.get('module_uri') and (not self.CFG.eeagent.get('code_download') or not self.CFG.eeagent.code_download.get('enabled', False))): msg = "Code download not enabled in this eeagent" raise EEAgentUnauthorizedException(msg) # check if url in whitelist if parameters.get('module_uri'): uri = parameters.get('module_uri') if not self._check_whitelist(uri): msg = "%s not in code_download whitelist: '%s'" % (uri, ", ".join(self._get_whitelist())) raise EEAgentUnauthorizedException(msg) # Check to see whether this process already exists process = self._find_proc(u_pid, round, ignore_round=True) if process is not None and not process.restartable: existing_upid, existing_round = unmake_id(process._name) state = str(process.get_state()) if int(round) > int(existing_round): msg = ("VERY BAD THING: A launch request for '%s' has been " "recieved, but it is in state %s. The existing process has " "round %s, but the PD is asking to start process with round " "%s. Restarting the process anyway, but something is wrong." % (u_pid, state, existing_round, round)) self._log.error(msg) else: msg = ("BAD THING: A launch request for '%s' has been " "recieved, but it is in state %s. The existing process has " "round %s, and the PD is asking to start process with round " "%s. Maybe a message never arrived? Leaving process as-is." % (u_pid, state, existing_round, round)) self._log.warning(msg) return try: process.terminate() except PIDanticStateException, pse: self._log.warning("Attempt to terminate a process in the state %s" % (str(process.get_state()))) try: process.clean_up() except Exception, ex: self._log.warning("Failed to cleanup: %s" % (str(ex)))
def beat_it(dashi, CFG, pm, log=logging): try: beat_msg = {} beat_msg['eeagent_id'] = "" beat_msg['timestamp'] = str(datetime.datetime.now()) beat_processes = [] # we can have many process managers per eeagent, walk them all to get all the processes pm.poll() processes = pm.get_all() for pname in processes: p = processes[pname] (name, round) = unmake_id(p.get_name()) beat_p = {'upid': name, 'round': round, 'state': p.get_state(), 'msg': p.get_error_message()} beat_processes.append(beat_p) beat_msg['processes'] = beat_processes log.log(logging.DEBUG, "Sending the heartbeat : %s" % (json.dumps(beat_msg))) dashi.fire(CFG.pd.name, "heartbeat", message=beat_msg) except Exception, ex: log.log(logging.ERROR, "Error Sending the heartbeat : %s" % (str(ex)))