Class: FC::Storage
Class Attribute Summary collapse
-
.check_time_limit ⇒ Object
Returns the value of attribute check_time_limit.
-
.get_copy_storages_mutex ⇒ Object
Returns the value of attribute get_copy_storages_mutex.
-
.storages_cache_time ⇒ Object
Returns the value of attribute storages_cache_time.
Attributes inherited from DbBase
#additional_fields, #database_fields, #id
Class Method Summary collapse
Instance Method Summary collapse
- #check_time_delay ⇒ Object
-
#copy_path(local_path, file_name, try_move = false, speed_limit = nil) ⇒ Object
copy local_path to storage.
-
#copy_to_local(file_name, local_path, speed_limit = nil) ⇒ Object
copy object to local_path.
-
#delete_file(file_name) ⇒ Object
delete object from storage.
-
#file_size(file_name, ignore_errors = false) ⇒ Object
return object size on storage.
- #free ⇒ Object
- #free_rate ⇒ Object
- #get_copy_storages ⇒ Object
-
#get_proper_storage_for_copy(size, exclude = []) ⇒ Object
get available storage for copy by size.
-
#initialize(params = {}) ⇒ Storage
constructor
A new instance of Storage.
-
#md5_sum(file_name) ⇒ Object
return object md5_sum on storage.
- #size_rate ⇒ Object
- #up? ⇒ Boolean
- #update_check_time ⇒ Object
Methods inherited from DbBase
create_from_fiels, #delete, find, #reload, #save, set_table, where
Constructor Details
#initialize(params = {}) ⇒ Storage
Returns a new instance of Storage.
33 34 35 36 37 38 39 40 41 |
# File 'lib/fc/storage.rb', line 33 def initialize(params = {}) path = (params['path'] || params[:path]) if path && !path.to_s.empty? path += '/' unless path[-1] == '/' raise "Storage path must be like '/bla/bla../'" unless path.match(/^\/.*\/$/) params['path'] = params[:path] = path end super params end |
Class Attribute Details
.check_time_limit ⇒ Object
Returns the value of attribute check_time_limit.
10 11 12 |
# File 'lib/fc/storage.rb', line 10 def check_time_limit @check_time_limit end |
.get_copy_storages_mutex ⇒ Object
Returns the value of attribute get_copy_storages_mutex.
10 11 12 |
# File 'lib/fc/storage.rb', line 10 def get_copy_storages_mutex @get_copy_storages_mutex end |
.storages_cache_time ⇒ Object
Returns the value of attribute storages_cache_time.
10 11 12 |
# File 'lib/fc/storage.rb', line 10 def storages_cache_time @storages_cache_time end |
Class Method Details
.curr_host ⇒ Object
16 17 18 |
# File 'lib/fc/storage.rb', line 16 def self.curr_host @uname || @uname = `uname -n`.chomp end |
.select_proper_storage_for_create(storages, size, exclude = []) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/fc/storage.rb', line 20 def self.select_proper_storage_for_create(storages, size, exclude = []) list = storages.select do |storage| !exclude.include?(storage.name) && storage.up? && storage.size + size < storage.size_limit && storage.write_weight.to_i >= 0 end list = yield(list) if block_given? # sort by random(free_rate * write_weight) list.map{ |storage| [storage, Kernel.rand(storage.free_rate * (storage.write_weight.to_i == 0 ? 0.01 : storage.write_weight.to_i) * 1000000000)] }.sort{ |a, b| a[1] <=> b[1] }.map{|el| el[0]}.last end |
Instance Method Details
#check_time_delay ⇒ Object
72 73 74 |
# File 'lib/fc/storage.rb', line 72 def check_time_delay Time.new.to_i - check_time.to_i end |
#copy_path(local_path, file_name, try_move = false, speed_limit = nil) ⇒ Object
copy local_path to storage
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/fc/storage.rb', line 81 def copy_path(local_path, file_name, try_move = false, speed_limit = nil) dst_path = "#{self.path}#{file_name}" cmd = "rm -rf #{dst_path.shellescape}; mkdir -p #{File.dirname(dst_path).shellescape}" cmd = self.class.curr_host == host ? cmd : "ssh -q -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"#{cmd}\"" r = `#{cmd} 2>&1` raise r if $?.exitstatus != 0 op = try_move && self.class.curr_host == host && File.stat(local_path).dev == File.stat(File.dirname(dst_path)).dev ? 'mv' : 'cp -r' speed_limit = (speed_limit * 1000).to_i if speed_limit.to_f > 0 cmd = self.class.curr_host == host ? "#{op} #{local_path.shellescape} #{dst_path.shellescape}" : "scp -r -q -oBatchMode=yes -oStrictHostKeyChecking=no #{speed_limit.to_i > 0 ? '-l '+speed_limit.to_s : ''} #{local_path.shellescape} #{self.host}:\"#{dst_path.shellescape}\"" r = `#{cmd} 2>&1` raise r if $?.exitstatus != 0 end |
#copy_to_local(file_name, local_path, speed_limit = nil) ⇒ Object
copy object to local_path
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/fc/storage.rb', line 99 def copy_to_local(file_name, local_path, speed_limit = nil) src_path = "#{self.path}#{file_name}" r = `rm -rf #{local_path.shellescape}; mkdir -p #{File.dirname(local_path).shellescape} 2>&1` raise r if $?.exitstatus != 0 speed_limit = (speed_limit * 1000).to_i if speed_limit.to_f > 0 cmd = self.class.curr_host == host ? "cp -r #{src_path.shellescape} #{local_path.shellescape}" : "scp -r -q -oBatchMode=yes -oStrictHostKeyChecking=no #{speed_limit.to_i > 0 ? '-l '+speed_limit.to_s : ''} #{self.host}:\"#{src_path.shellescape}\" #{local_path.shellescape}" r = `#{cmd} 2>&1` raise r if $?.exitstatus != 0 end |
#delete_file(file_name) ⇒ Object
delete object from storage
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/fc/storage.rb', line 114 def delete_file(file_name) dst_path = "#{self.path}#{file_name}" if self.class.curr_host == host begin File.delete(dst_path) rescue Errno::EISDIR FileUtils.rm_r(dst_path) rescue Errno::ENOENT end else cmd = "ssh -q -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"rm -rf #{dst_path.shellescape}\"" r = `#{cmd} 2>&1` raise r if $?.exitstatus != 0 cmd = "ssh -q -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"ls -la #{dst_path.shellescape}\"" r = `#{cmd} 2>/dev/null` raise "Path #{dst_path} not deleted" unless r.empty? end end |
#file_size(file_name, ignore_errors = false) ⇒ Object
return object size on storage
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/fc/storage.rb', line 135 def file_size(file_name, ignore_errors = false) dst_path = "#{self.path}#{file_name}" cmd = self.class.curr_host == host ? "du -sb #{dst_path.shellescape}" : "ssh -q -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"du -sb #{dst_path.shellescape}\"" r = ignore_errors ? `#{cmd} 2>/dev/null` : `#{cmd} 2>&1` raise r if $?.exitstatus != 0 r.to_i end |
#free ⇒ Object
43 44 45 |
# File 'lib/fc/storage.rb', line 43 def free size_limit - size end |
#free_rate ⇒ Object
51 52 53 54 |
# File 'lib/fc/storage.rb', line 51 def free_rate rate = free.to_f / size_limit rate < 0 ? 0.0 : rate end |
#get_copy_storages ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/fc/storage.rb', line 56 def get_copy_storages self.class.get_copy_storages_mutex.synchronize do unless @copy_storages_cache && Time.new.to_i - @get_copy_storages_time.to_i < self.class.storages_cache_time @get_copy_storages_time = Time.new.to_i names = copy_storages.to_s.split(',').map{|s| "'#{s}'"}.join(',') @copy_storages_cache = names.empty? ? [] : FC::Storage.where("name IN (#{names}) ORDER BY FIELD(name, #{names})") end end @copy_storages_cache end |
#get_proper_storage_for_copy(size, exclude = []) ⇒ Object
get available storage for copy by size
158 159 160 |
# File 'lib/fc/storage.rb', line 158 def get_proper_storage_for_copy(size, exclude = []) FC::Storage.select_proper_storage_for_create(get_copy_storages, size, exclude) end |
#md5_sum(file_name) ⇒ Object
return object md5_sum on storage
147 148 149 150 151 152 153 154 155 |
# File 'lib/fc/storage.rb', line 147 def md5_sum(file_name) dst_path = "#{self.path}#{file_name}" cmd = self.class.curr_host == host ? "find #{dst_path.shellescape} -type f -exec md5sum {} \\; | awk '{print $1}' | sort | md5sum" : "ssh -q -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"find #{dst_path.shellescape} -type f -exec md5sum {} \\; | awk '{print \\$1}' | sort | md5sum\"" r = `#{cmd} 2>&1` raise r if $?.exitstatus != 0 r.to_s[0..31] end |
#size_rate ⇒ Object
47 48 49 |
# File 'lib/fc/storage.rb', line 47 def size_rate size.to_f / size_limit end |
#up? ⇒ Boolean
76 77 78 |
# File 'lib/fc/storage.rb', line 76 def up? check_time_delay < self.class.check_time_limit end |
#update_check_time ⇒ Object
67 68 69 70 |
# File 'lib/fc/storage.rb', line 67 def update_check_time self.check_time = Time.new.to_i save end |