def delivery(self, info_list, info_type, server_id): try: # send heartbeat data self.__producer.send( self.__topic_heartbeat, { Mc.FIELD_SERVER_ID: server_id, Mc.MSG_TYPE: info_type, Mc.MSG_TIME: Mu.generate_check_id() }) self.__producer.flush() if info_list: # as producer doesn't support transaction # https://github.com/dpkp/kafka-python/issues/1396 # https://github.com/dpkp/kafka-python/issues/1063 # add message begin and end part, consumer will abandon all messages if missing begin or end # header Mu.log_debug( self.__logger, "Sending {0} message header to queue...".format(info_type)) self.__producer.send( self.__topic, MsgProducerService.__get_message_header( info_type, server_id)) # body for info in info_list: # for all messages, add type and server id info[Mc.MSG_TYPE] = info_type info[Mc.FIELD_SERVER_ID] = server_id Mu.log_debug( self.__logger, "Sending {0} info {1} to queue...".format( info_type, info)) self.__producer.send(self.__topic, info) Mu.log_debug( self.__logger, "{0} info {1} is sent to queue...".format( info_type, info)) # ending Mu.log_debug( self.__logger, "Sending {0} message ending to queue...".format(info_type)) self.__producer.send( self.__topic, MsgProducerService.__get_message_ending( info_type, server_id)) self.__producer.flush() Mu.log_debug( self.__logger, "Sending {0} message to queue is finished...".format( info_type)) except Exception as ex: Mu.log_error( self.__logger, "Some thing wrong when delivering, error: {0}".format(ex))
def run(self): """run the thread""" while True: check_id = Mu.generate_check_id() self.monitoring(check_id) time.sleep(self._get_interval())