Class: Upperkut::BatchExecution

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/upperkut/batch_execution.rb

Instance Method Summary collapse

Methods included from Util

#decode_json_items, #encode_json_items, #to_underscore

Constructor Details

#initialize(worker, logger = Upperkut::Logging.logger) ⇒ BatchExecution

Returns a new instance of BatchExecution.

[View source]

7
8
9
10
# File 'lib/upperkut/batch_execution.rb', line 7

def initialize(worker, logger = Upperkut::Logging.logger)
  @worker = worker
  @logger = logger
end

Instance Method Details

#executeObject

[View source]

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/upperkut/batch_execution.rb', line 12

def execute
  worker_instance = @worker.new
  items = @worker.fetch_items.freeze

  items_body = items.collect do |item|
    item['body']
  end

  @worker.server_middlewares.invoke(@worker, items) do
    worker_instance.perform(items_body.dup)
  end
rescue StandardError => error
  @logger.info(
    action: :requeue,
    ex: error,
    item_size: items_body.size
  )

  @logger.error(error.backtrace.join("\n"))

  if worker_instance.respond_to?(:handle_error)
    worker_instance.handle_error(error, items_body)
    return
  else
    @worker.push_items(items_body)
  end

  raise error
end