Class: CircuitSwitch::Reporter

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
lib/circuit_switch/workers/reporter.rb

Instance Method Summary collapse

Instance Method Details

#perform(key:, limit_count:, called_path:, stacktrace:, run:) ⇒ Object



8
9
10
11
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
# File 'lib/circuit_switch/workers/reporter.rb', line 8

def perform(key:, limit_count:, called_path:, stacktrace:, run:)
  # Wait for RunCountUpdater saves circuit_switch
  sleep(3) if run

  first_raise = true
  begin
    circuit_switch = key ? CircuitSwitch.find_by(key: key) : CircuitSwitch.find_by(caller: called_path)
    if run && circuit_switch.nil?
      raise ActiveRecord::RecordNotFound.new('Couldn\'t find CircuitSwitch::CircuitSwitch')
    end

    circuit_switch ||= CircuitSwitch.new(key: key, caller: called_path)
    circuit_switch.due_date ||= config.due_date
    circuit_switch.assign(report_limit_count: limit_count).increment_report_count!
    raise CalledNotification.new(circuit_switch.message)
  rescue ActiveRecord::RecordInvalid => e
    raise e unless first_raise

    first_raise = false
    sleep(2)
    retry
  rescue CalledNotification => notification
    notification.set_backtrace(stacktrace)
    if config.reporter.arity == 1
      config.reporter.call(notification.to_message(called_path: called_path))
    else
      config.reporter.call(notification.to_message(called_path: called_path), notification)
    end
  end
end