Class: Stoplight::Domain::Strategies::YellowRunStrategy Private

Inherits:
Object
  • Object
show all
Includes:
_RunStrategy
Defined in:
lib/stoplight/domain/strategies/yellow_run_strategy.rb,
sig/_private/stoplight/domain/strategies/yellow_run_strategy.rbs

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Defines how the light executes when it is yellow.

This strategy clears failures after successful execution and notifies about color switch from Red to Green. It also handles errors by either raising them or invoking a fallback if provided.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, error_tracking_policy:, notifiers:, request_tracker:, red_run_strategy:, state_store:, metrics_store:, recovery_lock_store:, config:) ⇒ YellowRunStrategy

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of YellowRunStrategy.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/stoplight/domain/strategies/yellow_run_strategy.rb', line 14

def initialize(
  name:,
  error_tracking_policy:,
  notifiers:,
  request_tracker:,
  red_run_strategy:,
  state_store:,
  metrics_store:,
  recovery_lock_store:,
  config: # FIXME: needed for backward compatibility, remove when notifier accepts light config
)
  @notifiers = notifiers
  @request_tracker = request_tracker
  @red_run_strategy = red_run_strategy
  @state_store = state_store
  @metrics_store = metrics_store
  @recovery_lock_store = recovery_lock_store
  @name = name
  @error_tracking_policy = error_tracking_policy
  @config = config
end

Instance Attribute Details

#metrics_store_MetricsStore (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



77
78
79
# File 'lib/stoplight/domain/strategies/yellow_run_strategy.rb', line 77

def metrics_store
  @metrics_store
end

#notifiersArray[state_transition_notifier] (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Array[state_transition_notifier])


73
74
75
# File 'lib/stoplight/domain/strategies/yellow_run_strategy.rb', line 73

def notifiers
  @notifiers
end

#recovery_lock_store_RecoveryLockStore (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



78
79
80
# File 'lib/stoplight/domain/strategies/yellow_run_strategy.rb', line 78

def recovery_lock_store
  @recovery_lock_store
end

#red_run_strategyRedRunStrategy (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



75
76
77
# File 'lib/stoplight/domain/strategies/yellow_run_strategy.rb', line 75

def red_run_strategy
  @red_run_strategy
end

#request_trackerTracker::RecoveryProbe (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



74
75
76
# File 'lib/stoplight/domain/strategies/yellow_run_strategy.rb', line 74

def request_tracker
  @request_tracker
end

#state_storeDomain::_StateStore (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



76
77
78
# File 'lib/stoplight/domain/strategies/yellow_run_strategy.rb', line 76

def state_store
  @state_store
end

Instance Method Details

#enter_recovery(state_snapshot) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/stoplight/domain/strategies/yellow_run_strategy.rb', line 101

def enter_recovery(state_snapshot)
  return if state_snapshot.recovery_started?

  state_store.transition_to_color(Color::YELLOW)
  metrics_store.clear
  # FIXME: use light config instead of @_config
  # light_info = LightInfo.new(name: @name)
  notifiers.each do |notifier|
    notifier.notify(@config, Color::RED, Color::YELLOW, nil)
  end
end

#execute(fallback, state_snapshot:) { ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Executes the provided code block when the light is in the yellow state.

Parameters:

  • fallback

    A fallback proc to execute in case of an error.

  • state_snapshot

Yields:

  • The code block to execute.

Returns:

  • The result of the code block if successful.

Raises:

  • Re-raises the error if it is not tracked or no fallback is provided.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/stoplight/domain/strategies/yellow_run_strategy.rb', line 43

def execute(fallback, state_snapshot:, &code)
  # Everything withing this block executed exclusively:
  #   - enter recovery
  #   - execute user's code
  #   - record outcome
  #   - transition to green or red if needed
  with_recovery_lock(fallback:, state_snapshot:, code:) do
    enter_recovery(state_snapshot)

    result = code.call
    record_recovery_probe_success
    result
  rescue => error
    if @error_tracking_policy.track?(error)
      record_recovery_probe_failure(error)

      if fallback
        fallback.call(error)
      else
        raise
      end
    else
      record_recovery_probe_success
      raise
    end
  end
end

#record_recovery_probe_failure(error) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

  • (StandardError)


97
98
99
# File 'lib/stoplight/domain/strategies/yellow_run_strategy.rb', line 97

def record_recovery_probe_failure(error)
  request_tracker.record_failure(error)
end

#record_recovery_probe_successvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.



93
94
95
# File 'lib/stoplight/domain/strategies/yellow_run_strategy.rb', line 93

def record_recovery_probe_success
  request_tracker.record_success
end

#with_recovery_lock(fallback:, state_snapshot:, code:) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/stoplight/domain/strategies/yellow_run_strategy.rb', line 80

def with_recovery_lock(fallback:, state_snapshot:, code:)
  recovery_lock_token = recovery_lock_store.acquire_lock
  if recovery_lock_token.nil?
    return red_run_strategy.execute(fallback, state_snapshot:, &code)
  end

  begin
    yield
  ensure
    recovery_lock_store.release_lock(recovery_lock_token)
  end
end