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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 128

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

#is_closingObject



144
145
146
147
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 144

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



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

def off

  # TODO: Implement - Reactivity support
end

#on_auth_failedObject



122
123
124
125
126
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 122

def on_auth_failed
  SdkCodes::warn_auth_failed_srv_defaults @config.logger
  @initialized = true
  @condition.signal
end

#on_auth_successObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 101

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



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

def on_metrics_processor_ready

  on_processor_ready(@metrics_processor)
end

#on_poller_error(e) ⇒ Object



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

def on_poller_error(e)

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

#on_poller_iteration(poller) ⇒ Object



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

def on_poller_iteration(poller)

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

#on_poller_ready(poller) ⇒ Object



176
177
178
179
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 176

def on_poller_ready(poller)

  on_processor_ready(poller)
end

#on_processor_ready(processor) ⇒ Object



211
212
213
214
215
216
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
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 211

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.signal
    @initialized = true
  end
end

#on_unauthorizedObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 154

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



201
202
203
204
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 201

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



191
192
193
194
195
196
197
198
199
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 191

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



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/ff/ruby/server/sdk/api/inner_client.rb', line 251

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