Module: Chewy::RakeHelper

Defined in:
lib/chewy/rake_helper.rb

Class Method Summary collapse

Class Method Details

.eager_load_chewy!Object



19
20
21
22
23
24
25
26
27
# File 'lib/chewy/rake_helper.rb', line 19

def eager_load_chewy!
  dirs = Chewy::Railtie.all_engines.map { |engine| engine.paths['app/chewy'] }.compact.map(&:existent).flatten.uniq

  dirs.each do |dir|
    Dir.glob(File.join(dir, '**/*.rb')).each do |file|
      require_dependency file
    end
  end
end

.normalize_index(index) ⇒ Object



29
30
31
# File 'lib/chewy/rake_helper.rb', line 29

def normalize_index index
  "#{index.to_s.gsub(/index\z/i, '').camelize}Index".constantize
end

.reset_allObject

Performs zero downtime reindexing of all documents across all indices.



41
42
43
44
# File 'lib/chewy/rake_helper.rb', line 41

def reset_all
  eager_load_chewy!
  Chewy::Index.descendants.each { |index| reset_index index }
end

.reset_index(index) ⇒ Object

Performs zero downtime reindexing of all documents in the specified index.



34
35
36
37
38
# File 'lib/chewy/rake_helper.rb', line 34

def reset_index index
  index = normalize_index(index)
  puts "Resetting #{index}"
  index.reset! (Time.now.to_f * 1000).round
end

.subscribe_task_stats!Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/chewy/rake_helper.rb', line 5

def subscribe_task_stats!
  ActiveSupport::Notifications.subscribe('import_objects.chewy') do |name, start, finish, id, payload|
    duration = (finish - start).round(2)
    puts "  Imported #{payload[:type]} for #{duration}s, documents total: #{payload[:import].try(:[], :index).to_i}"
    payload[:errors].each do |action, errors|
      puts "    #{action.to_s.humanize} errors:"
      errors.each do |error, documents|
        puts "      `#{error}`"
        puts "        on #{documents.count} documents: #{documents}"
      end
    end if payload[:errors]
  end
end

.update_allObject



56
57
58
59
# File 'lib/chewy/rake_helper.rb', line 56

def update_all
  eager_load_chewy!
  Chewy::Index.descendants.each { |index| update_index index }
end

.update_index(index) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/chewy/rake_helper.rb', line 46

def update_index index
  index = normalize_index(index)
  puts "Updating #{index}"
  if index.exists?
    index.import
  else
    puts "Index `#{index.index_name}` does not exists. Use rake chewy:reset[#{index.index_name}] to create and update it."
  end
end