Class: InnerClient

Inherits:
ClientCallback show all
Defined in:
lib/ff/ruby/server/sdk/api/inner_client.rb

Constant Summary

Constants inherited from ClientCallback

ClientCallback::TBI

Instance Method Summary collapse

Methods inherited from ClientCallback

#on_authorized

Constructor Details

#initialize(api_key = nil, config = nil, connector = nil) ⇒ InnerClient

Returns a new instance of InnerClient.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 19

def initialize(api_key = nil, config = nil, connector = nil)

  if api_key == nil || api_key == ""
    SdkCodes::raise_missing_sdk_key config.logger
  end

  if config == nil

    @config = ConfigBuilder.new.build
  else

    unless config.kind_of?(Config)

      raise "The 'config' parameter must be of '" + Config.to_s + "' data type"
    end

    @config = config
  end

  @config.logger.debug "Ruby SDK version: " + Ff::Ruby::Server::Sdk::VERSION

  if connector == nil

    @connector = HarnessConnector.new(api_key, config = @config, on_unauthorized = self)

  else

    unless connector.kind_of?(Connector)

      raise "The 'connector' parameter must be of '" + Connector.to_s + "' data type"
    end

    @connector = connector
  end
  @condition = ConditionVariable.new

  @closing = false
  @failure = false
  @initialized = false
  @poller_ready = false
  @stream_ready = false
  @metrics_ready = false

  @my_mutex = Mutex.new

  @repository_callback = InnerClientRepositoryCallback.new(@config.logger)

  setup
end

Instance Method Details

#bool_variation(identifier, target, default_value) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 69

def bool_variation(identifier, target, default_value)
  unless @initialized
    log_sdk_not_initialized_warning(identifier, default_value)
    return default_value
  end
  @evaluator.bool_variation(identifier, target, default_value, @evaluator_callback)
end

#closeObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 134

def close

  @config.logger.info "Closing the client: " + self.to_s

  @closing = true

  off

  @auth_service.close
  @repository.close
  @poll_processor.close
  @update_processor.close
  @metrics_processor.close
  @connector.close
end

#initializedObject



101
102
103
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 101

def initialized
  @initialized
end

#is_closingObject



150
151
152
153
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 150

def is_closing

  @closing
end

#json_variation(identifier, target, default_value) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 93

def json_variation(identifier, target, default_value)
  unless @initialized
    log_sdk_not_initialized_warning(identifier, default_value)
    return default_value
  end
  @evaluator.json_variation(identifier, target, default_value, @evaluator_callback)
end

#number_variation(identifier, target, default_value) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 85

def number_variation(identifier, target, default_value)
  unless @initialized
    log_sdk_not_initialized_warning(identifier, default_value)
    return default_value
  end
  @evaluator.number_variation(identifier, target, default_value, @evaluator_callback)
end

#offObject



155
156
157
158
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 155

def off

  # TODO: Implement - Reactivity support
end

#on_auth_failedObject



126
127
128
129
130
131
132
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 126

def on_auth_failed
  @my_mutex.synchronize do
    SdkCodes::warn_auth_failed_srv_defaults @config.logger
    @initialized = true
    @condition.broadcast
  end
end

#on_auth_successObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 105

def on_auth_success

  SdkCodes::info_sdk_auth_ok @config.logger

  if @closing

    return
  end

  @poll_processor.start

  if @config.stream_enabled
    @update_processor.start
  end

  if @config.analytics_enabled
    @metrics_processor.start
  end

end

#on_metrics_processor_readyObject



212
213
214
215
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 212

def on_metrics_processor_ready

  on_processor_ready(@metrics_processor)
end

#on_poller_error(e) ⇒ Object



187
188
189
190
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 187

def on_poller_error(e)

  @config.logger.error "Poller error: " + e.to_s
end

#on_poller_iteration(poller) ⇒ Object



192
193
194
195
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 192

def on_poller_iteration(poller)

  @config.logger.debug "Poller iterated" + poller.to_s
end

#on_poller_ready(poller) ⇒ Object



182
183
184
185
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 182

def on_poller_ready(poller)

  on_processor_ready(poller)
end

#on_processor_ready(processor) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 217

def on_processor_ready(processor)
  @my_mutex.synchronize do

    if @closing

      return
    end

    if processor == @poll_processor

      @poller_ready = true
      @config.logger.debug "PollingProcessor ready"
    end

    if processor == @update_processor

      @stream_ready = true
      @config.logger.debug "Updater ready"
    end

    if processor == @metrics_processor

      @metrics_ready = true
      @config.logger.debug "Metrics ready"
    end

    if (@config.stream_enabled && !@stream_ready) ||
      (@config.analytics_enabled && !@metrics_ready) ||
      !@poller_ready

      return
    end

    SdkCodes.info_sdk_init_ok @config.logger

    @condition.broadcast
    @initialized = true
  end
end

#on_unauthorizedObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 160

def on_unauthorized

  if @closing

    return
  end

  @poll_processor.stop

  if @config.stream_enabled

    @update_processor.stop
  end

  if @config.analytics_enabled

    @metrics_processor.stop
  end

  @auth_service.start_async
end

#on_update_processor_readyObject



207
208
209
210
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 207

def on_update_processor_ready

  on_processor_ready(@update_processor)
end

#string_variation(identifier, target, default_value) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 77

def string_variation(identifier, target, default_value)
  unless @initialized
    log_sdk_not_initialized_warning(identifier, default_value)
    return default_value
  end
  @evaluator.string_variation(identifier, target, default_value, @evaluator_callback)
end

#update(message, manual) ⇒ Object



197
198
199
200
201
202
203
204
205
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 197

def update(message, manual)

  if @config.stream_enabled && manual

    @config.logger.warn "You run the update method manually with the stream enabled. Please turn off the stream in this case."
  end

  @update_processor.update(message)
end

#wait_for_initialization(timeout: nil) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 257

def wait_for_initialization(timeout: nil)
  SdkCodes::info_sdk_waiting_to_initialize(@config.logger, timeout)
  return if @initialized

  @my_mutex.synchronize do
    start_time = Time.now
    remaining = timeout ? timeout / 1000.0 : nil # Convert timeout to seconds

    until @initialized
      # Break if timeout has elapsed
      if remaining && remaining <= 0
        @config.logger.warn "The SDK has timed out waiting to initialize with supplied timeout #{timeout} ms. The SDK will continue to initialize in the background.  Default variations will be served until the SDK initializes."
        break
      end
      # Wait for the signal or timeout
      @condition.wait(@my_mutex, remaining)

      # Recalculate the remaining time after the wait
      if timeout
        elapsed = Time.now - start_time
        remaining = (timeout / 1000.0) - elapsed
      end
    end

    @config.logger.debug "Waiting for initialization has completed" if @initialized
  end
end