From c1e417122255f44a53364094b1bd83b147693868 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 2 Feb 2016 13:50:49 +0100 Subject: [PATCH] Improved logging. --- app/models/store/file.rb | 32 +++++++++++++++---------------- app/models/store/provider/db.rb | 4 ++-- app/models/store/provider/file.rb | 20 +++++++++---------- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/app/models/store/file.rb b/app/models/store/file.rb index f294ea02a..041e1c54d 100644 --- a/app/models/store/file.rb +++ b/app/models/store/file.rb @@ -14,9 +14,9 @@ add new file to store =end def self.add(data) - sha = Digest::SHA256.hexdigest( data ) + sha = Digest::SHA256.hexdigest(data) - file = Store::File.find_by( sha: sha ) + file = Store::File.find_by(sha: sha) if file.nil? # load backend based on config @@ -24,8 +24,8 @@ add new file to store if !adapter_name fail 'Missing storage_provider setting option' end - adapter = load_adapter( "Store::Provider::#{adapter_name}" ) - adapter.add( data, sha ) + adapter = load_adapter("Store::Provider::#{adapter_name}") + adapter.add(data, sha) file = Store::File.create( provider: adapter_name, sha: sha, @@ -47,10 +47,10 @@ read content of a file def content adapter = self.class.load_adapter("Store::Provider::#{provider}") c = if sha - adapter.get( sha ) + adapter.get(sha) else # fallback until migration is done - Store::Provider::DB.find_by( md5: md5 ).data + Store::Provider::DB.find_by(md5: md5).data end c end @@ -73,15 +73,15 @@ in case of fixing sha hash use: success = true Store::File.all.each {|item| content = item.content - sha = Digest::SHA256.hexdigest( content ) - logger.info "CHECK: Store::File.find(#{item.id}) " - + sha = Digest::SHA256.hexdigest(content) + logger.info "CHECK: Store::File.find(#{item.id})" next if sha == item.sha - success = false - logger.error "DIFF: sha diff of Store::File.find(#{item.id}) " + logger.error "DIFF: sha diff of Store::File.find(#{item.id}) current:#{sha}/db:#{item.sha}/provider:#{item.provider}" + store = Store.find(store_file_id: item.id) + logger.error "STORE: #{store.inspect}" if fix_it - item.update_attribute( :sha, sha ) + item.update_attribute(:sha, sha) end } success @@ -110,13 +110,13 @@ move files from db backend to fs content = item.content # add to new provider - adapter_target.add( content, item.sha ) + adapter_target.add(content, item.sha) # update meta data - item.update_attribute( :provider, target ) + item.update_attribute(:provider, target) # remove from old provider - adapter_source.delete( item.sha ) + adapter_source.delete(item.sha) logger.info "Moved file #{item.sha} from #{source} to #{target}" } @@ -127,7 +127,7 @@ move files from db backend to fs def destroy_provider adapter = self.class.load_adapter("Store::Provider::#{provider}") - adapter.delete( sha ) + adapter.delete(sha) end end end diff --git a/app/models/store/provider/db.rb b/app/models/store/provider/db.rb index a5b132761..55e184a90 100644 --- a/app/models/store/provider/db.rb +++ b/app/models/store/provider/db.rb @@ -14,13 +14,13 @@ class Store end def self.get(sha) - file = Store::Provider::DB.find_by( sha: sha ) + file = Store::Provider::DB.find_by(sha: sha) return if !file file.data end def self.delete(sha) - Store::Provider::DB.where( sha: sha ).destroy_all + Store::Provider::DB.where(sha: sha).destroy_all true end end diff --git a/app/models/store/provider/file.rb b/app/models/store/provider/file.rb index c83080a67..9ff8f4354 100644 --- a/app/models/store/provider/file.rb +++ b/app/models/store/provider/file.rb @@ -6,16 +6,16 @@ class Store::Provider::File # install file permission = '600' - if !File.exist?( get_locaton(sha) ) + if !File.exist?(get_locaton(sha)) Rails.logger.debug "storge write '#{get_locaton(sha)}' (#{permission})" - file = File.new( get_locaton(sha), 'wb' ) - file.write( data ) + file = File.new(get_locaton(sha), 'wb') + file.write(data) file.close end - File.chmod( permission.to_i(8), get_locaton(sha) ) + File.chmod(permission.to_i(8), get_locaton(sha)) # check sha - local_sha = Digest::SHA256.hexdigest( get(sha) ) + local_sha = Digest::SHA256.hexdigest(get(sha)) if sha != local_sha fail "ERROR: Corrupt file in fs #{get_locaton(sha)}, sha should be #{sha} but is #{local_sha}" end @@ -26,14 +26,14 @@ class Store::Provider::File # read file from fs def self.get(sha) Rails.logger.debug "read from fs #{get_locaton(sha)}" - if !File.exist?( get_locaton(sha) ) + if !File.exist?(get_locaton(sha)) fail "ERROR: No such file #{get_locaton(sha)}" end - data = File.open( get_locaton(sha), 'rb' ) + data = File.open(get_locaton(sha), 'rb') content = data.read # check sha - local_sha = Digest::SHA256.hexdigest( content ) + local_sha = Digest::SHA256.hexdigest(content) if local_sha != sha fail "ERROR: Corrupt file in fs #{get_locaton(sha)}, sha should be #{sha} but is #{local_sha}" end @@ -59,8 +59,8 @@ class Store::Provider::File location = "#{base}/#{path}" # create directory if not exists - if !File.exist?( location ) - FileUtils.mkdir_p( location ) + if !File.exist?(location) + FileUtils.mkdir_p(location) end location += file end