Class: Fractor::Workflow::CircuitBreakerRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/fractor/workflow/circuit_breaker_registry.rb

Overview

Registry for managing circuit breakers across jobs

Provides centralized circuit breaker management, allowing multiple jobs to share circuit breakers or have isolated ones.

Examples:

Shared circuit breaker

registry = CircuitBreakerRegistry.new
breaker = registry.get_or_create("api", threshold: 5)

Per-job circuit breaker

registry = CircuitBreakerRegistry.new
breaker = registry.get_or_create("job_123", threshold: 3)

Circuit breaker orchestrator

registry = CircuitBreakerRegistry.new
orchestrator = registry.get_or_create_orchestrator("api", threshold: 5, job_name: "my_job")

Instance Method Summary collapse

Constructor Details

#initializeCircuitBreakerRegistry

Returns a new instance of CircuitBreakerRegistry.



24
25
26
27
28
# File 'lib/fractor/workflow/circuit_breaker_registry.rb', line 24

def initialize
  @breakers = {}
  @orchestrators = {}
  @mutex = Mutex.new
end

Instance Method Details

#all_statsHash

Get statistics for all circuit breakers and orchestrators

Returns:

  • (Hash)

    Map of key to circuit breaker/orchestrator statistics



97
98
99
100
101
# File 'lib/fractor/workflow/circuit_breaker_registry.rb', line 97

def all_stats
  breakers_stats = @breakers.transform_values(&:stats)
  orchestrators_stats = @orchestrators.transform_values(&:stats)
  breakers_stats.merge(orchestrators_stats)
end

#clearObject

Clear all circuit breakers and orchestrators



104
105
106
107
108
109
# File 'lib/fractor/workflow/circuit_breaker_registry.rb', line 104

def clear
  @mutex.synchronize do
    @breakers.clear
    @orchestrators.clear
  end
end

#get(key) ⇒ CircuitBreaker?

Get an existing circuit breaker

Parameters:

  • key (String)

    Unique identifier for the circuit breaker

Returns:



64
65
66
# File 'lib/fractor/workflow/circuit_breaker_registry.rb', line 64

def get(key)
  @breakers[key]
end

#get_or_create(key, **options) ⇒ CircuitBreaker

Get or create a circuit breaker

Parameters:

  • key (String)

    Unique identifier for the circuit breaker

  • options (Hash)

    Circuit breaker options

Options Hash (**options):

  • :threshold (Integer)

    Failure threshold

  • :timeout (Integer)

    Timeout in seconds

  • :half_open_calls (Integer)

    Test calls in half-open

Returns:



38
39
40
41
42
# File 'lib/fractor/workflow/circuit_breaker_registry.rb', line 38

def get_or_create(key, **options)
  @mutex.synchronize do
    @breakers[key] ||= CircuitBreaker.new(**options)
  end
end

#get_or_create_orchestrator(key, **options) ⇒ CircuitBreakerOrchestrator

Get or create a circuit breaker orchestrator

Parameters:

  • key (String)

    Unique identifier for the circuit breaker

  • options (Hash)

    Circuit breaker orchestrator options

Options Hash (**options):

  • :threshold (Integer)

    Failure threshold

  • :timeout (Integer)

    Timeout in seconds

  • :half_open_calls (Integer)

    Test calls in half-open

  • :job_name (String)

    Job name for logging

  • :debug (Boolean)

    Debug logging flag

Returns:



54
55
56
57
58
# File 'lib/fractor/workflow/circuit_breaker_registry.rb', line 54

def get_or_create_orchestrator(key, **options)
  @mutex.synchronize do
    @orchestrators[key] ||= CircuitBreakerOrchestrator.new(**options)
  end
end

#get_orchestrator(key) ⇒ CircuitBreakerOrchestrator?

Get an existing circuit breaker orchestrator

Parameters:

  • key (String)

    Unique identifier for the orchestrator

Returns:



72
73
74
# File 'lib/fractor/workflow/circuit_breaker_registry.rb', line 72

def get_orchestrator(key)
  @orchestrators[key]
end

#remove(key) ⇒ CircuitBreaker, ...

Remove a circuit breaker

Parameters:

  • key (String)

    Unique identifier for the circuit breaker

Returns:



80
81
82
83
84
# File 'lib/fractor/workflow/circuit_breaker_registry.rb', line 80

def remove(key)
  @mutex.synchronize do
    @breakers.delete(key) || @orchestrators.delete(key)
  end
end

#reset_allObject

Reset all circuit breakers and orchestrators



87
88
89
90
91
92
# File 'lib/fractor/workflow/circuit_breaker_registry.rb', line 87

def reset_all
  @mutex.synchronize do
    @breakers.each_value(&:reset)
    @orchestrators.each_value(&:reset!)
  end
end