Sha256: 30feaef1364c332dad547cf9a4aae592b71a257a6d74cd0b09958e694d1b8ae7

Contents?: true

Size: 1.89 KB

Versions: 4

Compression:

Stored size: 1.89 KB

Contents

module Appfuel
  module Service
    class Worker
      include Sneakers::Worker
      include Appfuel::Application::AppContainer
      include Appfuel::Application::Dispatcher

      class << self
        def inherited(klass)
          container = klass.app_container
          consumer_key = "#{klass.top_container_key}.consumer_keys"
          unless container.key?(consumer_key)
            container.register(consumer_key, [])
          end
          key = klass.container_class_path
          container.register(key, klass)
          container[consumer_key] << key
        end

        def top_container_key
          "message_brokers"
        end

        def container_class_type
          'consumers'
        end
      end

      # Sneakers worker hook to handle messages from RabbitMQ
      #
      #
      # @param msg [String] JSON string of inputs
      # @param delivery_info [Bunny::Delivery::Info]
      # @param properties [Bunny::MessageProperties]
      # @return [Appfuel::Response]
      def work_with_params(msg, delivery_info, properties)
        container = app_container
        request   = create_request(msg, delivery_info, properties)
        response  = dispatch(request, container)
        if request.rpc?
          publish_rpc(request, response)
        end

        ack!
      end


      # Publish a response for the rpc request.
      #
      # @param request [MsgRequest]
      # @param respons [Appfuel::Response]
      # @return [Nil]
      def publish_rpc(request, response)
        options = {
          correlation_id: request.correlation_id,
          routing_key: request.reply_to,
          headers: { "action_route" => request.action_route }
        }
        publish(response.to_json, options)
        nil
      end

      private

      def create_request(msg, delivery_info, properties)
        Appfuel::Service::MsgRequest.new(msg, delivery_info, properties)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
appfuel-service-0.2.2 lib/appfuel/service/worker.rb
appfuel-service-0.2.1 lib/appfuel/service/worker.rb
appfuel-service-0.1.7 lib/appfuel/service/worker.rb
appfuel-service-0.1.6 lib/appfuel/service/worker.rb