lib/unleash/toggle_fetcher.rb in unleash-4.2.1 vs lib/unleash/toggle_fetcher.rb in unleash-4.3.0

- old
+ new

@@ -63,28 +63,24 @@ save! end def save! Unleash.logger.debug "Will save toggles to disk now" - 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") + 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) - file.close - 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}" - ensure - file&.close if defined?(file) - self.toggle_lock.unlock if self.toggle_lock.locked? + 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 private def synchronize_with_local_cache!(features) @@ -108,25 +104,18 @@ def read! Unleash.logger.debug "read!()" backup_file = Unleash.configuration.backup_file return nil unless File.exist?(backup_file) - begin - file = File.new(backup_file, "r") - file_content = file.read - - backup_as_hash = JSON.parse(file_content) - synchronize_with_local_cache!(backup_as_hash) - update_running_client! - rescue IOError => e - Unleash.logger.error "Unable to read the backup_file: #{e}" - rescue JSON::ParserError => e - Unleash.logger.error "Unable to parse JSON from existing backup_file: #{e}" - rescue StandardError => e - Unleash.logger.error "Unable to extract valid data from backup_file. Exception thrown: #{e}" - ensure - file&.close - end + backup_as_hash = JSON.parse(File.read(backup_file)) + synchronize_with_local_cache!(backup_as_hash) + update_running_client! + rescue IOError => e + Unleash.logger.error "Unable to read the backup_file: #{e}" + rescue JSON::ParserError => e + Unleash.logger.error "Unable to parse JSON from existing backup_file: #{e}" + rescue StandardError => e + Unleash.logger.error "Unable to extract valid data from backup_file. Exception thrown: #{e}" end def bootstrap bootstrap_payload = Unleash::Bootstrap::Handler.new(Unleash.configuration.bootstrap_config).retrieve_toggles synchronize_with_local_cache! get_features bootstrap_payload