Class: Elastics::ModelTasks

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(overrides = {}) ⇒ ModelTasks

Returns a new instance of ModelTasks.



7
8
9
10
11
12
13
14
15
# File 'lib/elastics/model_tasks.rb', line 7

def initialize(overrides={})
  options = Elastics::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]

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/elastics/model_tasks.rb', line 5

def options
  @options
end

Instance Method Details

#default_optionsObject



17
18
19
20
21
22
23
# File 'lib/elastics/model_tasks.rb', line 17

def default_options
  @default_options ||= { :force          => false,
                         :timeout        => 60,
                         :batch_size     => 500,
                         :models         => Conf.elastics_models,
                         :verbose        => true }
end

#import_modelsObject



25
26
27
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
55
56
57
58
59
# File 'lib/elastics/model_tasks.rb', line 25

def import_models
  Prompter.say_title "Import models: #{models.map(&:to_s).inspect}" if options[:verbose]
  Conf.http_client.options[:timeout] = options[:timeout]
  deleted = []
  models.each do |model|
    raise ArgumentError, "The model #{model.name} is not a standard Elastics::ModelIndexer model" \
          unless model.include?(Elastics::ModelIndexer)
    index = model.elastics.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)
        Elastics.delete_index(:index => index)
        deleted << index
        Prompter.say_warning "#{index} index deleted" if options[:verbose]
      end
    end

    # block never called during live-reindex, since prefix_index creates it
    unless Elastics.indices_exists(:index => index)
      Conf.indices.create_index(index)
      Prompter.say_ok "#{index} index created" if options[:verbose]
    end

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

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

    pbar.finish if options[:verbose]
  end
end