Class: Keen::Async::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/keen/async/worker.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Worker

Returns a new instance of Worker.



14
15
16
17
# File 'lib/keen/async/worker.rb', line 14

def initialize(client)
  @client = client
  @storage_handler = client.storage_handler
end

Instance Method Details

#batch_url(project_id) ⇒ Object



19
20
21
22
23
24
# File 'lib/keen/async/worker.rb', line 19

def batch_url(project_id)
  if not project_id
    raise "Missing project_id."
  end
  "https://api.keen.io/2.0/projects/#{project_id}/_events"
end

#process_queueObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/keen/async/worker.rb', line 26

def process_queue
  queue_length = @storage_handler.count_active_queue

  batch_size = Keen::Async::BATCH_SIZE


  events = []


  responses = []

  num_batches = queue_length / batch_size + 1
  num_batches.times do

    job_definitions = @storage_handler.get_authorized_jobs(batch_size, @client)

    job_definitions.each do |job_definition|
      #puts JSON.generate job_definition
      job = Keen::Async::Job.new(@client, job_definition)
      events.push Keen::Event.new(job.timestamp, job.collection_name, job.event_body)
    end

    responses.push @client.send_batch(events)
  end

  if @client.logging
    puts responses
  end

  responses

end