Class: Toro::Fetcher

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

Instance Method Summary collapse

Methods included from Actor

included

Constructor Details

#initialize(options = {}) ⇒ Fetcher

Returns a new instance of Fetcher.



5
6
7
8
9
10
11
12
13
# File 'lib/toro/fetcher.rb', line 5

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

Instance Method Details

#fetchObject



22
23
24
25
# File 'lib/toro/fetcher.rb', line 22

def fetch
  job = retrieve
  @manager.async.assign(job) if job
end

#notifyObject



15
16
17
18
19
20
# File 'lib/toro/fetcher.rb', line 15

def notify
  if @manager.is_ready?
    job = retrieve
    @manager.assign(job) if job
  end
end

#retrieveObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/toro/fetcher.rb', line 27

def retrieve
  job = nil
  queue_list = @queues.map { |queue| "'#{queue}'" }.join(', ')
  sql = "SELECT * FROM toro_pop(ARRAY[#{queue_list}]::TEXT[], '#{Toro.process_identity}')"
  result = nil
  Toro::Database.with_connection do
    result = Toro::Database.query(sql).first
    result = nil if result['id'].nil?
  end
  return nil if result.nil?
  Job.instantiate(result)
end