Class: Upperkut::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/upperkut/processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(worker, logger = Logging.logger) ⇒ Processor

Returns a new instance of Processor.



5
6
7
8
9
10
# File 'lib/upperkut/processor.rb', line 5

def initialize(worker, logger = Logging.logger)
  @worker = worker
  @strategy = worker.strategy
  @worker_instance = worker.new
  @logger = logger
end

Instance Method Details

#blocking_processObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/upperkut/processor.rb', line 43

def blocking_process
  sleeping_time = 0

  loop do
    break if @stopped

    if @strategy.process?
      sleeping_time = 0
      process
      next
    end

    sleeping_time += sleep(@worker.setup.polling_interval)
    @logger.debug(sleeping_time: sleeping_time)
  end
end

#processObject



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
41
# File 'lib/upperkut/processor.rb', line 12

def process
  items = @worker.fetch_items.freeze
  return unless items.any?

  @worker.server_middlewares.invoke(@worker, items) do
    @worker_instance.perform(items)
  end

  nacked_items, pending_ack_items = items.partition(&:nacked?)
  @strategy.nack(nacked_items) if nacked_items.any?
  @strategy.ack(pending_ack_items) if pending_ack_items.any?
rescue StandardError => error
  @logger.error(
    action: :handle_execution_error,
    ex: error.to_s,
    backtrace: error.backtrace.join("\n"),
    item_size: Array(items).size
  )

  if items
    if @worker_instance.respond_to?(:handle_error)
      @worker_instance.handle_error(error, items)
      return
    end

    @strategy.nack(items)
  end

  raise error
end

#stopObject



60
61
62
# File 'lib/upperkut/processor.rb', line 60

def stop
  @stopped = true
end