Class: DevCycle::ConfigManager

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/devcycle-ruby-server-sdk/localbucketing/config_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(sdkKey, local_bucketing, wait_for_init) ⇒ ConfigManager

Returns a new instance of ConfigManager.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/devcycle-ruby-server-sdk/localbucketing/config_manager.rb', line 16

def initialize(sdkKey, local_bucketing, wait_for_init)
  @first_load = true
  @config_version = "v1"
  @local_bucketing = local_bucketing
  @sdkKey = sdkKey
  @config_e_tag = ""
  @logger = local_bucketing.options.logger

  @config_poller = Concurrent::TimerTask.new(
    {
      execution_interval: @local_bucketing.options.config_polling_interval_ms.fdiv(1000),
      run_now: true
    }) do |task|
    fetch_config(false, task)
  end

  t = Thread.new { fetch_config(false, nil) }
  t.join if wait_for_init
end

Instance Method Details

#closeObject



92
93
94
95
# File 'lib/devcycle-ruby-server-sdk/localbucketing/config_manager.rb', line 92

def close
  @config_poller.shutdown
  nil
end

#fetch_config(retrying, task) ⇒ Object



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
68
69
# File 'lib/devcycle-ruby-server-sdk/localbucketing/config_manager.rb', line 36

def fetch_config(retrying, task)
  req = Typhoeus::Request.new(
    get_config_url,
    headers: {
      Accept: "application/json",
    })

  if @config_e_tag != ""
    req.options[:headers]['If-None-Match'] = @config_e_tag
  end

  resp = req.run

  case resp.code
  when 304
    return nil
  when 200
    return set_config(resp.body, resp.headers['Etag'])
  when 403
    raise("Failed to download DevCycle config; Invalid SDK Key.")
  when 500...599
    if !retrying
      return fetch_config(true, task)
    end
    @logger.warn("Failed to download DevCycle config. Status: #{resp.code}")
  else
    if task != nil
      task.shutdown
    end
    raise("Unexpected response code - DevCycle Response: #{Oj.dump(resp)}")
  end

  nil
end

#get_config_urlObject



87
88
89
90
# File 'lib/devcycle-ruby-server-sdk/localbucketing/config_manager.rb', line 87

def get_config_url
  configBasePath = @local_bucketing.options.config_cdn_uri
  "#{configBasePath}/config/#{@config_version}/server/#{@sdkKey}.json"
end

#set_config(config, etag) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/devcycle-ruby-server-sdk/localbucketing/config_manager.rb', line 71

def set_config(config, etag)
  if !JSON.parse(config).is_a?(Hash)
    raise("Invalid JSON body parsed from Config Response")
  end

  @local_bucketing.store_config(@sdkKey, config)
  @config_e_tag = etag

  if @first_load
    @logger.info("Config Set. Client Initialized.")
    @first_load = false
    @local_bucketing.initialized = true
    @config_poller.execute
  end
end