Skip to content

Instantly share code, notes, and snippets.

@jsierles
Created November 25, 2009 20:51
Show Gist options
  • Select an option

  • Save jsierles/243015 to your computer and use it in GitHub Desktop.

Select an option

Save jsierles/243015 to your computer and use it in GitHub Desktop.
require 'json'
require 'bunny'
class AMQPNotifierQueue
def initialize(options = {})
@amqp = Bunny.new(options)
@amqp.start
@exchange = @amqp.exchange('rails', :type => :topic)
@hostname = `hostname -s`
@pid = $$
end
# name, start, finish, result, instrumenter_id, payload
def publish(*args)
routing_key = [args[0], args[4], @pid, @hostname].join(".")
@exchange.publish(args.to_json, :key => routing_key)
end
def subscribe(listener, pattern=nil, &block)
@queue = @amqp.queue
@queue.bind(@exchange, :key => pattern.to_s)
Thread.new do
@queue.subscribe { |msg| block.call(msg) }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment