Class: Flex::ModelTasks

Inherits:
Tasks
  • Object
show all
Defined in:
lib/flex/model_tasks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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={})
  options = Flex::Utils.env2options *default_options.keys

  options[:timeout]    = options[:timeout].to_i      if options[:timeout]
  options[:batch_size] = options[:batch_size].to_i   if options[:batch_size]
  options[:models]     = options[:models].split(',') if options[:models]

  if options[:import_options]
    import_options = {}
    options[:import_options].split('&').each do |pair|
      k, v  = pair.split('=')
      import_options[k.to_sym] = v
    end
    options[:import_options] = import_options
  end

  @options = default_options.merge(options).merge(overrides)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



23
24
25
# File 'lib/flex/model_tasks.rb', line 23

def options
  @options
end

Instance Method Details

#default_optionsObject



44
45
46
47
48
49
50
51
52
# File 'lib/flex/model_tasks.rb', line 44

def default_options
  @default_options ||= { :force          => false,
                         :timeout        => 20,
                         :batch_size     => 1000,
                         :import_options => { },
                         :models         => Conf.flex_models,
                         :config_file    => Conf.config_file,
                         :verbose        => true }
end

#import_modelsObject



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.options[:timeout] = options[: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 options[:force]
      unless deleted.include?(index)
        delete_index(index)
        deleted << index
        puts "#{index} index deleted" if options[:verbose]
      end
    end

    # block never called during live-reindex, since prefix_index creates it
    unless exist?(index)
      create(index)
      puts "#{index} index created" if options[:verbose]
    end

    if defined?(Mongoid::Document) && model.include?(Mongoid::Document)
      def model.find_in_batches(options={})
        0.step(count, options[:batch_size]) do |offset|
          yield limit(options[: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

    pbar = ProgBar.new(model.count, options[:batch_size], "Model #{model}: ") if options[:verbose]

    model.find_in_batches(:batch_size => options[:batch_size]) do |batch|
      result = Flex.post_bulk_collection(batch, options[:import_options]) || next
      pbar.process_result(result, batch.size) if options[:verbose]
    end

    pbar.finish if options[:verbose]
  end
end