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

#initializeToggleFetcher

Returns a new instance of ToggleFetcher.



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

def initialize
  self.etag = nil
  self.toggle_cache = nil
  self.segment_cache = 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
    # fail 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!
  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.



8
9
10
# File 'lib/unleash/toggle_fetcher.rb', line 8

def etag
  @etag
end

#retry_countObject

Returns the value of attribute retry_count.



8
9
10
# File 'lib/unleash/toggle_fetcher.rb', line 8

def retry_count
  @retry_count
end

#segment_cacheObject

Returns the value of attribute segment_cache.



8
9
10
# File 'lib/unleash/toggle_fetcher.rb', line 8

def segment_cache
  @segment_cache
end

#toggle_cacheObject

Returns the value of attribute toggle_cache.



8
9
10
# File 'lib/unleash/toggle_fetcher.rb', line 8

def toggle_cache
  @toggle_cache
end

#toggle_lockObject

Returns the value of attribute toggle_lock.



8
9
10
# File 'lib/unleash/toggle_fetcher.rb', line 8

def toggle_lock
  @toggle_lock
end

#toggle_resourceObject

Returns the value of attribute toggle_resource.



8
9
10
# File 'lib/unleash/toggle_fetcher.rb', line 8

def toggle_resource
  @toggle_resource
end

Instance Method Details

#fetchObject

rename to refresh_from_server! ??



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/unleash/toggle_fetcher.rb', line 44

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

  response = Unleash::Util::Http.get(Unleash.configuration.fetch_toggles_uri, etag)

  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 a non 200/304 HTTP result."
  end

  self.etag = response['ETag']
  features = get_features(response.body)

  # always synchronize with the local cache when fetching:
  synchronize_with_local_cache!(features)

  update_running_client!
  save!
end

#save!Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/unleash/toggle_fetcher.rb', line 67

def save!
  Unleash.logger.debug "Will save toggles to disk now"

  backup_file = Unleash.configuration.backup_file
  backup_file_tmp = "#{backup_file}.tmp"

  self.toggle_lock.synchronize do
    File.open(backup_file_tmp, "w") do |file|
      file.write(self.toggle_cache.to_json)
    end
    File.rename(backup_file_tmp, backup_file)
  end
rescue StandardError => e
  # This is not really the end of the world. Swallowing the exception.
  Unleash.logger.error "Unable to save backup file. Exception thrown #{e.class}:'#{e}'"
  Unleash.logger.error "stacktrace: #{e.backtrace}"
end

#togglesObject



35
36
37
38
39
40
41
# File 'lib/unleash/toggle_fetcher.rb', line 35

def toggles
  self.toggle_lock.synchronize do
    # wait for resource, only if it is null
    self.toggle_resource.wait(self.toggle_lock) if self.toggle_cache.nil?
    return self.toggle_cache
  end
end