Improved logging.

This commit is contained in:
Martin Edenhofer 2016-02-02 13:50:49 +01:00
parent fb17c49c8c
commit c1e4171222
3 changed files with 28 additions and 28 deletions

View file

@ -14,9 +14,9 @@ add new file to store
=end =end
def self.add(data) 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? if file.nil?
# load backend based on config # load backend based on config
@ -24,8 +24,8 @@ add new file to store
if !adapter_name if !adapter_name
fail 'Missing storage_provider setting option' fail 'Missing storage_provider setting option'
end end
adapter = load_adapter( "Store::Provider::#{adapter_name}" ) adapter = load_adapter("Store::Provider::#{adapter_name}")
adapter.add( data, sha ) adapter.add(data, sha)
file = Store::File.create( file = Store::File.create(
provider: adapter_name, provider: adapter_name,
sha: sha, sha: sha,
@ -47,10 +47,10 @@ read content of a file
def content def content
adapter = self.class.load_adapter("Store::Provider::#{provider}") adapter = self.class.load_adapter("Store::Provider::#{provider}")
c = if sha c = if sha
adapter.get( sha ) adapter.get(sha)
else else
# fallback until migration is done # fallback until migration is done
Store::Provider::DB.find_by( md5: md5 ).data Store::Provider::DB.find_by(md5: md5).data
end end
c c
end end
@ -73,15 +73,15 @@ in case of fixing sha hash use:
success = true success = true
Store::File.all.each {|item| Store::File.all.each {|item|
content = item.content content = item.content
sha = Digest::SHA256.hexdigest( content ) sha = Digest::SHA256.hexdigest(content)
logger.info "CHECK: Store::File.find(#{item.id}) " logger.info "CHECK: Store::File.find(#{item.id})"
next if sha == item.sha next if sha == item.sha
success = false 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 if fix_it
item.update_attribute( :sha, sha ) item.update_attribute(:sha, sha)
end end
} }
success success
@ -110,13 +110,13 @@ move files from db backend to fs
content = item.content content = item.content
# add to new provider # add to new provider
adapter_target.add( content, item.sha ) adapter_target.add(content, item.sha)
# update meta data # update meta data
item.update_attribute( :provider, target ) item.update_attribute(:provider, target)
# remove from old provider # remove from old provider
adapter_source.delete( item.sha ) adapter_source.delete(item.sha)
logger.info "Moved file #{item.sha} from #{source} to #{target}" logger.info "Moved file #{item.sha} from #{source} to #{target}"
} }
@ -127,7 +127,7 @@ move files from db backend to fs
def destroy_provider def destroy_provider
adapter = self.class.load_adapter("Store::Provider::#{provider}") adapter = self.class.load_adapter("Store::Provider::#{provider}")
adapter.delete( sha ) adapter.delete(sha)
end end
end end
end end

View file

@ -14,13 +14,13 @@ class Store
end end
def self.get(sha) def self.get(sha)
file = Store::Provider::DB.find_by( sha: sha ) file = Store::Provider::DB.find_by(sha: sha)
return if !file return if !file
file.data file.data
end end
def self.delete(sha) def self.delete(sha)
Store::Provider::DB.where( sha: sha ).destroy_all Store::Provider::DB.where(sha: sha).destroy_all
true true
end end
end end

View file

@ -6,16 +6,16 @@ class Store::Provider::File
# install file # install file
permission = '600' permission = '600'
if !File.exist?( get_locaton(sha) ) if !File.exist?(get_locaton(sha))
Rails.logger.debug "storge write '#{get_locaton(sha)}' (#{permission})" Rails.logger.debug "storge write '#{get_locaton(sha)}' (#{permission})"
file = File.new( get_locaton(sha), 'wb' ) file = File.new(get_locaton(sha), 'wb')
file.write( data ) file.write(data)
file.close file.close
end end
File.chmod( permission.to_i(8), get_locaton(sha) ) File.chmod(permission.to_i(8), get_locaton(sha))
# check sha # check sha
local_sha = Digest::SHA256.hexdigest( get(sha) ) local_sha = Digest::SHA256.hexdigest(get(sha))
if sha != local_sha if sha != local_sha
fail "ERROR: Corrupt file in fs #{get_locaton(sha)}, sha should be #{sha} but is #{local_sha}" fail "ERROR: Corrupt file in fs #{get_locaton(sha)}, sha should be #{sha} but is #{local_sha}"
end end
@ -26,14 +26,14 @@ class Store::Provider::File
# read file from fs # read file from fs
def self.get(sha) def self.get(sha)
Rails.logger.debug "read from fs #{get_locaton(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)}" fail "ERROR: No such file #{get_locaton(sha)}"
end end
data = File.open( get_locaton(sha), 'rb' ) data = File.open(get_locaton(sha), 'rb')
content = data.read content = data.read
# check sha # check sha
local_sha = Digest::SHA256.hexdigest( content ) local_sha = Digest::SHA256.hexdigest(content)
if local_sha != sha if local_sha != sha
fail "ERROR: Corrupt file in fs #{get_locaton(sha)}, sha should be #{sha} but is #{local_sha}" fail "ERROR: Corrupt file in fs #{get_locaton(sha)}, sha should be #{sha} but is #{local_sha}"
end end
@ -59,8 +59,8 @@ class Store::Provider::File
location = "#{base}/#{path}" location = "#{base}/#{path}"
# create directory if not exists # create directory if not exists
if !File.exist?( location ) if !File.exist?(location)
FileUtils.mkdir_p( location ) FileUtils.mkdir_p(location)
end end
location += file location += file
end end