def __log_receival_one_message(self, message): if self.__first_message_receival: logdebug(LOGGER, 'Handing over first message to rabbit thread...') self.__first_message_receival = False logtrace(LOGGER, 'Handing over one message over to the rabbit thread (%s)', message) log_every_x_times(LOGGER, self.__logcounter_received, self.__LOGFREQUENCY, 'Handing over one message over to the rabbit thread (no. %i).', self.__logcounter_received) self.__logcounter_received += 1
def __try_publishing_otherwise_put_back_to_stack(self, message): try: # Getting message info: properties = self.nodemanager.get_properties_for_message_publications() routing_key, msg_string = rabbitutils.get_routing_key_and_string_message_from_message_if_possible(message) routing_key = self.nodemanager.adapt_routing_key_for_untrusted(routing_key) # Logging logtrace(LOGGER, 'Publishing message %i (key %s) (body %s)...', self.__delivery_number+1, routing_key, msg_string) # +1 because it will be incremented after the publish. log_every_x_times(LOGGER, self.__logcounter_trigger, self.__LOGFREQUENCY, 'Trying actual publish... (trigger no. %i).', self.__logcounter_trigger) logtrace(LOGGER, '(Publish to channel no. %i).', self.thread._channel.channel_number) # Actual publish to exchange self.thread._channel.basic_publish( exchange=self.thread.get_exchange_name(), routing_key=routing_key, body=msg_string, properties=properties, mandatory=defaults.RABBIT_MANDATORY_DELIVERY ) return True # If anything went wrong, put it back into the stack of # unpublished messages before re-raising the exception # for further handling: except Exception as e: success = False logwarn(LOGGER, 'Message was not published. Putting back to queue. Reason: %s: "%s"',e.__class__.__name__, repr(e)) self.thread.put_one_message_into_queue_of_unsent_messages(message) logtrace(LOGGER, 'Now (after putting back) left in queue to be published: %i messages.', self.thread.get_num_unpublished()) raise e
def __postparations_after_successful_feeding(self, msg): # Pass the successfully published message and its delivery_number # to the confirmer module, to wait for its confirmation. # Increase the delivery number for the next message. self.thread.put_to_unconfirmed_delivery_tags(self.__delivery_number) self.thread.put_to_unconfirmed_messages_dict(self.__delivery_number, msg) self.__delivery_number += 1 # Logging self.__logcounter_success += 1 log_every_x_times(LOGGER, self.__logcounter_success, self.__LOGFREQUENCY, 'Actual publish to channel done (trigger no. %i, publish no. %i).', self.__logcounter_trigger, self.__logcounter_success) logtrace(LOGGER, 'Publishing messages %i to RabbitMQ... done.', self.__delivery_number-1) if (self.__delivery_number-1 == 1): loginfo(LOGGER, 'First message published to RabbitMQ.') logdebug(LOGGER, 'Message published (no. %i)', self.__delivery_number-1)
def on_delivery_confirmation(self, method_frame): deliv_tag, confirmation_type, multiple = self.__get_confirm_info( method_frame) log_every_x_times(LOGGER, self.__logcounter, self.__LOGFREQUENCY, 'Received a confirm (%s)', confirmation_type) self.__logcounter += 1 if confirmation_type == 'ack': logtrace(LOGGER, 'Received "ACK" from messaging service.') self.__react_on_ack(deliv_tag, multiple) elif confirmation_type == 'nack': logtrace(LOGGER, 'Received "NACK" from messaging service.') self.__react_on_nack(deliv_tag, multiple) else: msg = 'Received asynchronous response of unknown type from messaging service.' logwarn(LOGGER, msg) raise UnknownServerResponse(msg + ':' + str(method_frame))
def __log_why_cannot_feed_the_rabbit_now(self): log_every_x_times( LOGGER, self.__logcounter_trigger, self.__LOGFREQUENCY, 'Cannot publish message to RabbitMQ (trigger no. %i).', self.__logcounter_trigger) if self.statemachine.is_WAITING_TO_BE_AVAILABLE(): logdebug( LOGGER, 'Cannot publish message to RabbitMQ yet, as the connection is not ready.' ) elif self.statemachine.is_NOT_STARTED_YET(): logerror( LOGGER, 'Cannot publish message to RabbitMQ, as the thread is not running yet.' ) elif self.statemachine.is_PERMANENTLY_UNAVAILABLE( ) or self.statemachine.is_FORCE_FINISHED(): if self.statemachine.detail_could_not_connect: logtrace( LOGGER, 'Could not publish message to RabbitMQ, as the connection failed.' ) if self.__have_not_warned_about_connection_fail_yet: logwarn( LOGGER, 'Could not publish message(s) to RabbitMQ. The connection failed definitively.' ) self.__have_not_warned_about_connection_fail_yet = False elif self.statemachine.get_detail_closed_by_publisher(): logtrace( LOGGER, 'Cannot publish message to RabbitMQ, as the connection was closed by the user.' ) if self.__have_not_warned_about_force_close_yet: logwarn( LOGGER, 'Could not publish message(s) to RabbitMQ. The sender was closed by the user.' ) self.__have_not_warned_about_force_close_yet = False else: if self.thread._channel is None: logerror( LOGGER, 'Very unexpected. Could not publish message(s) to RabbitMQ. There is no channel.' )
def __publish_message(self): self.__logcounter_trigger += 1 if self.statemachine.is_NOT_STARTED_YET( ) or self.statemachine.is_WAITING_TO_BE_AVAILABLE(): log_every_x_times( LOGGER, self.__logcounter_trigger, self.__LOGFREQUENCY, 'Received early trigger for feeding the rabbit (trigger %i).', self.__logcounter_trigger) self.__log_why_cannot_feed_the_rabbit_now() elif self.statemachine.is_AVAILABLE( ) or self.statemachine.is_AVAILABLE_BUT_WANTS_TO_STOP(): log_every_x_times( LOGGER, self.__logcounter_trigger, self.__LOGFREQUENCY, 'Received trigger for publishing message to RabbitMQ (trigger %i).', self.__logcounter_trigger) self.__log_publication_trigger() self.__publish_message_to_channel() elif self.statemachine.is_PERMANENTLY_UNAVAILABLE( ) or self.statemachine.is_FORCE_FINISHED(): log_every_x_times( LOGGER, self.__logcounter_trigger, self.__LOGFREQUENCY, 'Received late trigger for feeding the rabbit (trigger %i).', self.__logcounter_trigger) self.__log_why_cannot_feed_the_rabbit_now()