Class: CircuitSwitch::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/circuit_switch/core.rb

Direct Known Subclasses

Builder

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#close_ifObject (readonly)

Returns the value of attribute close_if.



8
9
10
# File 'lib/circuit_switch/core.rb', line 8

def close_if
  @close_if
end

#close_if_reach_limitObject (readonly)

Returns the value of attribute close_if_reach_limit.



8
9
10
# File 'lib/circuit_switch/core.rb', line 8

def close_if_reach_limit
  @close_if_reach_limit
end

#initially_closedObject (readonly)

Returns the value of attribute initially_closed.



8
9
10
# File 'lib/circuit_switch/core.rb', line 8

def initially_closed
  @initially_closed
end

#keyObject (readonly)

Returns the value of attribute key.



8
9
10
# File 'lib/circuit_switch/core.rb', line 8

def key
  @key
end

#report_ifObject (readonly)

Returns the value of attribute report_if.



8
9
10
# File 'lib/circuit_switch/core.rb', line 8

def report_if
  @report_if
end

#report_limit_countObject (readonly)

Returns the value of attribute report_limit_count.



8
9
10
# File 'lib/circuit_switch/core.rb', line 8

def report_limit_count
  @report_limit_count
end

#run_ifObject (readonly)

Returns the value of attribute run_if.



8
9
10
# File 'lib/circuit_switch/core.rb', line 8

def run_if
  @run_if
end

#run_limit_countObject (readonly)

Returns the value of attribute run_limit_count.



8
9
10
# File 'lib/circuit_switch/core.rb', line 8

def run_limit_count
  @run_limit_count
end

#stop_report_ifObject (readonly)

Returns the value of attribute stop_report_if.



8
9
10
# File 'lib/circuit_switch/core.rb', line 8

def stop_report_if
  @stop_report_if
end

#stop_report_if_reach_limitObject (readonly)

Returns the value of attribute stop_report_if_reach_limit.



8
9
10
# File 'lib/circuit_switch/core.rb', line 8

def stop_report_if_reach_limit
  @stop_report_if_reach_limit
end

Instance Method Details

#execute_reportObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/circuit_switch/core.rb', line 39

def execute_report
  if config.reporter.nil?
    raise CircuitSwitchError.new('Set config.reporter.')
  end
  if config.reporter.arity == 1
    Logger.new($stdout).info('config.reporter now receives 2 arguments. Improve your `config/initialzers/circuit_switch.rb`.')
  end
  if stop_report_if_reach_limit && report_limit_count == 0
    raise CircuitSwitchError.new('Can\'t set limit_count to 0 when stop_report_if_reach_limit is true')
  end
  return self unless config.enable_report?
  return self if evaluate(stop_report_if) || !evaluate(report_if)
  return self if switch.report_is_terminated?
  return self if stop_report_if_reach_limit && switch.reached_report_limit?(report_limit_count)

  Reporter.perform_later(
    key: key,
    limit_count: report_limit_count,
    called_path: called_path,
    stacktrace: StacktraceModifier.call(backtrace: caller),
    run: run?
  )
  @reported = true
  self
end

#execute_run(&block) ⇒ Object



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/core.rb', line 11

def execute_run(&block)
  run_executable = false
  if close_if_reach_limit && run_limit_count == 0
    raise CircuitSwitchError.new('Can\'t set limit_count to 0 when close_if_reach_limit is true')
  end

  return self if evaluate(close_if) || !evaluate(run_if)
  return self if close_if_reach_limit && switch.reached_run_limit?(run_limit_count)
  return self if switch.run_is_terminated?

  run_executable = true
  unless switch.new_record? && initially_closed
    yield
    @run = true
  end
  self
ensure
  if run_executable
    RunCountUpdater.perform_later(
      key: key,
      limit_count: run_limit_count,
      called_path: called_path,
      reported: reported?,
      initially_closed: initially_closed
    )
  end
end

#reported?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/circuit_switch/core.rb', line 71

def reported?
  @reported
end

#run?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/circuit_switch/core.rb', line 66

def run?
  @run
end