Class: Batsir::Strategies::RetryStrategy
- Includes:
- Log
- Defined in:
- lib/batsir/strategies/retry_strategy.rb
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#retries ⇒ Object
readonly
Returns the value of attribute retries.
Attributes inherited from Strategy
Instance Method Summary collapse
- #execute(message, error) ⇒ Object
-
#initialize(context, retries = 3) ⇒ RetryStrategy
constructor
A new instance of RetryStrategy.
- #reset_attempts(message) ⇒ Object
Methods included from Log
Constructor Details
#initialize(context, retries = 3) ⇒ RetryStrategy
Returns a new instance of RetryStrategy.
8 9 10 11 12 |
# File 'lib/batsir/strategies/retry_strategy.rb', line 8 def initialize(context, retries = 3) super(context) @retries = retries @attempts = {} end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
6 7 8 |
# File 'lib/batsir/strategies/retry_strategy.rb', line 6 def attempts @attempts end |
#retries ⇒ Object (readonly)
Returns the value of attribute retries.
6 7 8 |
# File 'lib/batsir/strategies/retry_strategy.rb', line 6 def retries @retries end |
Instance Method Details
#execute(message, error) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/batsir/strategies/retry_strategy.rb', line 14 def execute(, error) @attempts[] ? @attempts[] += 1 : @attempts[] = 0 if @attempts[] >= @retries error_msg = "Tried to send '#{}' #{@attempts[]} times and failed" reset_attempts() log.error error_msg raise Batsir::Errors::RetryStrategyFailed.new error_msg else log.warn "Recovering from #{error}. Making another attempt (##{@attempts[]+1})" result = @context.execute() reset_attempts() return result end end |
#reset_attempts(message) ⇒ Object
30 31 32 |
# File 'lib/batsir/strategies/retry_strategy.rb', line 30 def reset_attempts() @attempts.delete() end |