Class: IbmAppconfigurationRubySdk::BackgroundRetryManager

Inherits:
Object
  • Object
show all
Defined in:
lib/ibm_appconfiguration_ruby_sdk/background_retry_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection_id:, environment_id:, handler:, logger: nil) ⇒ BackgroundRetryManager

Initialize the retry manager

Parameters:

  • collection_id (String)

    Collection ID for API request

  • environment_id (String)

    Environment ID for API request

  • handler (ConfigurationHandler)

    Handler to push loaded configurations into

  • logger (Logger) (defaults to: nil)

    Optional logger instance (creates new one if not provided)



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ibm_appconfiguration_ruby_sdk/background_retry_manager.rb', line 50

def initialize(collection_id:, environment_id:, handler:, logger: nil)
  @collection_id = collection_id
  @environment_id = environment_id
  @handler = handler
  @logger = logger || Logger.instance

  # Initialize ConfigFetcher
  @config_fetcher = ConfigFetcher.new(
    collection_id: collection_id,
    environment_id: environment_id,
    handler: @handler,
    logger: @logger
  )

  # State management
  @active = false
  @attempt = 0
  @cap_ms = nil

  # Thread management
  @retry_thread = nil
  @mutex = Mutex.new

end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



42
43
44
# File 'lib/ibm_appconfiguration_ruby_sdk/background_retry_manager.rb', line 42

def active
  @active
end

#attemptObject (readonly)

Returns the value of attribute attempt.



42
43
44
# File 'lib/ibm_appconfiguration_ruby_sdk/background_retry_manager.rb', line 42

def attempt
  @attempt
end

Instance Method Details

#active?Boolean

Check if retry is currently active

Returns:

  • (Boolean)

    true if retry loop is running



139
140
141
# File 'lib/ibm_appconfiguration_ruby_sdk/background_retry_manager.rb', line 139

def active?
  @mutex.synchronize { @active }
end

#current_attemptInteger

Get current attempt number

Returns:

  • (Integer)

    Current retry attempt (0-based)



146
147
148
# File 'lib/ibm_appconfiguration_ruby_sdk/background_retry_manager.rb', line 146

def current_attempt
  @mutex.synchronize { @attempt }
end

#start(reason:) ⇒ Boolean

Start the background retry loop

This method:

  1. Checks if retry is already active (prevents duplicate retries)
  2. Initializes retry state (attempt counter, cap delay)
  3. Schedules the first retry attempt

Parameters:

  • reason (String)

    The reason for starting retry (for logging)

Returns:

  • (Boolean)

    true if started, false if already active



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ibm_appconfiguration_ruby_sdk/background_retry_manager.rb', line 84

def start(reason:)
  # Thread-safe check and initialization
  should_start = false

  @mutex.synchronize do
    if @active
      @logger.info("Background retry already active (attempt ##{@attempt}). Reason: #{reason}")
      return false
    end

    # Initialize retry state
    @active = true
    @attempt = 0
    @cap_ms = compute_cap_delay_ms
    should_start = true
  end

  if should_start
    cap_hours = (@cap_ms / 3_600_000.0).round(2)
    @logger.info("Starting background retry (cap #{cap_hours} hours). Reason: #{reason}")
    schedule_next_attempt(reason)
  end

  true
end

#stopObject

Stop the background retry loop

This method:

  1. Sets active flag to false (stops scheduling new attempts)
  2. Kills the current retry thread if running
  3. Resets all state variables

Thread-safe and idempotent (safe to call multiple times)



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ibm_appconfiguration_ruby_sdk/background_retry_manager.rb', line 118

def stop
  @mutex.synchronize do
    return unless @active

    @active = false
    @attempt = 0
    @cap_ms = nil
  end

  # Kill retry thread outside mutex to avoid deadlock
  if @retry_thread&.alive?
    @retry_thread.kill
    @retry_thread = nil
  end

  @logger.info(Constants::BACKGROUND_RETRY_STOPPED)
end