Class: Stoplight::Domain::Strategies::GreenRunStrategy Private

Inherits:
Object
  • Object
show all
Includes:
_RunStrategy
Defined in:
lib/stoplight/domain/strategies/green_run_strategy.rb,
sig/_private/stoplight/domain/strategies/green_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 green.

This strategy clears failures after successful execution and handles errors by either raising them or invoking a fallback if provided.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error_tracking_policy:, request_tracker:) ⇒ GreenRunStrategy

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 GreenRunStrategy.

Parameters:



13
14
15
16
# File 'lib/stoplight/domain/strategies/green_run_strategy.rb', line 13

def initialize(error_tracking_policy:, request_tracker:)
  @error_tracking_policy = error_tracking_policy
  @request_tracker = request_tracker
end

Instance Attribute Details

#request_trackerTracker::Request (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:



49
50
51
# File 'lib/stoplight/domain/strategies/green_run_strategy.rb', line 49

def request_tracker
  @request_tracker
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 green 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.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/stoplight/domain/strategies/green_run_strategy.rb', line 25

def execute(fallback, state_snapshot:, &code)
  # TODO: Consider implementing sampling rate to limit the memory footprint
  result = code.call
  record_success
  result
rescue => error
  if @error_tracking_policy.track?(error)
    record_error(error)

    if fallback
      fallback.call(error)
    else
      raise
    end
  else
    # User chose to not track the error, so we record it as a success
    record_success
    raise
  end
end

#record_error(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)


51
52
53
# File 'lib/stoplight/domain/strategies/green_run_strategy.rb', line 51

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

#record_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.



55
56
57
# File 'lib/stoplight/domain/strategies/green_run_strategy.rb', line 55

def record_success
  request_tracker.record_success
end