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

Inherits:
Object
  • Object
show all
Defined in:
lib/stoplight/domain/strategies/yellow_run_strategy.rb

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 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 Method Details

#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