Class: JayAPI::Abstract::WaitStrategy Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/jay_api/abstract/wait_strategy.rb

Overview

This class is abstract.

Subclass and override #wait_time to implement a custom WaitStrategy.

Abstract base class for implementing different waiting strategies. This class provides a framework for implementing a strategy that dictates how long to wait before retrying an operation, typically used in situations where an operation might need to be retried multiple times (like network requests, etc.)

Direct Known Subclasses

ConstantWait, GeometricWait

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wait_interval:, logger: nil) ⇒ WaitStrategy

Returns a new instance of WaitStrategy.

Parameters:

  • wait_interval (Integer)

    The initial time to wait before retrying.

  • logger (Logging::Logger) (defaults to: nil)

    The logger to be used for logging wait times, defaults to stdout.



16
17
18
19
# File 'lib/jay_api/abstract/wait_strategy.rb', line 16

def initialize(wait_interval:, logger: nil)
  @wait_interval = wait_interval
  @logger = logger || Logging.logger($stdout)
end

Instance Attribute Details

#wait_intervalObject (readonly)

Returns the value of attribute wait_interval.



12
13
14
# File 'lib/jay_api/abstract/wait_strategy.rb', line 12

def wait_interval
  @wait_interval
end

Instance Method Details

#waitObject

Executes the wait strategy. Logs the waiting time and pauses the execution for the determined wait time.



23
24
25
26
27
28
# File 'lib/jay_api/abstract/wait_strategy.rb', line 23

def wait
  wait_time.tap do |wait_time|
    logger.info("Sleeping: #{format('%.2f', wait_time)} s")
    Kernel.sleep(wait_time)
  end
end