Class: Sbmt::Outbox::ProcessItem

Inherits:
DryInteractor show all
Defined in:
app/interactors/sbmt/outbox/process_item.rb

Constant Summary collapse

METRICS_COUNTERS =
%i[error_counter retry_counter sent_counter fetch_error_counter discarded_counter].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DryInteractor

call

Instance Attribute Details

#process_latencyObject

Returns the value of attribute process_latency.



18
19
20
# File 'app/interactors/sbmt/outbox/process_item.rb', line 18

def process_latency
  @process_latency
end

Instance Method Details

#callObject



20
21
22
23
24
25
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
58
59
60
# File 'app/interactors/sbmt/outbox/process_item.rb', line 20

def call
  log_success(
    "Start processing #{box_type} item.\n" \
    "Record: #{item_class.name}##{item_id}"
  )

  item = nil

  item_class.transaction do
    item = yield fetch_item

    if item.processed_at?
      item.config.retry_strategies.each do |retry_strategy|
        yield check_retry_strategy(item, retry_strategy)
      end
    else
      self.process_latency = Time.current - item.created_at
    end

    middlewares = Middleware::Builder.new(item_process_middlewares)
    payload = yield build_payload(item)
    transports = yield fetch_transports(item)

    middlewares.call(item) do
      transports.each do |transport|
        yield process_item(transport, item, payload)
      end

      track_successed(item)

      Success(item)
    end
  rescue Dry::Monads::Do::Halt => e
    e.result
  rescue => e
    track_failed(e, item)
    Failure(e.message)
  end
ensure
  report_metrics(item)
end