def testEndMessageCloseReader(self, mocked_remove, mocked_filename):
     reader = mock.MagicMock()
     oc.end_message({
         'some': 'content',
         'contents_reader': reader
     }, 'unique name')
     reader.close.assert_called_once()
示例#2
0
 def testEndMessageRemoveFailed(self, mocked_print, mocked_remove, mocked_filename):
     mocked_remove.side_effect = Exception
     reader = mock.MagicMock()
     oc.end_message({'some': 'content', 'contents_reader': reader}, 'unique name')
     reader.close.assert_called_once()
     mocked_print.assert_called_once()
     self.assertTrue(mocked_print.call_args[0][0].startswith('Remove failed'))
    def testEndMessage(self, mocked_remove, mocked_filename):

        # End message without any contents_ref does nothing
        self.assertEqual(oc.end_message({}, {}), None)
        self.assertFalse(mocked_filename.called)
        self.assertFalse(mocked_remove.called)

        # End message with contents_ref gets the filename and removes it
        self.assertEqual(oc.end_message({}, "x"), None)
        mocked_filename.assert_called_with("x", "message_broker")
        mocked_remove.assert_called_with("filename")
            def run_message_handler():
                offload_id = None
                result = None
                msg = None
                try:
                    # Try to get the message, parse any json contents and retrieve any offloaded contents
                    # Include any queue specific parameters
                    params = {**self._params, **self._params.get(queue, {})}
                    msg, offload_id = get_message_from_body(body, params)
                    # Try to handle the message
                    result = message_handler(self, basic_deliver.exchange,
                                             queue, basic_deliver.routing_key,
                                             msg)
                except Exception as e:
                    # Print error message, the message that caused the error and a short stacktrace
                    stacktrace = traceback.format_exc(limit=-10)
                    print(
                        f"Message handling has failed: {str(e)}, message: {str(body)}",
                        stacktrace)
                    if basic_deliver.redelivered:
                        # When a message is redelivered then remove the message from the queue
                        print(
                            "Message handling has failed on second try, removing message"
                        )
                    else:
                        # Fatal fail program on first try
                        print(
                            "Message handling has failed, terminating program")
                        os._exit(os.EX_TEMPFAIL)

                if msg is not None:
                    # run fail-safe method to end the message
                    end_message(msg, offload_id)

                if result is not False:
                    # Default is to acknowledge message
                    # Only on an explicit return value of False the message keeps unacked.
                    channel.basic_ack(basic_deliver.delivery_tag)
                else:
                    # This prevents a task queue from executing the same message twice due to concurrency
                    # The original message should be handled
                    print("Message not acknowlegded, discarding message")
                    channel.basic_nack(basic_deliver.delivery_tag,
                                       requeue=False)
                def run_message_handler():
                    offload_id = None
                    result = None
                    msg = None
                    try:
                        # Try to get the message, parse any json contents and retrieve any offloaded contents
                        msg, offload_id = get_message_from_body()
                        # Try to handle the message
                        result = message_handler(self, basic_deliver.exchange, queue, basic_deliver.routing_key, msg)
                    except Exception as e:
                        # Print error message, the message that caused the error and a short stacktrace
                        stacktrace = traceback.format_exc(limit=-5)
                        print(f"Message handling has failed: {str(e)}, message: {str(body)}", stacktrace)

                    if msg is not None:
                        # run fail-safe method to end the message
                        end_message(msg, offload_id)

                    if result is not False:
                        # Default is to acknowledge message
                        # Only on an explicit return value of False the message keeps unacked.
                        channel.basic_ack(basic_deliver.delivery_tag)