Module: Protoboard::CircuitBreaker::ClassMethods

Defined in:
lib/protoboard/circuit_breaker.rb

Instance Method Summary collapse

Instance Method Details

#register_circuits(circuit_methods, on_before: [], on_after: [], options:, fallback: nil, singleton_methods: []) ⇒ Object

Registers a list of circuits to be executed

Attributes

  • circuit_methods - An array of symbols representing the names of the methods to be on a circuit or a hash containing a key with a symbol representing the name of the method and the value as the name of the circuit.

  • on_before - An array of callable objects with code to be executed before each circuit runs

  • on_after - An array of callable objects with code to be executed after each circuit runs

  • options - A hash containing the options needed for the circuit to execute

  • :service - A string representing the name of the service for the circuit

  • :open_after - An integer representing the number of errors to occur for the circuit to be opened

  • :cool_off_after - An integer representing the time in seconds for the circuit to attempt to recover

==== Example
options: {
  service: 'my_cool_service',
  open_after: 2,
  cool_off_after: 3
}
====
  • fallback - A callable object with code to be executed as an alternative plan if the code of the circuit fails



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
# File 'lib/protoboard/circuit_breaker.rb', line 31

def register_circuits(circuit_methods, on_before: [], on_after: [], options:, fallback: nil, singleton_methods: [])
  Protoboard::Helpers::VALIDATE_CALLBACKS.call(on_before)
  Protoboard::Helpers::VALIDATE_CALLBACKS.call(on_after)

  circuits = Protoboard::CircuitBreaker.create_circuits(
    circuit_methods,
    name,
    options.merge(
      fallback: fallback,
      on_before: on_before,
      on_after: on_after
    ),
    singleton_methods
  )

  circuits.each do |circuit|
    Protoboard::CircuitBreaker.add_circuit circuit
  end

  proxy_module = Protoboard::CircuitBreaker.create_circuit_proxy(circuits, name)

  prepend proxy_module::InstanceMethods

  singleton_class.prepend proxy_module::ClassMethods
end