class EasyBunnyRPC::Worker

Public Class Methods

new(options={}) click to toggle source
# File lib/easy_bunny_rpc/worker.rb, line 6
def initialize(options={})
  @options = options
end

Public Instance Methods

close() click to toggle source
# File lib/easy_bunny_rpc/worker.rb, line 26
def close
  if defined?(@channel)
    channel.close
    remove_instance_variable :@channel
    remove_instance_variable :@queue
  end

  if defined?(@connection)
    connection.close
    remove_instance_variable :@connection
  end

  if defined?(@exchange)
    remove_instance_variable :@exchange
  end
end
publish_failure(payload) click to toggle source
# File lib/easy_bunny_rpc/worker.rb, line 14
def publish_failure(payload)
  publish false, payload
end
publish_success(payload) click to toggle source
# File lib/easy_bunny_rpc/worker.rb, line 10
def publish_success(payload)
  publish true, payload
end
subscribe() { |parse.first| ... } click to toggle source
# File lib/easy_bunny_rpc/worker.rb, line 18
def subscribe
  queue.subscribe(block: true) do |delivery_info, properties, payload|
    @delivery_info, @properties, @payload = delivery_info, properties, payload

    yield JSON.parse(payload).first
  end
end

Private Instance Methods

channel() click to toggle source
# File lib/easy_bunny_rpc/worker.rb, line 59
def channel
  return @channel if defined?(@channel)

  @channel = connection.create_channel
  @channel.prefetch(1)
  @channel
end
connection() click to toggle source
# File lib/easy_bunny_rpc/worker.rb, line 51
def connection
  return @connection if defined?(@connection)

  @connection = Bunny.new(@options[:bunny])
  @connection.start
  @connection
end
exchange() click to toggle source
# File lib/easy_bunny_rpc/worker.rb, line 71
def exchange
  @exchange ||= channel.default_exchange
end
publish(success, payload) click to toggle source
# File lib/easy_bunny_rpc/worker.rb, line 45
def publish(success, payload)
  obj = { 'success' => success, 'payload' => payload }.to_json

  exchange.publish(obj, routing_key: @properties.reply_to, correlation_id: @properties.correlation_id)
end
queue() click to toggle source
# File lib/easy_bunny_rpc/worker.rb, line 67
def queue
  @queue ||= channel.queue(@options[:queue])
end