Class: RemoteFiles::Configuration
- Inherits:
-
Object
- Object
- RemoteFiles::Configuration
- Defined in:
- lib/remote_files/configuration.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #add_store(store_identifier, options = {}, &block) ⇒ Object
- #clear ⇒ Object
- #configured? ⇒ Boolean
- #delete!(file) ⇒ Object
-
#delete_in_parallel!(file, stores, exceptions) ⇒ Object
This method is used to delete a file from all stores in parallel exceptions are passed back to the caller.
- #delete_now!(file, parallel: false) ⇒ Object
- #file_from_url(url, options = {}) ⇒ Object
- #from_hash(hash) ⇒ Object
-
#initialize(name, config = {}) ⇒ Configuration
constructor
A new instance of Configuration.
- #logger ⇒ Object
- #logger=(logger) ⇒ Object
- #lookup_store(store_identifier) ⇒ Object
- #primary_store ⇒ Object
- #read_only_stores ⇒ Object
- #read_write_stores ⇒ Object
- #store!(file) ⇒ Object
- #store_once!(file) ⇒ Object
- #stores ⇒ Object
- #synchronize!(file) ⇒ Object
Constructor Details
#initialize(name, config = {}) ⇒ Configuration
Returns a new instance of Configuration.
7 8 9 10 11 12 13 |
# File 'lib/remote_files/configuration.rb', line 7 def initialize(name, config = {}) @name = name @stores = [] @stores_map = {} @max_delete_in_parallel = config.delete(:max_delete_in_parallel) || 10 from_hash(config) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/remote_files/configuration.rb', line 5 def name @name end |
Instance Method Details
#add_store(store_identifier, options = {}, &block) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/remote_files/configuration.rb', line 56 def add_store(store_identifier, = {}, &block) store = ([:class] || FogStore).new(store_identifier) block.call(store) if block_given? store[:read_only] = [:read_only] if .key?(:read_only) if [:primary] @stores.unshift(store) else @stores << store end @stores_map[store_identifier] = store end |
#clear ⇒ Object
23 24 25 26 |
# File 'lib/remote_files/configuration.rb', line 23 def clear @stores.clear @stores_map.clear end |
#configured? ⇒ Boolean
71 72 73 |
# File 'lib/remote_files/configuration.rb', line 71 def configured? !@stores.empty? end |
#delete!(file) ⇒ Object
127 128 129 |
# File 'lib/remote_files/configuration.rb', line 127 def delete!(file) RemoteFiles.delete_file(file) end |
#delete_in_parallel!(file, stores, exceptions) ⇒ Object
This method is used to delete a file from all stores in parallel exceptions are passed back to the caller
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/remote_files/configuration.rb', line 156 def delete_in_parallel!(file, stores, exceptions) pool = Concurrent::FixedThreadPool.new(@max_delete_in_parallel) futures = stores.map do |store| Concurrent::Promises.future_on(pool) do begin store.delete!(file.identifier) rescue NotFoundError => e e end end end futures.each do |future| result = future.value exceptions << result if result.is_a?(Exception) end pool.shutdown pool.wait_for_termination end |
#delete_now!(file, parallel: false) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/remote_files/configuration.rb', line 131 def delete_now!(file, parallel: false) exceptions = [] stores = file.read_write_stores raise "No stores configured" if stores.empty? if parallel delete_in_parallel!(file, stores, exceptions) else stores.each do |store| begin store.delete!(file.identifier) rescue NotFoundError => e exceptions << e end end end raise exceptions.first if exceptions.size == stores.size # they all failed true end |
#file_from_url(url, options = {}) ⇒ Object
187 188 189 190 191 192 193 194 |
# File 'lib/remote_files/configuration.rb', line 187 def file_from_url(url, = {}) stores.each do |store| file = store.file_from_url(url, .merge(:configuration => name)) return file if file end nil end |
#from_hash(hash) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/remote_files/configuration.rb', line 28 def from_hash(hash) hash.each do |store_identifier, config| #symbolize_keys! cfg = {} config.each { |name, value| cfg[name.to_sym] = config[name] } config = cfg #camelize type = config[:type].gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } + 'Store' klass = RemoteFiles.const_get(type) rescue nil unless klass require "remote_files/#{config[:type]}_store" klass = RemoteFiles.const_get(type) end config.delete(:type) add_store(store_identifier.to_sym, :class => klass, :primary => !!config.delete(:primary)) do |store| config.each do |name, value| store[name] = value end end end self end |
#logger ⇒ Object
19 20 21 |
# File 'lib/remote_files/configuration.rb', line 19 def logger @logger ||= RemoteFiles.logger end |
#logger=(logger) ⇒ Object
15 16 17 |
# File 'lib/remote_files/configuration.rb', line 15 def logger=(logger) @logger = logger end |
#lookup_store(store_identifier) ⇒ Object
89 90 91 |
# File 'lib/remote_files/configuration.rb', line 89 def lookup_store(store_identifier) @stores_map[store_identifier.to_sym] end |
#primary_store ⇒ Object
93 94 95 |
# File 'lib/remote_files/configuration.rb', line 93 def primary_store stores.first end |
#read_only_stores ⇒ Object
81 82 83 |
# File 'lib/remote_files/configuration.rb', line 81 def read_only_stores stores.select {|s| s.read_only?} end |
#read_write_stores ⇒ Object
85 86 87 |
# File 'lib/remote_files/configuration.rb', line 85 def read_write_stores stores.reject {|s| s.read_only?} end |
#store!(file) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/remote_files/configuration.rb', line 119 def store!(file) store_once!(file) unless file.stored? RemoteFiles.synchronize_stores(file) unless file.stored_everywhere? true end |
#store_once!(file) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/remote_files/configuration.rb', line 97 def store_once!(file) return file.stored_in.first if file.stored? exception = nil read_write_stores.each do |store| begin stored = store.store!(file) file.stored_in << store.identifier break rescue ::RemoteFiles::Error => e file.logger.info(e) if file.logger file.errors.push(e) if file.errors exception = e end end raise exception unless file.stored? file.stored_in.first end |
#stores ⇒ Object
75 76 77 78 79 |
# File 'lib/remote_files/configuration.rb', line 75 def stores raise "You need to configure add stores to the #{name} RemoteFiles configuration" unless configured? @stores end |
#synchronize!(file) ⇒ Object
178 179 180 181 182 183 184 185 |
# File 'lib/remote_files/configuration.rb', line 178 def synchronize!(file) file.missing_stores.each do |store| next if store.read_only? store.store!(file) file.stored_in << store.identifier end end |