Class: Unleash::ToggleFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/unleash/toggle_fetcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine) ⇒ ToggleFetcher

Returns a new instance of ToggleFetcher.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/unleash/toggle_fetcher.rb', line 13

def initialize(engine)
  self.toggle_engine = engine
  self.etag = nil
  self.toggle_lock = Mutex.new
  self.toggle_resource = ConditionVariable.new
  self.retry_count = 0

  begin
    # if bootstrap configuration is available, initialize. An immediate API read is also triggered
    if Unleash.configuration.use_bootstrap?
      bootstrap
    else
      fetch
    end
  rescue StandardError => e
    # fall back to reading the backup file
    Unleash.logger.warn "ToggleFetcher was unable to fetch from the network, attempting to read from backup file."
    Unleash.logger.debug "Exception Caught: #{e}"
    read_backup_file!
  end

  # once initialized, somewhere else you will want to start a loop with fetch()
end

Instance Attribute Details

#etagObject

Returns the value of attribute etag.



11
12
13
# File 'lib/unleash/toggle_fetcher.rb', line 11

def etag
  @etag
end

#retry_countObject

Returns the value of attribute retry_count.



11
12
13
# File 'lib/unleash/toggle_fetcher.rb', line 11

def retry_count
  @retry_count
end

#toggle_engineObject

Returns the value of attribute toggle_engine.



11
12
13
# File 'lib/unleash/toggle_fetcher.rb', line 11

def toggle_engine
  @toggle_engine
end

#toggle_lockObject

Returns the value of attribute toggle_lock.



11
12
13
# File 'lib/unleash/toggle_fetcher.rb', line 11

def toggle_lock
  @toggle_lock
end

#toggle_resourceObject

Returns the value of attribute toggle_resource.



11
12
13
# File 'lib/unleash/toggle_fetcher.rb', line 11

def toggle_resource
  @toggle_resource
end

Instance Method Details

#fetchObject

rename to refresh_from_server! ??



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/unleash/toggle_fetcher.rb', line 38

def fetch
  Unleash.logger.debug "fetch()"
  return if Unleash.configuration.disable_client

  headers = (Unleash.configuration.http_headers || {}).dup
  headers.merge!({ 'UNLEASH-INTERVAL' => Unleash.configuration.refresh_interval.to_s })
  response = Unleash::Util::Http.get(Unleash.configuration.fetch_toggles_uri, etag, headers)

  if response.code == '304'
    Unleash.logger.debug "No changes according to the unleash server, nothing to do."
    return
  elsif response.code != '200'
    raise IOError, "Unleash server returned unexpected HTTP response code #{response.code}."\
      " Only handle response codes 200 (indicates changes) or 304 (no changes)."
  end

  self.etag = response['ETag']

  # always synchronize with the local cache when fetching:
  update_engine_state!(response.body)

  Unleash::BackupFileWriter.save! response.body
end