def send_error_message(self, msg): logger = logging.getLogger("ProcessBot.send_error_message to " + str(self._remote_wps_endpoint)) logger.error(msg) if self.bus.state() != 'connected': try: self.bus.xmpp.reconnect() self.bus.xmpp.send_presence() except BaseException: logger.info( "[XMPP Disconnected]: Process " + str(self._uniqueExeId) + " Could not send info message to GeoServer Endpoint " + str(self._remote_wps_endpoint)) if self.bus.state() == 'connected': self.bus.SendMessage( busIndipendentMessages.ErrorMessage(self._remote_wps_endpoint, msg)) else: try: self.bus.xmpp.reconnect() self.bus.xmpp.send_presence() # self.bus.xmpp.get_roster() if self.bus.state() == 'connected': self.bus.SendMessage( busIndipendentMessages.ErrorMessage( self._remote_wps_endpoint, msg)) else: sys.stdout.write( "[XMPP Disconnected]: Process <UID>" + str(self._uniqueExeId) + "</UID> Could not send error message to GeoServer Endpoint <JID>" + str(self._remote_wps_endpoint) + "</JID> <MSG>" + msg.replace('\n', ' ').replace('\r', '') + "</MSG>") except BaseException: sys.stdout.write( "[XMPP Disconnected]: Process <UID>" + str(self._uniqueExeId) + "</UID> Could not send error message to GeoServer Endpoint <JID>" + str(self._remote_wps_endpoint) + "</JID> <MSG>" + msg.replace('\n', ' ').replace('\r', '') + "</MSG>") logger.debug("send error msg complete") self._finished = True self.exit(-1)
def send_error_message(self, msg): logger = logging.getLogger("ProcessBot.send_error_message to " + str(self._remote_wps_endpoint)) logger.error(msg) with self._lock_bus: if self.bus.state() == 'connected': self.bus.SendMessage( busIndipendentMessages.ErrorMessage( self._remote_wps_endpoint, msg)) else: try: self.bus.xmpp.reconnect() self.bus.xmpp.send_presence() # self.bus.xmpp.get_roster() if self.bus.state() == 'connected': self.bus.SendMessage( busIndipendentMessages.ErrorMessage( self._remote_wps_endpoint, msg)) else: sys.stdout.write( "[XMPP Disconnected]: Process <UID>" + str(self._uniqueExeId) + "</UID> Could not send error message to GeoServer Endpoint <JID>" + str(self._remote_wps_endpoint) + "</JID> <MSG>" + msg.replace('\n', ' ').replace('\r', '') + "</MSG>") except: sys.stdout.write( "[XMPP Disconnected]: Process <UID>" + str(self._uniqueExeId) + "</UID> Could not send error message to GeoServer Endpoint <JID>" + str(self._remote_wps_endpoint) + "</JID> <MSG>" + msg.replace('\n', ' ').replace('\r', '') + "</MSG>") logger.debug("send error msg complete") thread.interrupt_main() os._exit(-1)
def send_error_message(self, msg): logger = logging.getLogger("ServiceBot.send_error_message") logger.error(msg) try: if self.bus.state() != 'connected': self.bus.xmpp.reconnect() self.bus.xmpp.send_presence() if self._remote_wps_endpoint: self.bus.SendMessage( busIndipendentMessages.ErrorMessage( self._remote_wps_endpoint, msg)) else: msg = "Process " + str( self.service ) + " STALLED! Don't know who to send ERROR Message..." logger.error(msg) except BaseException: logger.info( "[XMPP Disconnected]: Service " + str(self.service) + " Could not send error message to GeoServer Endpoint " + str(self._remote_wps_endpoint))
def process_output_parser(self, invoked_process): logger = logging.getLogger("ProcessBot.process_output_parser") logger.info("start parsing stdout of created process " + self.service) with self._lock_bus: if self.bus.state() == 'connected': self.bus.SendMessage( busIndipendentMessages.LogMessage( self._remote_wps_endpoint, "INFO", "start parsing stdout of created process " + self.service)) else: try: self.bus.xmpp.reconnect() self.bus.xmpp.send_presence() # self.bus.xmpp.get_roster() if self.bus.state() == 'connected': self.bus.SendMessage( busIndipendentMessages.LogMessage( self._remote_wps_endpoint, "INFO", "start parsing stdout of created process " + self.service)) else: logger.info( "[XMPP Disconnected]: Process " + str(self._uniqueExeId) + " Could not send info message to GeoServer Endpoint " + str(self._remote_wps_endpoint)) except: logger.info( "[XMPP Disconnected]: Process " + str(self._uniqueExeId) + " Could not send info message to GeoServer Endpoint " + str(self._remote_wps_endpoint)) # Listen to stdout stdout_parser_compiled = [re.compile(r) for r in self._stdout_parser] stack_trace = [] while True: line = invoked_process.stdout.readline() if line != '': line = line.strip() logger.debug("Received line: " + line) stack_trace.append(line) match = False #todo: define a line to get pid of computational job process for rgx, action in zip(stdout_parser_compiled, self._stdout_action): res = rgx.match(line) if (res): if (action == "progress"): with self._lock_bus: if self.bus.state() != 'connected': try: self.bus.xmpp.reconnect() self.bus.xmpp.send_presence() # self.bus.xmpp.get_roster() except: logger.info( "[XMPP Disconnected]: Process " + str(self._uniqueExeId) + " Could not send info message to GeoServer Endpoint " + str(self._remote_wps_endpoint)) self.bus.SendMessage( busIndipendentMessages.ProgressMessage( self._remote_wps_endpoint, float(res.group(1).strip()))) match = True break elif (action == "log"): with self._lock_bus: if self.bus.state() != 'connected': try: self.bus.xmpp.reconnect() self.bus.xmpp.send_presence() # self.bus.xmpp.get_roster() except: logger.info( "[XMPP Disconnected]: Process " + str(self._uniqueExeId) + " Could not send info message to GeoServer Endpoint " + str(self._remote_wps_endpoint)) self.bus.SendMessage( busIndipendentMessages.LogMessage( self._remote_wps_endpoint, res.group(1).strip(), res.group(2).strip())) match = True break elif (action == "abort"): with self._lock_bus: if self.bus.state() != 'connected': try: self.bus.xmpp.reconnect() self.bus.xmpp.send_presence() # self.bus.xmpp.get_roster() except: logger.info( "[XMPP Disconnected]: Process " + str(self._uniqueExeId) + " Could not send info message to GeoServer Endpoint " + str(self._remote_wps_endpoint)) self.bus.SendMessage( busIndipendentMessages.ErrorMessage( self._remote_wps_endpoint, res.group(2).strip())) match = True break elif (action == "ignore"): match = True break else: pass else: #end of stream break logger.debug("process " + self.service + "stdout is over") #wait for process exit code return_code = invoked_process.wait() logger.info("process exit code is " + str(return_code)) if return_code == 0: #success logger.info("process exit code is " + str(return_code) + ": success") logger.info( "send job-completed message to WPS with output parameter") outputs = dict() try: for p in self._output_parameters_defs.parameters(): outputs[p.get_name()] = [ p.get_value(), p.get_description(), p.get_title(), p.get_type(), p.is_publish_as_layer(), p.get_publish_layer_name(), p.get_publish_default_style(), p.get_publish_target_workspace(), p.get_metadata() ] except: logging.exception("Process " + str(self._uniqueExeId) + " Exception: " + str(traceback.format_exc(sys.exc_info()))) error_message = "process exit code is " + str( return_code) + ": failure\n" + "\n".join( str(e) for e in stack_trace) self.send_error_message(error_message) #self.bus.disconnect() logger.info("after send job-error message to WPS") thread.interrupt_main() os._exit(return_code) logger.info("trying to acquire bus lock...") with self._lock_bus: logger.info("bus lock acquired...") if self.bus.state() != 'connected': try: self.bus.xmpp.reconnect() self.bus.xmpp.send_presence() # self.bus.xmpp.get_roster() except: logger.info( "[XMPP Disconnected]: Process " + str(self._uniqueExeId) + " Could not send info message to GeoServer Endpoint " + str(self._remote_wps_endpoint)) counter = 1 while not self._finished: logger.info("sending 'completed' message tentative #" + str(counter)) self.bus.SendMessage( busIndipendentMessages.CompletedMessage( self._remote_wps_endpoint, self._remote_wps_baseurl, outputs)) counter = counter + 1 if counter < 100: sleep(10) else: logger.error( "Could not contact Remote WPS with. Forcibly shutdown the process..." ) thread.interrupt_main() os._exit(-1) logger.info("after send job-completed message to WPS") else: error_message = "process exit code is " + str( return_code) + ": failure\n" + "\n".join( str(e) for e in stack_trace) logger.critical("process exit code is " + str(return_code) + ": failure") #todo: should i wait for finish message here as well? No self.send_error_message(error_message) #self.bus.disconnect() logger.info("after send job-error message to WPS") thread.interrupt_main() os._exit(return_code)
def output_parser_verbose(self, invoked_process, param_filepath): logger = logging.getLogger("servicebot.output_parser_verbose") logger.info("wait for end of execution of created process " + self.service + ", PId " + str(invoked_process.pid)) gs_UID = None gs_JID = None gs_MSG = None while True: line = invoked_process.stdout.readline() if line != '': # Look for GeoServer JID from Process gs_UID_search = re.search('<UID>(.*)</UID>', line, re.IGNORECASE) gs_JID_search = re.search('<JID>(.*)</JID>', line, re.IGNORECASE) if gs_UID_search: try: gs_UID = gs_UID_search.group(1) gs_JID = gs_JID_search.group(1) gs_MSG = gs_JID_search = re.search( '<MSG>(.*)</MSG>', line, re.IGNORECASE).group(1) except: pass if self._redirect_process_stdout_to_logger: line = line.strip() logger.debug("[SERVICE] " + line) else: logger.debug("created process " + self.service + ", PId " + str(invoked_process.pid) + " stopped send data on stdout") break #end of stream #wait for process exit code return_code = invoked_process.wait() if return_code != 0: msg = "Process " + self.service + " PId " + str( invoked_process.pid) + " terminated with exit code " + str( return_code) logger.critical(msg) logger.debug("gs_UID[%s] / gs_JID[%s]" % (gs_UID, gs_JID)) if gs_UID and gs_JID: self.bus.SendMessage( busIndipendentMessages.ErrorMessage( gs_JID, msg + " Exception: " + str(gs_MSG), gs_UID)) elif self._remote_wps_endpoint: self.bus.SendMessage( busIndipendentMessages.ErrorMessage( self._remote_wps_endpoint, msg)) else: exe_msg = None try: logger.debug( "Trying to recover Originator from Process Params!") exe_msg = busIndipendentMessages.ExecuteMessage.deserialize( param_filepath) if exe_msg.originator(): self.bus.SendMessage( busIndipendentMessages.ErrorMessage( exe_msg.originator(), msg + " Exception: remote process exception. Please check outputs!", exe_msg.UniqueId())) except: pass if not exe_msg: msg = "Process " + self.service + " PId " + str( invoked_process.pid ) + " STALLED! Don't know who to send ERROR Message..." logger.error(msg) else: msg = "Process " + self.service + " PId " + str( invoked_process.pid) + " terminated successfully!" logger.debug(msg)
def output_parser_verbose(self, invoked_process, param_filepath): logger = logging.getLogger("servicebot.output_parser_verbose") logger.info("wait for end of execution of created process " + self.service + ", PId " + str(invoked_process.pid)) gs_UID = None gs_JID = None gs_MSG = None while True: try: line = invoked_process.stdout.readline() if line != '' and 'send error msg complete' not in line: # Look for GeoServer JID from Process gs_UID_search = re.search('<UID>(.*)</UID>', line, re.IGNORECASE) gs_JID_search = re.search('<JID>(.*)</JID>', line, re.IGNORECASE) if gs_UID_search: try: gs_UID = gs_UID_search.group(1) gs_JID = gs_JID_search.group(1) gs_MSG = gs_JID_search = re.search( '<MSG>(.*)</MSG>', line, re.IGNORECASE).group(1) except BaseException: pass if self._redirect_process_stdout_to_logger: line = line.strip() logger.debug("[SERVICE] " + line) else: logger.debug("created process " + self.service + ", PId " + str(invoked_process.pid) + " stopped send data on stdout") break # end of stream except SystemExit: break # wait for process exit code return_code = -1 poll = invoked_process.poll() if poll: return_code = poll else: from threading import Timer timer = Timer(10, invoked_process.kill) try: timer.start() # stdout, stderr = invoked_process.communicate() return_code = invoked_process.wait() finally: timer.cancel() if return_code != 0: msg = "Process " + self.service + " PId " + \ str(invoked_process.pid) + " terminated with exit code " + str(return_code) logger.critical(msg) logger.debug("gs_UID[%s] / gs_JID[%s]" % (gs_UID, gs_JID)) try: if gs_UID and gs_JID: self.bus.SendMessage( busIndipendentMessages.ErrorMessage( gs_JID, msg + " Exception: " + str(gs_MSG), gs_UID)) elif self._remote_wps_endpoint: self.bus.SendMessage( busIndipendentMessages.ErrorMessage( self._remote_wps_endpoint, msg)) else: exe_msg = None try: logger.debug( "Trying to recover Originator from Process Params!" ) exe_msg = busIndipendentMessages.ExecuteMessage.deserialize( param_filepath) if exe_msg.originator(): self.bus.SendMessage( busIndipendentMessages.ErrorMessage( exe_msg.originator(), msg + " Exception: remote process exception. Please check outputs!", exe_msg.UniqueId())) except BaseException: pass if not exe_msg: msg = "Process " + self.service + " PId " + \ str(invoked_process.pid) + " STALLED! Don't know who to send ERROR Message..." logger.error(msg) except BaseException: logger.info( "[XMPP Disconnected]: Service " + str(self.service) + " Could not send error message to GeoServer Endpoint " + str(self._remote_wps_endpoint)) else: msg = "Process " + self.service + " PId " + str( invoked_process.pid) + " terminated successfully!" logger.debug(msg)