Class: Groovestack::Base::Listeners::Database
- Inherits:
-
Object
- Object
- Groovestack::Base::Listeners::Database
- Defined in:
- lib/groovestack/base/listeners.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#connection_instance ⇒ Object
Returns the value of attribute connection_instance.
-
#db_channel ⇒ Object
Returns the value of attribute db_channel.
Instance Method Summary collapse
- #disconnect ⇒ Object
-
#initialize(db_channel) ⇒ Database
constructor
A new instance of Database.
- #listen ⇒ Object
- #unlisten ⇒ Object
Constructor Details
#initialize(db_channel) ⇒ Database
Returns a new instance of Database.
48 49 50 51 52 |
# File 'lib/groovestack/base/listeners.rb', line 48 def initialize(db_channel) @db_channel = db_channel @connection = nil @connection_instance = nil end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
46 47 48 |
# File 'lib/groovestack/base/listeners.rb', line 46 def connection @connection end |
#connection_instance ⇒ Object
Returns the value of attribute connection_instance.
46 47 48 |
# File 'lib/groovestack/base/listeners.rb', line 46 def connection_instance @connection_instance end |
#db_channel ⇒ Object
Returns the value of attribute db_channel.
46 47 48 |
# File 'lib/groovestack/base/listeners.rb', line 46 def db_channel @db_channel end |
Instance Method Details
#disconnect ⇒ Object
79 80 81 |
# File 'lib/groovestack/base/listeners.rb', line 79 def disconnect ::ActiveRecord::Base.connection_pool.checkin(connection) end |
#listen ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/groovestack/base/listeners.rb', line 54 def listen Rails.logger.info "Listening to #{db_channel}" ::ActiveRecord::Base.connection_pool.with_connection do |connection| @connection = connection @connection_instance = connection.instance_variable_get(:@connection) begin connection_instance.async_exec "LISTEN #{db_channel}" loop do connection_instance.wait_for_notify do |_channel, _pid, payload| yield payload if block_given? end end ensure unlisten end end end |
#unlisten ⇒ Object
75 76 77 |
# File 'lib/groovestack/base/listeners.rb', line 75 def unlisten connection_instance.async_exec "UNLISTEN #{db_channel}" end |