def __init__(self, consumer_id: int, message_type: str, consumer_group: str, event_filter: Filter, event_parser: Parser, subscriber: Subscriber): """ Initalize Watcher class to monitor message bus event. Args: consumer_id (int): Consumer ID for message bus. message_type (str): Message type for getting event. consumer_group (str): Consumer Group of message bus. event_filter (Filter): Filter unused event. event_parser (Parser): Parse event to HealthEvent subscriber (Subscriber): Pass event to Subscriber. """ Log.info(f"Initalizing watcher {message_type}-{str(consumer_id)}") self.consumer_id = consumer_id self.message_type = message_type self.consumer_group = consumer_group self.filter = event_filter self.parser = event_parser self.subscriber = subscriber self._validate() # TBD : Call MessageBus.init() from here if this watcher needs to be used in HA. Reference EOS-26999 self.consumer = MessageBus.get_consumer( consumer_id=str(self.consumer_id), consumer_group=self.consumer_group, message_type=self.message_type, callback=self.process_message)
def _register_for_resp(self): """ Register to wait for a response to the sent request. """ # Unique consumer_group for each actuator response self.consumer_group = self._uuid self.consumer_id = Conf.get(const.HA_GLOBAL_INDEX, f"ACTUATOR_MANAGER{_DELIM}consumer_id") self.resp_message_type = Conf.get( const.HA_GLOBAL_INDEX, f"ACTUATOR_MANAGER{_DELIM}resp_message_type") self.consumer = MessageBus.get_consumer( consumer_id=str(self.consumer_id), consumer_group=self.consumer_group, message_type=self.resp_message_type, callback=self.process_resp, offset="latest", timeout=ACTUATOR_MSG_WAIT_TIME) # Start the thread to listen to response self.consumer.start() Log.debug( f"Waiting to get response on message_type {self.resp_message_type}" )
def _get_consumer(self) -> MessageBusConsumer: """ Returns an object of MessageBusConsumer class which will listen on cluster_event message type and callback will be executed """ self._consumer_id = Conf.get(const.HA_GLOBAL_INDEX, f'FAULT_TOLERANCE{_DELIM}consumer_id') self._consumer_group = Conf.get(const.HA_GLOBAL_INDEX, f'FAULT_TOLERANCE{_DELIM}consumer_group') self._message_type = Conf.get(const.HA_GLOBAL_INDEX, f'FAULT_TOLERANCE{_DELIM}message_type') MessageBus.init() return MessageBus.get_consumer(consumer_id=self._consumer_id, \ consumer_group=self._consumer_group, \ message_type=self._message_type, \ callback=self.process_message)
def _get_consumer(self): """ Return instace of message bus consumer. """ consumer_id = Conf.get(HA_GLOBAL_INDEX, f"EVENT_MANAGER{_DELIM}consumer_id") consumer_group = Conf.get(HA_GLOBAL_INDEX, f"EVENT_MANAGER{_DELIM}consumer_group") message_type = Conf.get(HA_GLOBAL_INDEX, f"EVENT_MANAGER{_DELIM}message_type") MessageBus.init() return MessageBus.get_consumer(consumer_id=consumer_id, consumer_group=consumer_group, message_type=message_type, callback=self.process_event)
def setUp(self): """ Setup. """ self.message_type = "test_ha" MessageBus.deregister(self.message_type) # Consumer self.consumer_id = 1 self.consumer_group = "test_ha_group" # Producer producer_id = "ha_producer" self.producer = MessageBus.get_producer(producer_id=producer_id, message_type=self.message_type) self.status = None self.stop_thread = False self.count = 1 self.consumer = MessageBus.get_consumer( consumer_id=self.consumer_id, consumer_group=self.consumer_group, message_type=self.message_type, callback=self._callback)
print("********Event Publisher********") event_manager = EventManager.get_instance() component = "csm" resource_type = "node:fru:disk" state = "failed" message_type = event_manager.subscribe( 'csm', [SubscribeEvent(resource_type, [state])]) print(f"Subscribed {component}, message type is {message_type}") health_event = HealthEvent("csm", "1", "failed", "fault", "1", "1", "_1", "1", "1", "1", "node", "16215009572", "1", None) action_event = RecoveryActionEvent(health_event) event_manager.publish(action_event.get_event()) print("Consuming the action event") message_consumer = MessageBus.get_consumer( consumer_id="1", consumer_group='test_publisher', message_type=message_type, callback=receive) message_consumer.start() while not MSG: time.sleep(2) print("waiting for msg") message_consumer.stop() unsubscribe = event_manager.unsubscribe( 'csm', [SubscribeEvent(resource_type, [state])]) print(f"Unsubscribed {component}") print("Event Publisher test completed successfully") except Exception as e: print(f"Failed to run event manager publiser test, Error: {e}")