Module: Brown::ModuleMethods
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#log_level ⇒ Object
readonly
Returns the value of attribute log_level.
Instance Method Summary collapse
- #compile_acls ⇒ Object
- #config ⇒ Object
- #running? ⇒ Boolean
- #shutdown_hook(&block) ⇒ Object
- #start(opts = {}) ⇒ Object
- #stop(immediately = false, &blk) ⇒ Object
Methods included from Logger
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
14 15 16 |
# File 'lib/brown/module_methods.rb', line 14 def connection @connection end |
#log_level ⇒ Object (readonly)
Returns the value of attribute log_level.
14 15 16 |
# File 'lib/brown/module_methods.rb', line 14 def log_level @log_level end |
Instance Method Details
#compile_acls ⇒ Object
16 17 |
# File 'lib/brown/module_methods.rb', line 16 def compile_acls end |
#config ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/brown/module_methods.rb', line 19 def config Class.new.tap do |cfg| cfg.send(:define_method, :method_missing) do |*_args| Class.new.tap do |group| group.send(:define_method, :method_missing) do |*_args| "" end end.new end end.new end |
#running? ⇒ Boolean
31 32 33 |
# File 'lib/brown/module_methods.rb', line 31 def running? EM.reactor_running? end |
#shutdown_hook(&block) ⇒ Object
80 81 82 |
# File 'lib/brown/module_methods.rb', line 80 def shutdown_hook(&block) EM.add_shutdown_hook(&block) end |
#start(opts = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/brown/module_methods.rb', line 35 def start(opts={}) @log_level = opts[:log_level] || "info" # Why these are not the defaults, I will never know EM.epoll if EM.epoll? EM.kqueue if EM.kqueue? connection_settings = { :on_tcp_connection_failure => method(:tcp_connection_failure_handler), :on_possible_authentication_failure => method(:authentication_failure_handler) } AMQP.start(opts[:server_url], connection_settings) do |connection| EM.threadpool_size = 1 @connection = connection connection.on_connection do logger.info { "Connected to: AMQP Broker: #{broker_identifier(connection)}" } end connection.on_tcp_connection_loss do |connection, settings| logger.info { "Reconnecting to AMQP Broker: #{broker_identifier(connection)} in 5s" } connection.reconnect(false, 5) end connection.after_recovery do |connection| logger.info { "Connection with AMQP Broker restored: #{broker_identifier(connection)}" } end connection.on_error do |connection, connection_close| # If the broker is gracefully shutdown we get a 320. Log a nice message. if connection_close.reply_code == 320 logger.info { "AMQP Broker shutdown: #{broker_identifier(connection)}" } else logger.warn { connection_close.reply_text } end end # This will be the last thing run by the reactor. shutdown_hook { logger.debug { "Reactor Stopped" } } yield if block_given? end end |
#stop(immediately = false, &blk) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/brown/module_methods.rb', line 84 def stop(immediately=false, &blk) shutdown_hook(&blk) if blk if running? if immediately EM.next_tick do @connection.close { EM.stop_event_loop } end else EM.add_timer(1) do @connection.close { EM.stop_event_loop } end end else logger.fatal { "Eventmachine is not running, exiting with prejudice" } exit! end end |