Sha256: c60c829448f78605413fbf2bc535a5b90c8380d85aeacd0475400284eb91e903

Contents?: true

Size: 1.88 KB

Versions: 3

Compression:

Stored size: 1.88 KB

Contents

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

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

      def handle_message(payload, msg_handler, &block)
        block.call payload, msg_handler
      rescue Exception => e
        @logger.error "Exception occured while processing message from #{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, destination, logger)
        @producer = producer
        @logger = logger
        @destination = destination
      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, correlation_id: @correlation_id, destination: @destination)
      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

3 entries across 3 versions & 2 rubygems

Version Path
freddy-0.4.4 lib/freddy/message_handlers.rb
freddy-jruby-0.4.3 lib/freddy/message_handlers.rb
freddy-0.4.3 lib/freddy/message_handlers.rb