Class: Flex::ModelTasks
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #default_options ⇒ Object
- #import_models ⇒ Object
-
#initialize(overrides = {}) ⇒ ModelTasks
constructor
A new instance of ModelTasks.
Methods inherited from Tasks
#config_hash, #original_config_hash
Constructor Details
#initialize(overrides = {}) ⇒ ModelTasks
Returns a new instance of ModelTasks.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/flex/model_tasks.rb', line 25 def initialize(overrides={}) = Flex::Utils. *.keys [:timeout] = [:timeout].to_i if [:timeout] [:batch_size] = [:batch_size].to_i if [:batch_size] [:models] = [:models].split(',') if [:models] if [:import_options] = {} [:import_options].split('&').each do |pair| k, v = pair.split('=') [k.to_sym] = v end [:import_options] = end @options = .merge().merge(overrides) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
23 24 25 |
# File 'lib/flex/model_tasks.rb', line 23 def @options end |
Instance Method Details
#default_options ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/flex/model_tasks.rb', line 44 def @default_options ||= { :force => false, :timeout => 20, :batch_size => 1000, :import_options => { }, :models => Conf.flex_models, :config_file => Conf.config_file, :verbose => true } end |
#import_models ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/flex/model_tasks.rb', line 54 def import_models Conf.http_client.[:timeout] = [:timeout] deleted = [] models.each do |model| raise ArgumentError, "The model #{model.name} is not a standard Flex::ModelIndexer model" \ unless model.include?(Flex::ModelIndexer) index = model.flex.index index = LiveReindex.prefix_index(index) if LiveReindex.should_prefix_index? # block never called during live-reindex, since it doesn't exist if [:force] unless deleted.include?(index) delete_index(index) deleted << index puts "#{index} index deleted" if [:verbose] end end # block never called during live-reindex, since prefix_index creates it unless exist?(index) create(index) puts "#{index} index created" if [:verbose] end if defined?(Mongoid::Document) && model.include?(Mongoid::Document) def model.find_in_batches(={}) 0.step(count, [:batch_size]) do |offset| yield limit([:batch_size]).skip(offset).to_a end end end unless model.respond_to?(:find_in_batches) Conf.logger.error "Model #{model} does not respond to :find_in_batches. Skipped." next end = ProgBar.new(model.count, [:batch_size], "Model #{model}: ") if [:verbose] model.find_in_batches(:batch_size => [:batch_size]) do |batch| result = Flex.post_bulk_collection(batch, [:import_options]) || next .process_result(result, batch.size) if [:verbose] end .finish if [:verbose] end end |