Class: BreakerMachines::CascadingCircuit
- Inherits:
-
CoordinatedCircuit
- Object
- Circuit
- CoordinatedCircuit
- BreakerMachines::CascadingCircuit
- Defined in:
- lib/breaker_machines/cascading_circuit.rb
Overview
CascadingCircuit extends the CoordinatedCircuit class with the ability to automatically trip dependent circuits when this circuit opens. This enables sophisticated failure cascade modeling, similar to how critical system failures in a starship would cascade to dependent subsystems.
Instance Attribute Summary collapse
-
#dependent_circuits ⇒ Object
readonly
Returns the value of attribute dependent_circuits.
-
#emergency_protocol ⇒ Object
readonly
Returns the value of attribute emergency_protocol.
Instance Method Summary collapse
-
#cascade_failure! ⇒ Object
Force cascade failure to all dependent circuits.
-
#cascade_info ⇒ Object
Provide cascade info for introspection.
-
#dependent_status ⇒ Object
Get the current status of all dependent circuits.
-
#dependents_compromised? ⇒ Boolean
Check if any dependent circuits are open.
-
#initialize(name, config = {}) ⇒ CascadingCircuit
constructor
A new instance of CascadingCircuit.
-
#summary ⇒ Object
Summary that includes cascade information.
-
#trip! ⇒ Object
Override the trip method to include cascading behavior.
Methods included from BreakerMachines::Circuit::CoordinatedStateManagement
#recovery_allowed?, #reset_allowed?
Constructor Details
#initialize(name, config = {}) ⇒ CascadingCircuit
Returns a new instance of CascadingCircuit.
26 27 28 29 30 31 |
# File 'lib/breaker_machines/cascading_circuit.rb', line 26 def initialize(name, config = {}) @dependent_circuits = Array(config.delete(:cascades_to)) @emergency_protocol = config.delete(:emergency_protocol) super end |
Instance Attribute Details
#dependent_circuits ⇒ Object (readonly)
Returns the value of attribute dependent_circuits.
24 25 26 |
# File 'lib/breaker_machines/cascading_circuit.rb', line 24 def dependent_circuits @dependent_circuits end |
#emergency_protocol ⇒ Object (readonly)
Returns the value of attribute emergency_protocol.
24 25 26 |
# File 'lib/breaker_machines/cascading_circuit.rb', line 24 def emergency_protocol @emergency_protocol end |
Instance Method Details
#cascade_failure! ⇒ Object
Force cascade failure to all dependent circuits
41 42 43 |
# File 'lib/breaker_machines/cascading_circuit.rb', line 41 def cascade_failure! perform_cascade end |
#cascade_info ⇒ Object
Provide cascade info for introspection
79 80 81 82 83 84 85 86 |
# File 'lib/breaker_machines/cascading_circuit.rb', line 79 def cascade_info BreakerMachines::CascadeInfo.new( dependent_circuits: @dependent_circuits, emergency_protocol: @emergency_protocol, cascade_triggered_at: @cascade_triggered_at&.value, dependent_status: dependent_status ) end |
#dependent_status ⇒ Object
Get the current status of all dependent circuits
46 47 48 49 50 51 52 53 |
# File 'lib/breaker_machines/cascading_circuit.rb', line 46 def dependent_status return {} if @dependent_circuits.empty? @dependent_circuits.each_with_object({}) do |circuit_name, status| circuit = BreakerMachines.registry.find(circuit_name) status[circuit_name] = circuit ? circuit.status_name : :not_found end end |
#dependents_compromised? ⇒ Boolean
Check if any dependent circuits are open
56 57 58 59 60 61 |
# File 'lib/breaker_machines/cascading_circuit.rb', line 56 def dependents_compromised? @dependent_circuits.any? do |circuit_name| circuit = BreakerMachines.registry.find(circuit_name) circuit&.open? end end |
#summary ⇒ Object
Summary that includes cascade information
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/breaker_machines/cascading_circuit.rb', line 64 def summary base_summary = super return base_summary if @dependent_circuits.empty? if @cascade_triggered_at&.value compromised_count = dependent_status.values.count(:open) " CASCADE TRIGGERED: #{compromised_count}/#{@dependent_circuits.length} dependent systems compromised." else " Monitoring #{@dependent_circuits.length} dependent systems." end base_summary + cascade_info_text end |
#trip! ⇒ Object
Override the trip method to include cascading behavior
34 35 36 37 38 |
# File 'lib/breaker_machines/cascading_circuit.rb', line 34 def trip! result = super perform_cascade if result && @dependent_circuits.any? result end |