lib/unleash/toggle_fetcher.rb in unleash-0.1.5 vs lib/unleash/toggle_fetcher.rb in unleash-0.1.6

- old
+ new

@@ -1,7 +1,6 @@ require 'unleash/configuration' -require 'unleash/scheduled_executor' require 'net/http' require 'json' require 'thread' module Unleash @@ -19,16 +18,15 @@ # 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." + Unleash.logger.debug "Exception Caught: #{e}" read! end - # once we have initialized, start the fetcher loop - scheduledExecutor = Unleash::ScheduledExecutor.new('ToggleFetcher', Unleash.configuration.refresh_interval) - scheduledExecutor.run { remote_toggles = fetch() } + # once initialized, somewhere else you will want to start a loop with fetch() end def toggles self.toggle_lock.synchronize do # wait for resource, only if it is null @@ -36,11 +34,11 @@ return self.toggle_cache end end # rename to refresh_from_server! ?? - # TODO: should simplify by moving uri / http initialization to class initialization + # TODO: should simplify by moving uri / http initialization elsewhere def fetch Unleash.logger.debug "fetch()" Unleash.logger.debug "ETag: #{self.etag}" unless self.etag.nil? uri = URI(Unleash.configuration.fetch_toggles_url) @@ -80,10 +78,30 @@ Unleash.logger.info "Saved to toggle cache, will save to disk now" save! end + def save! + begin + backup_file = Unleash.configuration.backup_file + backup_file_tmp = "#{backup_file}.tmp" + + self.toggle_lock.synchronize do + file = File.open(backup_file_tmp, "w") + file.write(self.toggle_cache.to_json) + File.rename(backup_file_tmp, backup_file) + end + rescue Exception => 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}" + ensure + file.close if defined?(file) && ! file.nil? + self.toggle_lock.unlock if self.toggle_lock.locked? + end + end + private def synchronize_with_local_cache!(features) if self.toggle_cache != features self.toggle_lock.synchronize do @@ -100,51 +118,26 @@ Unleash.logger.info "Updating toggles to main client, there has been a change in the server." Unleash.toggles = self.toggles end end - def backup_file_exists? - File.exists?(backup_file) - end - - def save! - begin - file = File.open(Unleash.configuration.backup_file, "w") - - self.toggle_lock.synchronize do - file.write(self.toggle_cache.to_json) - end - rescue Exception => 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}" - ensure - file.close unless file.nil? - end - end - def read! Unleash.logger.debug "read!()" - return nil unless File.exists?(Unleash.configuration.backup_file) + return nil unless File.exist?(Unleash.configuration.backup_file) begin - file = File.open(Unleash.configuration.backup_file, "r") - line_cache = "" - file.each_line do |line| - line_cache += line - end + file = File.new(Unleash.configuration.backup_file, "r") + file_content = file.read - backup_as_hash = JSON.parse(line_cache) + backup_as_hash = JSON.parse(file_content) synchronize_with_local_cache!(backup_as_hash) update_client! - rescue IOError => e Unleash.logger.error "Unable to read the backup_file." rescue JSON::ParserError => e Unleash.logger.error "Unable to parse JSON from existing backup_file." rescue Exception => e - Unleash.logger.error "Unable to extract valid data from backup_file. Exception thrown #{e.class}:'#{e}'" - Unleash.logger.error "stacktrace: #{e.backtrace}" + Unleash.logger.error "Unable to extract valid data from backup_file. Exception thrown", e ensure file.close unless file.nil? end end end