Sha256: 1cbf891b079706b76b3377c643b09eaa1cf69c44c35b00c7c8ccb5a7697a8ea3

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 KB

Contents

require "sneakers"
require "sneakers_packer/version"
require "sneakers_packer/configuration"
require "sneakers_packer/message_packer"
require "sneakers_packer/common_worker"

require "sneakers_packer/rpc_worker"
require "sneakers_packer/rpc_request"
require "sneakers_packer/rpc_reply_subscriber"
require "sneakers_packer/rpc_client"

module SneakersPacker
  class RemoteCallTimeoutError < StandardError; end

  # sneakers_packer_mutex is mutex for class instance variables initialization
  @sneakers_packer_mutex = Mutex.new
  @publish_mutex = Mutex.new

  class << self
    # sender message to sneaker exchange
    # @param name route_key for message
    # @param data
    def publish(name, data)
      message = message_packer.pack_request(data)
      @publish_mutex.synchronize do
        publisher.publish message, to_queue: name
      end
    end

    # call remote service via rabbitmq rpc
    # @param name route_key for service
    # @param data
    # @param options{timeout} [int] timeout. seconds.   optional
    # @return result of service
    # @raise RemoteCallTimeoutError if timeout
    #
    def remote_call(name, data, options = {})
      request = RpcRequest.new name, data

      rpc_client.call request, options
    end

    # publisher is a singleton object
    def publisher
      if !@publisher
        @sneakers_packer_mutex.synchronize {
          @publisher ||= ::Sneakers::Publisher.new
        }
      end
      @publisher
    end

    # message_packer is a singleton object
    def message_packer
      if !@message_packer
        @sneakers_packer_mutex.synchronize {
          @message_packer ||= MessagePacker.new(self.conf.app_name)
        }
      end
      @message_packer
    end

    def rpc_client
      if !@rpc_client
        _publisher = publisher
        @sneakers_packer_mutex.synchronize {
          @rpc_client ||= RpcClient.new(_publisher)
        }
      end
      @rpc_client
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sneakers_packer-0.2.0 lib/sneakers_packer.rb
sneakers_packer-0.1.5 lib/sneakers_packer.rb