def monitor(self): """ Monitor whether a process is dead. raises: MessageFailure: when the message thread is dead or exited. RunPayloadFailure: when the payload process is dead or exited. """ if self.__no_more_event_time and time.time() - self.__no_more_event_time > self.__waiting_time: self.__ret_code = -1 raise Exception('Too long time (%s seconds) since "No more events" is injected' % (time.time() - self.__no_more_event_time)) if self.__monitor_log_time is None or self.__monitor_log_time < time.time() - 10 * 60: self.__monitor_log_time = time.time() logger.info('monitor is checking dead process.') if self.__message_thread is None: raise MessageFailure("Message thread has not started.") if not self.__message_thread.is_alive(): raise MessageFailure("Message thread is not alive.") if self.__process is None: raise RunPayloadFailure("Payload process has not started.") if self.__process.poll() is not None: if self.is_no_more_events: logger.info("Payload finished with no more events") else: self.__ret_code = self.__process.poll() raise RunPayloadFailure("Payload process is not alive: %s" % self.__process.poll()) if self.__stop.is_set() and time.time() > self.__stop_set_time + self.__stop_delay: logger.info("Stop has been set for %s seconds, which is more than the stop wait time. Will terminate" % self.__stop_delay) self.terminate()
def __init__(self, message_queue, socket_name='EventService_EventRanges', context='local', **kwds): """ Initialize yampl server socket. :param message_queue: a queue to transfer messages between current instance and ESProcess. :param socket_name: name of the socket between current process and payload. :param context: name of the context between current process and payload, default is 'local'. :param **kwds: other parameters. :raises MessageFailure: when failed to setup message socket. """ threading.Thread.__init__(self, **kwds) self.setName("MessageThread") self.__message_queue = message_queue self._stop = threading.Event() logger.info('try to import yampl') try: import yampl except Exception as e: raise MessageFailure("Failed to import yampl: %s" % e) logger.info('finished to import yampl') logger.info('start to setup yampl server socket.') try: self.__message_server = yampl.ServerSocket(socket_name, context) except Exception as e: raise MessageFailure("Failed to setup yampl server socket: %s %s" % (e, traceback.print_exc())) logger.info('finished to setup yampl server socket.')
def get_event_ranges(self, num_ranges=None): """ Calling get_event_ranges hook to get event ranges. :param num_ranges: number of event ranges to get. :raises: SetupFailure: If get_event_ranges_hook is not set. MessageFailure: when failed to get event ranges. """ if not num_ranges: num_ranges = self.corecount logger.debug('getting event ranges(num_ranges=%s)' % num_ranges) if not self.get_event_ranges_hook: raise SetupFailure("get_event_ranges_hook is not set") try: logger.debug( 'calling get_event_ranges hook(%s) to get event ranges.' % self.get_event_ranges_hook) event_ranges = self.get_event_ranges_hook(num_ranges) logger.debug('got event ranges: %s' % event_ranges) return event_ranges except Exception as e: raise MessageFailure("Failed to get event ranges: %s" % e)
def send(self, message): """ Send messages to payload through yampl server socket. :param message: String of the message. :raises MessageFailure: When failed to send a message to the payload. """ logger.debug('Send a message to yampl: %s' % message) try: if not self.__message_server: raise MessageFailure("No message server.") self.__message_server.send_raw(message) except Exception as e: raise MessageFailure(e)
def run(self): """ Main thread loop to poll messages from payload and put received into message queue for other processes to fetch. """ logger.info('Message thread starts to run.') try: while True: if self.is_stopped(): self.terminate() break if not self.__message_server: raise MessageFailure("No message server.") size, buf = self.__message_server.try_recv_raw() if size == -1: time.sleep(0.00001) else: self.__message_queue.put(buf) except PilotException as e: self.terminate() logger.error( "Pilot Exception: Message thread got an exception, will finish: %s, %s" % (e.get_detail(), traceback.format_exc())) # raise e except Exception as e: self.terminate() logger.error("Message thread got an exception, will finish: %s" % str(e)) # raise MessageFailure(e) self.terminate() logger.info('Message thread finished.')
def init_message_thread(self, socketname=None, context='local'): """ init message thread. :param socket_name: name of the socket between current process and payload. :param context: name of the context between current process and payload, default is 'local'. :raises MessageFailure: when failed to init message thread. """ logger.info("start to init message thread") try: self.__message_thread = MessageThread(self.__message_queue, socketname, context) self.__message_thread.start() except PilotException as e: logger.error("Failed to start message thread: %s" % e.get_detail()) self.__ret_code = -1 except Exception as e: logger.error("Failed to start message thread: %s" % str(e)) self.__ret_code = -1 raise MessageFailure(e) logger.info("finished to init message thread")