Guest User

Untitled

a guest
Apr 25th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. require 'json'
  2. require 'bunny'
  3.  
  4. class AMQPNotifierQueue
  5. def initialize(options = {})
  6. @amqp = Bunny.new(options)
  7. @amqp.start
  8.  
  9. @exchange = @amqp.exchange('rails', :type => :topic)
  10. @hostname = `hostname -s`
  11. @pid = $$
  12. end
  13.  
  14. # name, start, finish, result, instrumenter_id, payload
  15. def publish(*args)
  16. routing_key = [args[0], args[4], @pid, @hostname].join(".")
  17. @exchange.publish(args.to_json, :key => routing_key)
  18. end
  19.  
  20. def subscribe(listener, pattern=nil, &block)
  21. @queue = @amqp.queue
  22. @queue.bind(@exchange, :key => pattern.to_s)
  23. Thread.new do
  24. @queue.subscribe { |msg| block.call(msg) }
  25. end
  26. end
  27. end
Add Comment
Please, Sign In to add comment