Class: Unleash::ToggleFetcher
- Inherits:
-
Object
- Object
- Unleash::ToggleFetcher
- Defined in:
- lib/unleash/toggle_fetcher.rb
Instance Attribute Summary collapse
-
#etag ⇒ Object
Returns the value of attribute etag.
-
#retry_count ⇒ Object
Returns the value of attribute retry_count.
-
#toggle_cache ⇒ Object
Returns the value of attribute toggle_cache.
-
#toggle_lock ⇒ Object
Returns the value of attribute toggle_lock.
-
#toggle_resource ⇒ Object
Returns the value of attribute toggle_resource.
Instance Method Summary collapse
-
#fetch ⇒ Object
rename to refresh_from_server! ?? TODO: should simplify by moving uri / http initialization to class initialization.
-
#initialize ⇒ ToggleFetcher
constructor
A new instance of ToggleFetcher.
- #toggles ⇒ Object
Constructor Details
#initialize ⇒ ToggleFetcher
Returns a new instance of ToggleFetcher.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/unleash/toggle_fetcher.rb', line 12 def initialize self.etag = nil self.toggle_cache = nil self.toggle_lock = Mutex.new self.toggle_resource = ConditionVariable.new self.retry_count = 0 # start by fetching synchronously, and failing back to reading the backup file. begin fetch rescue Exception => e Unleash.logger.warn "ToggleFetcher was unable to fetch from the network, attempting to read from backup file." read! end # once we have initialized, start the fetcher loop scheduledExecutor = Unleash::ScheduledExecutor.new('ToggleFetcher', Unleash.configuration.refresh_interval) scheduledExecutor.run { remote_toggles = fetch() } end |
Instance Attribute Details
#etag ⇒ Object
Returns the value of attribute etag.
10 11 12 |
# File 'lib/unleash/toggle_fetcher.rb', line 10 def etag @etag end |
#retry_count ⇒ Object
Returns the value of attribute retry_count.
10 11 12 |
# File 'lib/unleash/toggle_fetcher.rb', line 10 def retry_count @retry_count end |
#toggle_cache ⇒ Object
Returns the value of attribute toggle_cache.
10 11 12 |
# File 'lib/unleash/toggle_fetcher.rb', line 10 def toggle_cache @toggle_cache end |
#toggle_lock ⇒ Object
Returns the value of attribute toggle_lock.
10 11 12 |
# File 'lib/unleash/toggle_fetcher.rb', line 10 def toggle_lock @toggle_lock end |
#toggle_resource ⇒ Object
Returns the value of attribute toggle_resource.
10 11 12 |
# File 'lib/unleash/toggle_fetcher.rb', line 10 def toggle_resource @toggle_resource end |
Instance Method Details
#fetch ⇒ Object
rename to refresh_from_server! ?? TODO: should simplify by moving uri / http initialization to class initialization
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/unleash/toggle_fetcher.rb', line 42 def fetch Unleash.logger.debug "fetch()" Unleash.logger.debug "ETag: #{self.etag}" unless self.etag.nil? uri = URI(Unleash.configuration.fetch_toggles_url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.scheme == 'https' http.open_timeout = Unleash.configuration.timeout # in seconds http.read_timeout = Unleash.configuration.timeout # in seconds headers = (Unleash.configuration.get_http_headers || {}).dup headers['Content-Type'] = 'application/json' headers['If-None-Match'] = self.etag unless self.etag.nil? request = Net::HTTP::Get.new(uri.request_uri, headers) response = http.request(request) Unleash.logger.debug "No changes according to the unleash server, nothing to do." if response.code == '304' return if response.code == '304' raise IOError, "Unleash server returned a non 200/304 HTTP result." if response.code != '200' self.etag = response['ETag'] response_hash = JSON.parse(response.body) if response_hash['version'] == 1 features = response_hash['features'] else raise NotImplemented, "Version of features provided by unleash server" \ " is unsupported by this client." end # always synchronize with the local cache when fetching: synchronize_with_local_cache!(features) Unleash.logger.info "Flush changes to running client variable" update_client! Unleash.logger.info "Saved to toggle cache, will save to disk now" save! end |
#toggles ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/unleash/toggle_fetcher.rb', line 32 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 |