Class: StandardCircuit::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/standard_circuit/runner.rb

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



3
4
5
6
7
# File 'lib/standard_circuit/runner.rb', line 3

def initialize
  @lights = Concurrent::Map.new
  @forced_states = Concurrent::Map.new
  @config = Config.new
end

Instance Method Details

#apply_config!(config) ⇒ Object



9
10
11
12
# File 'lib/standard_circuit/runner.rb', line 9

def apply_config!(config)
  @config = config
  @lights.clear
end

#cached_lightsObject

Snapshot Hash of the current cached lights. Used by Health to discover prefix-matched circuits that have been exercised at least once — we can’t enumerate prefix-matched dynamic names any other way.



56
57
58
59
60
# File 'lib/standard_circuit/runner.rb', line 56

def cached_lights
  hash = {}
  @lights.each_pair { |name, light| hash[name] = light }
  hash
end

#force_closed(name, &block) ⇒ Object



26
27
28
# File 'lib/standard_circuit/runner.rb', line 26

def force_closed(name, &block)
  apply_force(name, :closed, &block)
end

#force_open(name, &block) ⇒ Object



22
23
24
# File 'lib/standard_circuit/runner.rb', line 22

def force_open(name, &block)
  apply_force(name, :open, &block)
end

#health_overall(snapshot = nil) ⇒ Object

Accepts an optional pre-computed snapshot so callers that also need the raw circuits list don’t read the data store twice (which could yield an inconsistent status/circuits pair). Prefer health_report when you need both.



70
71
72
# File 'lib/standard_circuit/runner.rb', line 70

def health_overall(snapshot = nil)
  Health.overall(snapshot || health_snapshot)
end

#health_reportObject

Atomic health report — takes a single snapshot and returns both the rolled-up status and the per-circuit list. Preferred for rendering a health-check endpoint so status and circuits describe the same moment.



77
78
79
80
# File 'lib/standard_circuit/runner.rb', line 77

def health_report
  snapshot = health_snapshot
  { status: Health.overall(snapshot), circuits: snapshot }
end

#health_snapshotObject



62
63
64
# File 'lib/standard_circuit/runner.rb', line 62

def health_snapshot
  Health.snapshot(self, @config)
end

#light_for(name) ⇒ Object



49
50
51
# File 'lib/standard_circuit/runner.rb', line 49

def light_for(name)
  @lights.compute_if_absent(name.to_sym) { build_light(name.to_sym) }
end

#reset!Object

Full state reset for tests. Clears the light cache, forced states, AND swaps the Stoplight data store to a fresh Memory instance. Clearing the cache alone is not sufficient: failure counters live in the data store, so a spec that deliberately trips a circuit would otherwise leak RedLight into later specs. We only swap the store when the existing one is already a Memory store, because replacing a Redis store (production config) would silently defeat shared-process coordination.



41
42
43
44
45
46
47
# File 'lib/standard_circuit/runner.rb', line 41

def reset!
  @lights.clear
  @forced_states.clear
  return unless @config.data_store.is_a?(Stoplight::DataStore::Memory)

  @config.data_store = Stoplight::DataStore::Memory.new
end

#reset_force!Object



30
31
32
# File 'lib/standard_circuit/runner.rb', line 30

def reset_force!
  @forced_states.clear
end

#run(name, fallback: nil, &block) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/standard_circuit/runner.rb', line 14

def run(name, fallback: nil, &block)
  forced = @forced_states[name.to_sym]
  return run_forced_open(name, fallback) if forced == :open
  return yield if forced == :closed

  execute(name, fallback, &block)
end