Sha256: cf95669e03e84bf8492a966b6da3f9955e64be4feba04042d18b3179bde6d025

Contents?: true

Size: 1.86 KB

Versions: 11

Compression:

Stored size: 1.86 KB

Contents

class Freddy
  module MessageHandlers
    def self.for_type(type)
      type == 'request' ? RequestHandler : StandardMessageHandler
    end

    class StandardMessageHandler
      def initialize(producer, logger)
        @producer = producer
        @logger = logger
      end

      def handle_message(payload, msg_handler, &block)
        block.call payload, msg_handler
      rescue Exception => e
        destination = msg_handler.destination
        @logger.error "Exception occured while processing message from #{destination}: #{Freddy.format_exception(e)}"
        Freddy.notify_exception(e, destination: destination)
      end

      def success(*)
        # NOP
      end

      def error(*)
        # NOP
      end
    end

    class RequestHandler
      def initialize(producer, logger)
        @producer = producer
        @logger = logger
      end

      def handle_message(payload, msg_handler, &block)
        @correlation_id = msg_handler.correlation_id

        if !@correlation_id
          @logger.error "Received request without correlation_id"
          Freddy.notify_exception(e)
        else
          block.call payload, msg_handler
        end
      rescue Exception => e
        @logger.error "Exception occured while handling the request with correlation_id #{@correlation_id}: #{Freddy.format_exception(e)}"
        Freddy.notify_exception(e, destination: msg_handler.destination, correlation_id: @correlation_id)
      end

      def success(reply_to, response)
        send_response(reply_to, response, type: 'success')
      end

      def error(reply_to, response)
        send_response(reply_to, response, type: 'error')
      end

      private

      def send_response(reply_to, response, opts = {})
        @producer.produce reply_to.force_encoding('utf-8'), response, {
          correlation_id: @correlation_id
        }.merge(opts)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
freddy-0.4.2 lib/freddy/message_handlers.rb
freddy-0.4.1 lib/freddy/message_handlers.rb
freddy-0.4.0 lib/freddy/message_handlers.rb
freddy-0.3.7 lib/freddy/message_handlers.rb
freddy-0.3.6 lib/freddy/message_handlers.rb
freddy-0.3.5 lib/freddy/message_handlers.rb
freddy-0.3.4 lib/freddy/message_handlers.rb
freddy-0.3.3 lib/freddy/message_handlers.rb
freddy-0.3.2 lib/freddy/message_handlers.rb
freddy-0.3.1 lib/freddy/message_handlers.rb
freddy-0.3.0 lib/freddy/message_handlers.rb