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.



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 11

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
    # 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.



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

def etag
  @etag
end

#retry_countObject

Returns the value of attribute retry_count.



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

def retry_count
  @retry_count
end

#toggle_engineObject

Returns the value of attribute toggle_engine.



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

def toggle_engine
  @toggle_engine
end

#toggle_lockObject

Returns the value of attribute toggle_lock.



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

def toggle_lock
  @toggle_lock
end

#toggle_resourceObject

Returns the value of attribute toggle_resource.



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

def toggle_resource
  @toggle_resource
end

Instance Method Details

#fetchObject

rename to refresh_from_server! ??



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

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']

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

  save! response.body
end

#save!(toggle_data) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/unleash/toggle_fetcher.rb', line 57

def save!(toggle_data)
  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(toggle_data)
    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