Class: Toro::Listener

Inherits:
Object
  • Object
show all
Includes:
Actor
Defined in:
lib/toro/listener.rb

Instance Method Summary collapse

Methods included from Actor

included

Constructor Details

#initialize(options = {}) ⇒ Listener

Returns a new instance of Listener.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/toro/listener.rb', line 5

def initialize(options={})
  defaults = {
    queues: [Toro.options[:default_queue]]
  }
  options.reverse_merge!(defaults)
  @queues = options[:queues]
  @fetcher = options[:fetcher]
  @manager = options[:manager]
  @is_done = false
  raise 'No fetcher provided' if @fetcher.blank?
  raise 'No manager provided' if @manager.blank?
  @manager.register_actor(:listener, self)
end

Instance Method Details

#startObject



19
20
21
22
23
24
# File 'lib/toro/listener.rb', line 19

def start
  Toro::Database.with_connection do
    Toro::Database.raw_connection.async_exec(channels.map { |channel| "LISTEN #{channel}" }.join('; '))
    wait_for_notify
  end
end

#stopObject



26
27
28
29
# File 'lib/toro/listener.rb', line 26

def stop
  Toro::Database.raw_connection.async_exec(channels.map { |channel| "UNLISTEN #{channel}" }.join('; '))
  @is_done = true
end