Class: Framework::DbListener
- Inherits:
-
Object
- Object
- Framework::DbListener
- Defined in:
- lib/framework/db_listener.rb
Instance Attribute Summary collapse
-
#channel ⇒ Object
readonly
Returns the value of attribute channel.
-
#db_name ⇒ Object
readonly
Returns the value of attribute db_name.
Instance Method Summary collapse
-
#initialize(channel: 'framework_notifications', db_name: 'default') ⇒ DbListener
constructor
A new instance of DbListener.
- #listen ⇒ Object
Constructor Details
#initialize(channel: 'framework_notifications', db_name: 'default') ⇒ DbListener
Returns a new instance of DbListener.
7 8 9 10 |
# File 'lib/framework/db_listener.rb', line 7 def initialize(channel: 'framework_notifications', db_name: 'default') @channel = channel.freeze @db_name = db_name.freeze end |
Instance Attribute Details
#channel ⇒ Object (readonly)
Returns the value of attribute channel.
5 6 7 |
# File 'lib/framework/db_listener.rb', line 5 def channel @channel end |
#db_name ⇒ Object (readonly)
Returns the value of attribute db_name.
5 6 7 |
# File 'lib/framework/db_listener.rb', line 5 def db_name @db_name end |
Instance Method Details
#listen ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/framework/db_listener.rb', line 12 def listen connection = Framework.app.db_connection(db_name) connection.execute "LISTEN #{channel}" loop do connection.raw_connection.wait_for_notify do |event, pid, data| yield data.nil? ? data : JSON.parse(data) end end ensure connection.execute "UNLISTEN #{channel}" end |