Class: Framework::DbListener

Inherits:
Object
  • Object
show all
Defined in:
lib/framework/db_listener.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#channelObject (readonly)

Returns the value of attribute channel.



5
6
7
# File 'lib/framework/db_listener.rb', line 5

def channel
  @channel
end

#db_nameObject (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

#listenObject



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