Module: Chewy::RakeHelper

Defined in:
lib/chewy/rake_helper.rb

Class Method Summary collapse

Class Method Details

.eager_load_chewy!Object



22
23
24
25
26
27
28
29
30
# File 'lib/chewy/rake_helper.rb', line 22

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



32
33
34
# File 'lib/chewy/rake_helper.rb', line 32

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.



44
45
46
47
# File 'lib/chewy/rake_helper.rb', line 44

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.



37
38
39
40
41
# File 'lib/chewy/rake_helper.rb', line 37

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

.subscribed_task_stats(&block) ⇒ Object



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

def subscribed_task_stats(&block)
  callback = ->(name, start, finish, id, payload) do
    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
  ActiveSupport::Notifications.subscribed(callback, 'import_objects.chewy') do
    yield
  end
end

.update_allObject



59
60
61
62
# File 'lib/chewy/rake_helper.rb', line 59

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

.update_index(index) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/chewy/rake_helper.rb', line 49

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