def term_handler(_, __): self.logger.info("Term handler") pp_handler = self.state.get_corresponding_handler( PoisonPillMessage()) pp_handler.handle(PoisonPillMessage(soft=False)) self._kill_process() sys.exit(0)
def send_kill(self, by_data=False): """ Kill this actor by sending a :class:`PoisonPillMessage <powerapi.message.message.PoisonPillMessage>` :param bool by_data: Define if the kill msg is send in the control socket or the data socket """ if by_data: self.send_data(PoisonPillMessage()) else: self.send_control(PoisonPillMessage()) self.socket_interface.close()
def test_get_handler(initialized_dummy_actor): """ Test to get the predefined handler for PoisonPillMessage type """ handler = initialized_dummy_actor.state.get_corresponding_handler( PoisonPillMessage()) assert isinstance(handler, PoisonPillMessageHandler)
def hard_kill(self): """Kill this actor by sending a hard :class:`PoisonPillMessage <powerapi.message.message.PoisonPillMessage>` """ self.send_control(PoisonPillMessage(soft=False)) self.socket_interface.close()
def test_hard_PoisonPillMessage_attr(): """ Create a hard PoisonPillMessage instance and test its attribute value """ msg = PoisonPillMessage(soft=False) assert not msg.is_soft assert msg.is_hard
def test_PoisonPillMessageHandler_hard(mocked_ppHandler): """ handle a hard PoisonPillMessage with the handler Test if: - the handler teardown method was called """ mocked_ppHandler.handle(PoisonPillMessage(soft=False)) assert mocked_ppHandler.teardown.called
def test_soft_kill_method(dummy_actor_mocked): """ Call the soft_kill method of and actor Test if: - a soft PoisonPillMessage was sent to the actor on the control socket """ dummy_actor_mocked.soft_kill() assert dummy_actor_mocked.socket_interface.send_control.called msg = dummy_actor_mocked.socket_interface.send_control.call_args[0][0] assert msg == PoisonPillMessage(soft=True)
def test_PoisonPillMessageHandler_soft(mocked_ppHandler): """ handle a soft PoisonPillMessage with the handler Test if: - the receive method was called - the handler teardown method was called """ mocked_ppHandler.handle(PoisonPillMessage(soft=True)) assert mocked_ppHandler.state.actor.socket_interface.receive.called assert mocked_ppHandler.teardown.called
def run(self): """ Read data from Database and send it to the dispatchers. If there is no more data, send a kill message to every dispatcher. If stream mode is disable, kill the actor. :param None msg: None. """ if self.state.asynchrone: self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) self.state.loop = self.loop self.loop.set_debug(enabled=True) logging.basicConfig(level=logging.DEBUG) self._connect() while self.state.alive: try: report = self._pull_database() dispatchers = self._get_dispatchers(report) for dispatcher in dispatchers: dispatcher.send_data(report) except NoReportExtractedException: time.sleep(self.state.timeout_puller / 1000) if not self.state.stream_mode: self.handler.handle_internal_msg( PoisonPillMessage(soft=False)) return except FilterUselessError: self.handler.handle_internal_msg(PoisonPillMessage(soft=False)) return except StopIteration: continue
def pull_db(self): """ Initialize the database and connect all dispatcher to the socket_interface """ db_puller_thread = DBPullerThread(self.state, self.timeout) db_puller_thread.start() while db_puller_thread.is_alive() and self.state.alive: time.sleep(0.4) msg = self.state.actor.receive_control(0.1) if msg is not None: self.handle_internal_msg(msg) self.handle_internal_msg(PoisonPillMessage(soft=False))
def test_create_PoisonPillMessage_with_soft_False_set_attribute_is_hard_to_True( ): msg = PoisonPillMessage(soft=False) assert msg.is_hard
def test_create_PoisonPillMessage_with_soft_True_set_attribute_is_soft_to_True( ): msg = PoisonPillMessage(soft=True) assert msg.is_soft
def test_send_soft_PoisonPillMessage_make_dispatcher_soft_kill_formula( self, dispatcher_with_two_formula, formula_queue): dispatcher_with_two_formula.send_control(PoisonPillMessage()) assert formula_queue.get(timeout=0.5) == 'soft kill' assert formula_queue.get(timeout=0.5) == 'soft kill'
def test_send_hard_PoisonPillMessage_make_dispatcher_hard_kill_formula( self, dispatcher_with_two_formula, formula_queue): dispatcher_with_two_formula.send_control(PoisonPillMessage(soft=False)) assert formula_queue.get(timeout=0.5) == 'hard kill' assert formula_queue.get(timeout=0.5) == 'hard kill'
def test_send_PoisonPillMessage_set_actor_alive_to_False(self, init_actor): init_actor.send_control(PoisonPillMessage()) time.sleep(0.1) assert not init_actor.is_alive()