Class: Vx::Consumer::Session
- Inherits:
-
Object
- Object
- Vx::Consumer::Session
- Includes:
- Instrument
- Defined in:
- lib/vx/consumer/session.rb
Constant Summary collapse
- @@session_lock =
Mutex.new
- @@shutdown_lock =
Mutex.new
- @@shutdown =
ConditionVariable.new
- @@live =
false
Instance Attribute Summary collapse
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
Instance Method Summary collapse
- #close ⇒ Object
- #conn_info ⇒ Object
- #declare_exchange(ch, name, options = nil) ⇒ Object
- #declare_queue(ch, name, options = nil) ⇒ Object
- #live? ⇒ Boolean
- #open(options = {}) ⇒ Object
- #open? ⇒ Boolean
- #resume ⇒ Object
- #shutdown ⇒ Object
- #wait_shutdown(timeout = nil) ⇒ Object
- #with_channel ⇒ Object
Methods included from Instrument
Instance Attribute Details
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
16 17 18 |
# File 'lib/vx/consumer/session.rb', line 16 def conn @conn end |
Instance Method Details
#close ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/vx/consumer/session.rb', line 40 def close if open? @@session_lock.synchronize do instrument("closing_collection", info: conn_info) instrument("close_collection", info: conn_info) do begin conn.close while conn.status != :closed sleep 0.01 end rescue Bunny::ChannelError, Bunny::ClientTimeout => e Consumer.exception_handler(e, {}) end end @conn = nil end end end |
#conn_info ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/vx/consumer/session.rb', line 93 def conn_info if conn "amqp://#{conn.user}@#{conn.host}:#{conn.port}/#{conn.vhost}" else "not connected" end end |
#declare_exchange(ch, name, options = nil) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/vx/consumer/session.rb', line 107 def declare_exchange(ch, name, = nil) assert_connection_is_open ||= {} ch.exchange name, end |
#declare_queue(ch, name, options = nil) ⇒ Object
114 115 116 117 118 119 |
# File 'lib/vx/consumer/session.rb', line 114 def declare_queue(ch, name, = nil) assert_connection_is_open ||= {} ch.queue name, end |
#live? ⇒ Boolean
25 26 27 |
# File 'lib/vx/consumer/session.rb', line 25 def live? @@live end |
#open(options = {}) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/vx/consumer/session.rb', line 60 def open( = {}) return self if open? @@session_lock.synchronize do unless open? resume @conn ||= Bunny.new( nil, # from ENV['RABBITMQ_URL'] heartbeat: Consumer.configuration.heartbeat, automatically_recover: false ) instrumentation = { info: conn_info }.merge() instrument("start_connecting", instrumentation) instrument("connect", instrumentation) do conn.start while conn.connecting? sleep 0.01 end end end end self end |
#open? ⇒ Boolean
89 90 91 |
# File 'lib/vx/consumer/session.rb', line 89 def open? conn && conn.open? && conn.status == :open end |
#resume ⇒ Object
29 30 31 |
# File 'lib/vx/consumer/session.rb', line 29 def resume @@live = true end |
#shutdown ⇒ Object
18 19 20 21 22 23 |
# File 'lib/vx/consumer/session.rb', line 18 def shutdown @@shutdown_lock.synchronize do @@live = false @@shutdown.broadcast end end |
#wait_shutdown(timeout = nil) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/vx/consumer/session.rb', line 33 def wait_shutdown(timeout = nil) @@shutdown_lock.synchronize do @@shutdown.wait(@@shutdown_lock, timeout) not live? end end |
#with_channel ⇒ Object
101 102 103 104 105 |
# File 'lib/vx/consumer/session.rb', line 101 def with_channel assert_connection_is_open conn.with_channel { |ch| yield ch } end |