def run(self): try: bind_debug_signal_handlers() logger.info("StatusCommandsExecutor starting") while True: command = self.actionQueue.statusCommandQueue.get( True) # blocks until status status command appears logger.debug("Running status command for {0}".format( command['componentName'])) timeout_timer = threading.Timer(self.status_command_timeout, self.respawn, [command]) timeout_timer.start() self.process_status_command(command) timeout_timer.cancel() logger.debug("Completed status command for {0}".format( command['componentName'])) except: logger.exception( "StatusCommandsExecutor process failed with exception:") raise logger.warn("StatusCommandsExecutor process has finished")
def bind_signal_handlers(agentPid, stop_event): global _handler if OSCheck.get_os_family() != OSConst.WINSRV_FAMILY: if os.getpid() == agentPid: signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) bind_debug_signal_handlers() _handler = stop_event else: _handler = stop_event return _handler
def bind_signal_handlers(agentPid): global _handler if OSCheck.get_os_family() != OSConst.WINSRV_FAMILY: if os.getpid() == agentPid: signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) bind_debug_signal_handlers() _handler = HeartbeatStopHandlersLinux() else: _handler = HeartbeatStopHandlersWindows() return _handler
def _worker_process_target(self): """ Internal method that running in separate process. """ # cleanup monkey-patching results in child process, as it causing problems import subprocess reload(subprocess) import multiprocessing reload(multiprocessing) bind_debug_signal_handlers() self._log_message(logging.INFO, "StatusCommandsExecutor process started") # region StatusCommandsExecutor process internals internal_in_queue = Queue.Queue() internal_out_queue = Queue.Queue() def _internal_worker(): """ thread that actually executes status commands """ while True: _cmd = internal_in_queue.get() internal_out_queue.put( self.actionQueue. execute_status_command_and_security_status(_cmd)) worker = threading.Thread(target=_internal_worker) worker.daemon = True worker.start() def _internal_process_command(_command): internal_in_queue.put(_command) start_time = time.time() result = None while not self.mustDieEvent.is_set() and not result and time.time( ) - start_time < self.status_command_timeout: try: result = internal_out_queue.get(timeout=1) except Queue.Empty: pass if result: self.mp_result_queue.put(result) return True else: # do not set timed out event twice if not self.timedOutEvent.is_set(): self._set_timed_out(_command) return False # endregion try: while not self.mustDieEvent.is_set(): try: command = self.mp_task_queue.get(False) except Queue.Empty: # no command, lets try in other loop iteration time.sleep(.1) continue self._log_message( logging.DEBUG, "Running status command for {0}".format( command['componentName'])) if _internal_process_command(command): self._log_message( logging.DEBUG, "Completed status command for {0}".format( command['componentName'])) except Exception as e: self._log_message( logging.ERROR, "StatusCommandsExecutor process failed with exception:", e) raise self._log_message(logging.INFO, "StatusCommandsExecutor subprocess finished")