module Protoboard::CircuitBreaker

This module is responsible to manage the circuits.

Public Class Methods

add_circuit(circuit) click to toggle source

Adds a circuit to the list of registered circuits.

# File lib/protoboard/circuit_breaker.rb, line 73
def add_circuit(circuit)
  circuits << circuit
end
circuits() click to toggle source

Returns a list of circuits.

# File lib/protoboard/circuit_breaker.rb, line 79
def circuits
  @circuits ||= []
end
create_circuit_proxy(circuits, class_name) click to toggle source

Calls the module responsible for creating the proxy module that will execute the circuit.

# File lib/protoboard/circuit_breaker.rb, line 85
def create_circuit_proxy(circuits, class_name)
  CircuitProxyFactory.create_module(circuits, class_name)
end
create_circuits(circuit_methods,class_name, options, singleton_methods) click to toggle source

Creates a new circuit.

# File lib/protoboard/circuit_breaker.rb, line 91
def create_circuits(circuit_methods,class_name, options, singleton_methods)
  circuit_hash = case circuit_methods
                 when Array
                   circuit_methods.reduce({}) do |memo, value|
                     memo.merge(value.to_sym => "#{formatted_namespace}#{options[:service]}/#{class_name}\##{value}")
                   end
                 when Hash
                   circuit_methods.map { |key, value| [key, "#{formatted_namespace}#{value}"] }.to_h
                 else
                   raise ArgumentError, 'Invalid input for circuit methods'
                 end

  circuit_hash.map do |circuit_method, circuit_name|
    Circuit.new({
      name: circuit_name,
      method_name: circuit_method,
      singleton_method: singleton_methods.include?(circuit_method.to_sym)
    }
    .merge(options))
  end
end
included(klass) click to toggle source
# File lib/protoboard/circuit_breaker.rb, line 113
def included(klass)
  klass.extend(ClassMethods)
end
registered_circuits() click to toggle source

Returns a list of registered circuits.

# File lib/protoboard/circuit_breaker.rb, line 67
def registered_circuits
  circuits
end
services_healthcheck(with_namespace: true) click to toggle source

Returns a hash with the circuits names and its states.

# File lib/protoboard/circuit_breaker.rb, line 61
def services_healthcheck(with_namespace: true)
  Protoboard::Helpers::ServicesHealthcheckGenerator.new.call(with_namespace: with_namespace)
end

Private Class Methods

formatted_namespace() click to toggle source

Formats the namespace considering the configuration given when the gem starts

# File lib/protoboard/circuit_breaker.rb, line 121
def formatted_namespace
  !Protoboard.config.namespace.empty? ? "#{Protoboard.config.namespace}/" : ''
end