Class: DevCycle::ConfigManager
- Inherits:
-
Object
- Object
- DevCycle::ConfigManager
- Extended by:
- T::Sig
- Defined in:
- lib/devcycle-ruby-server-sdk/localbucketing/config_manager.rb
Instance Method Summary collapse
- #close ⇒ Object
- #fetch_config ⇒ Object
- #get_config_url ⇒ Object
-
#initialize(sdkKey, local_bucketing, wait_for_init) ⇒ ConfigManager
constructor
A new instance of ConfigManager.
- #initialize_config ⇒ Object
- #set_config(config, etag, lastmodified) ⇒ Object
- #stop_polling ⇒ Object
Constructor Details
#initialize(sdkKey, local_bucketing, wait_for_init) ⇒ ConfigManager
Returns a new instance of ConfigManager.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/config_manager.rb', line 17 def initialize(sdkKey, local_bucketing, wait_for_init) @first_load = true @config_version = "v1" @local_bucketing = local_bucketing @sdkKey = sdkKey @config_e_tag = "" @config_last_modified = "" @logger = local_bucketing..logger @polling_enabled = true @max_config_retries = 2 @config_poller = Concurrent::TimerTask.new({ execution_interval: @local_bucketing..config_polling_interval_ms.fdiv(1000) }) do |task| fetch_config end t = Thread.new { initialize_config } t.join if wait_for_init end |
Instance Method Details
#close ⇒ Object
138 139 140 141 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/config_manager.rb', line 138 def close @config_poller.shutdown if @config_poller.running? nil end |
#fetch_config ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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 109 110 111 112 113 114 115 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/config_manager.rb', line 49 def fetch_config return unless @polling_enabled req = Typhoeus::Request.new( get_config_url, headers: { Accept: "application/json", }) begin Date.parse(@config_last_modified) if @config_last_modified != "" req.[:headers]["If-Modified-Since"] = Time.httpdate(@config_last_modified) end rescue end if @config_e_tag != "" req.[:headers]['If-None-Match'] = @config_e_tag end @max_config_retries.times do @logger.debug("Requesting new config from #{get_config_url}, current etag: #{@config_e_tag}, last modified: #{@config_last_modified}") resp = req.run @logger.debug("Config request complete, status: #{resp.code}") case resp.code when 304 @logger.debug("Config not modified, using cache, etag: #{@config_e_tag}, last modified: #{@config_last_modified}") break when 200 @logger.debug("New config received, etag: #{resp.headers['Etag']} LM:#{resp.headers['Last-Modified']}") lm_header = resp.headers['Last-Modified'] begin = Time.rfc2822(lm_header) current_lm = Time.rfc2822(@config_last_modified) if == "" && @config_last_modified == "" || (current_lm.utc < .utc) set_config(resp.body, resp.headers['Etag'], lm_header) @logger.debug("New config stored, etag: #{@config_e_tag}, last modified: #{lm_header}") else @logger.warn("Config response was an older config than currently stored config.") end rescue @logger.warn("Failed to parse last modified header, setting config.") set_config(resp.body, resp.headers['Etag'], lm_header) end break when 403 stop_polling @logger.error("Failed to download DevCycle config; Invalid SDK Key.") break when 404 stop_polling @logger.error("Failed to download DevCycle config; Config not found.") break when 500...599 @logger.error("Failed to download DevCycle config. Status: #{resp.code}") else @logger.error("Unexpected response from DevCycle CDN") @logger.error("Response code: #{resp.code}") @logger.error("Response body: #{resp.body}") break end end nil end |
#get_config_url ⇒ Object
128 129 130 131 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/config_manager.rb', line 128 def get_config_url configBasePath = @local_bucketing..config_cdn_uri "#{configBasePath}/config/#{@config_version}/server/#{@sdkKey}.json" end |
#initialize_config ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/config_manager.rb', line 38 def initialize_config begin fetch_config @config_poller.execute if @polling_enabled rescue => e @logger.error("DevCycle: Error Initializing Config: #{e.message}") ensure @local_bucketing.initialized = true end end |
#set_config(config, etag, lastmodified) ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/config_manager.rb', line 117 def set_config(config, etag, lastmodified) if !JSON.parse(config).is_a?(Hash) raise("Invalid JSON body parsed from Config Response") end @local_bucketing.store_config(config) @config_e_tag = etag @config_last_modified = lastmodified @local_bucketing.has_config = true end |
#stop_polling ⇒ Object
133 134 135 136 |
# File 'lib/devcycle-ruby-server-sdk/localbucketing/config_manager.rb', line 133 def stop_polling @polling_enabled = false @config_poller.shutdown if @config_poller.running? end |