/vendor/daemon_kit/lib/daemon_kit/amqp.rb

http://github.com/mperham/qanat · Ruby · 38 lines · 24 code · 9 blank · 5 comment · 0 complexity · 20d2a908dd4e2810a2f1be4683ba51d0 MD5 · raw file

  1. require 'yaml'
  2. module DaemonKit
  3. # Thin wrapper around the amqp gem, specifically designed to ease
  4. # configuration of a AMQP consumer daemon and provide some added
  5. # simplicity
  6. class AMQP
  7. @@instance = nil
  8. class << self
  9. def instance
  10. @instance ||= new
  11. end
  12. private :new
  13. def run(&block)
  14. instance.run(&block)
  15. end
  16. end
  17. def initialize( config = {} )
  18. @config = DaemonKit::Config.load('amqp').to_h( true )
  19. end
  20. def run(&block)
  21. # Ensure graceful shutdown of the connection to the broker
  22. DaemonKit.trap('INT') { ::AMQP.stop { ::EM.stop } }
  23. DaemonKit.trap('TERM') { ::AMQP.stop { ::EM.stop } }
  24. # Start our event loop and AMQP client
  25. DaemonKit.logger.debug("AMQP.start(#{@config.inspect})")
  26. ::AMQP.start(@config, &block)
  27. end
  28. end
  29. end